How to use add_script_tag method in Playwright Python

Best Python code snippet using playwright-python

test_page.py

Source:test_page.py Github

copy

Full Screen

...402 await page.set_content("<div>\n</div>")403 assert await page.eval_on_selector("div", "div => div.textContent") == "\n"404async def test_add_script_tag_should_work_with_a_url(page, server):405 await page.goto(server.EMPTY_PAGE)406 script_handle = await page.add_script_tag(url="/injectedfile.js")407 assert script_handle.as_element()408 assert await page.evaluate("__injected") == 42409async def test_add_script_tag_should_work_with_a_url_and_type_module(page, server):410 await page.goto(server.EMPTY_PAGE)411 await page.add_script_tag(url="/es6/es6import.js", type="module")412 assert await page.evaluate("__es6injected") == 42413async def test_add_script_tag_should_work_with_a_path_and_type_module(414 page, server, assetdir415):416 await page.goto(server.EMPTY_PAGE)417 await page.add_script_tag(path=assetdir / "es6" / "es6pathimport.js", type="module")418 await page.wait_for_function("window.__es6injected")419 assert await page.evaluate("__es6injected") == 42420async def test_add_script_tag_should_work_with_a_content_and_type_module(page, server):421 await page.goto(server.EMPTY_PAGE)422 await page.add_script_tag(423 content="import num from '/es6/es6module.js';window.__es6injected = num;",424 type="module",425 )426 await page.wait_for_function("window.__es6injected")427 assert await page.evaluate("__es6injected") == 42428async def test_add_script_tag_should_throw_an_error_if_loading_from_url_fail(429 page, server430):431 await page.goto(server.EMPTY_PAGE)432 with pytest.raises(Error) as exc_info:433 await page.add_script_tag(url="/nonexistfile.js")434 assert exc_info.value435async def test_add_script_tag_should_work_with_a_path(page, server, assetdir):436 await page.goto(server.EMPTY_PAGE)437 script_handle = await page.add_script_tag(path=assetdir / "injectedfile.js")438 assert script_handle.as_element()439 assert await page.evaluate("__injected") == 42440@pytest.mark.skip_browser("webkit")441async def test_add_script_tag_should_include_source_url_when_path_is_provided(442 page, server, assetdir443):444 # Lacking sourceURL support in WebKit445 await page.goto(server.EMPTY_PAGE)446 await page.add_script_tag(path=assetdir / "injectedfile.js")447 result = await page.evaluate("__injectedError.stack")448 assert os.path.join("assets", "injectedfile.js") in result449async def test_add_script_tag_should_work_with_content(page, server):450 await page.goto(server.EMPTY_PAGE)451 script_handle = await page.add_script_tag(content="window.__injected = 35;")452 assert script_handle.as_element()453 assert await page.evaluate("__injected") == 35454@pytest.mark.skip_browser("firefox")455async def test_add_script_tag_should_throw_when_added_with_content_to_the_csp_page(456 page, server457):458 # Firefox fires onload for blocked script before it issues the CSP console error.459 await page.goto(server.PREFIX + "/csp.html")460 with pytest.raises(Error) as exc_info:461 await page.add_script_tag(content="window.__injected = 35;")462 assert exc_info.value463async def test_add_script_tag_should_throw_when_added_with_URL_to_the_csp_page(464 page, server465):466 await page.goto(server.PREFIX + "/csp.html")467 with pytest.raises(Error) as exc_info:468 await page.add_script_tag(url=server.CROSS_PROCESS_PREFIX + "/injectedfile.js")469 assert exc_info.value470async def test_add_script_tag_should_throw_a_nice_error_when_the_request_fails(471 page, server472):473 await page.goto(server.EMPTY_PAGE)474 url = server.PREFIX + "/this_does_not_exist.js"475 with pytest.raises(Error) as exc_info:476 await page.add_script_tag(url=url)477 assert url in exc_info.value.message478async def test_add_style_tag_should_work_with_a_url(page, server):479 await page.goto(server.EMPTY_PAGE)480 style_handle = await page.add_style_tag(url="/injectedstyle.css")481 assert style_handle.as_element()482 assert (483 await page.evaluate(484 "window.getComputedStyle(document.querySelector('body')).getPropertyValue('background-color')"485 )486 == "rgb(255, 0, 0)"487 )488async def test_add_style_tag_should_throw_an_error_if_loading_from_url_fail(489 page, server490):...

Full Screen

Full Screen

test_browsercontext.py

Source:test_browsercontext.py Github

copy

Full Screen

...160 context = await browser.new_context()161 page = await context.new_page()162 await page.goto(server.PREFIX + "/csp.html")163 try:164 await page.add_script_tag(content="window.__injected = 42;")165 except Error:166 pass167 assert await page.evaluate("window.__injected") is None168 await context.close()169 await baseline()170 # By-pass CSP and try one more time.171 async def override():172 context = await browser.new_context(bypass_csp=True)173 page = await context.new_page()174 await page.goto(server.PREFIX + "/csp.html")175 await page.add_script_tag(content="window.__injected = 42;")176 assert await page.evaluate("() => window.__injected") == 42177 await context.close()178 await override()179async def test_page_event_should_bypass_csp_header(browser, server):180 # Make sure CSP prohibits add_script_tag.181 server.set_csp("/empty.html", 'default-src "self"')182 async def baseline():183 context = await browser.new_context()184 page = await context.new_page()185 await page.goto(server.EMPTY_PAGE)186 try:187 await page.add_script_tag(content="window.__injected = 42;")188 except Error:189 pass190 assert await page.evaluate("() => window.__injected") is None191 await context.close()192 await baseline()193 # By-pass CSP and try one more time.194 async def override():195 context = await browser.new_context(bypass_csp=True)196 page = await context.new_page()197 await page.goto(server.EMPTY_PAGE)198 await page.add_script_tag(content="window.__injected = 42;")199 assert await page.evaluate("window.__injected") == 42200 await context.close()201 await override()202async def test_page_event_should_bypass_after_cross_process_navigation(browser, server):203 context = await browser.new_context(bypass_csp=True)204 page = await context.new_page()205 await page.goto(server.PREFIX + "/csp.html")206 await page.add_script_tag(content="window.__injected = 42;")207 assert await page.evaluate("window.__injected") == 42208 await page.goto(server.CROSS_PROCESS_PREFIX + "/csp.html")209 await page.add_script_tag(content="window.__injected = 42;")210 assert await page.evaluate("window.__injected") == 42211 await context.close()212async def test_page_event_should_bypass_csp_in_iframes_as_well(browser, server, utils):213 async def baseline():214 # Make sure CSP prohibits add_script_tag in an iframe.215 context = await browser.new_context()216 page = await context.new_page()217 await page.goto(server.EMPTY_PAGE)218 frame = await utils.attach_frame(page, "frame1", server.PREFIX + "/csp.html")219 try:220 await frame.add_script_tag(content="window.__injected = 42;")221 except Error:222 pass223 assert await frame.evaluate("window.__injected") is None224 await context.close()225 await baseline()226 # By-pass CSP and try one more time.227 async def override():228 context = await browser.new_context(bypass_csp=True)229 page = await context.new_page()230 await page.goto(server.EMPTY_PAGE)231 frame = await utils.attach_frame(page, "frame1", server.PREFIX + "/csp.html")232 try:233 await frame.add_script_tag(content="window.__injected = 42;")234 except Error:235 pass236 assert await frame.evaluate("window.__injected") == 42237 await context.close()238 await override()239async def test_csp_should_work(browser, is_webkit):240 async def baseline():241 context = await browser.new_context(java_script_enabled=False)242 page = await context.new_page()243 await page.goto('data:text/html, <script>var something = "forbidden"</script>')244 with pytest.raises(Error) as exc_info:245 await page.evaluate("something")246 if is_webkit:247 assert "Can't find variable: something" in exc_info.value.message...

Full Screen

Full Screen

test_defaultbrowsercontext.py

Source:test_defaultbrowsercontext.py Github

copy

Full Screen

...144 assert request.getHeader("user-agent") == "foobar"145async def test_should_support_bypass_csp_option(launch_persistent, server):146 (page, context) = await launch_persistent(bypass_csp=True)147 await page.goto(server.PREFIX + "/csp.html")148 await page.add_script_tag(content="window.__injected = 42;")149 assert await page.evaluate("() => window.__injected") == 42150async def test_should_support_javascript_enabled_option(launch_persistent, is_webkit):151 (page, context) = await launch_persistent(java_script_enabled=False)152 await page.goto('data:text/html, <script>var something = "forbidden"</script>')153 with pytest.raises(Error) as exc:154 await page.evaluate("something")155 if is_webkit:156 assert "Can't find variable: something" in exc.value.message157 else:158 assert "something is not defined" in exc.value.message159async def test_should_support_http_credentials_option(server, launch_persistent):160 (page, context) = await launch_persistent(161 http_credentials={"username": "user", "password": "pass"}162 )...

Full Screen

Full Screen

d3.py

Source:d3.py Github

copy

Full Screen

...19 path, _ = split(abspath(__file__))20 with open(join(path, name)) as f:21 result = f.read()22 return result23def add_script_tag(source):24 """ wrap the source file in a script tag """25 return """<script src="{}" type="text/javascript"></script>""".format(source)26class D3Object(object):27 """ Base class for d3.js helper classes """28 libs = [] # custom libs (for the page tail)29 styles = [] # custom styles30 css = "" # component specific css31 js = "" # component specific js32 name = property(lambda self: '%s_%s' % (self.__class__.__name__, id(self)))33 ref = property(lambda self: self.name, doc="the reference to the object")34 wrapper = '<div class="dz-d3-vis" id="{}"></div>'35 def __init__(self, data, options=None, **kwargs):36 """ Initialize the d3 object class37 data -- data for the chart...

Full Screen

Full Screen

Playwright tutorial

LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.

Chapters:

  1. What is Playwright : Playwright is comparatively new but has gained good popularity. Get to know some history of the Playwright with some interesting facts connected with it.
  2. How To Install Playwright : Learn in detail about what basic configuration and dependencies are required for installing Playwright and run a test. Get a step-by-step direction for installing the Playwright automation framework.
  3. Playwright Futuristic Features: Launched in 2020, Playwright gained huge popularity quickly because of some obliging features such as Playwright Test Generator and Inspector, Playwright Reporter, Playwright auto-waiting mechanism and etc. Read up on those features to master Playwright testing.
  4. What is Component Testing: Component testing in Playwright is a unique feature that allows a tester to test a single component of a web application without integrating them with other elements. Learn how to perform Component testing on the Playwright automation framework.
  5. Inputs And Buttons In Playwright: Every website has Input boxes and buttons; learn about testing inputs and buttons with different scenarios and examples.
  6. Functions and Selectors in Playwright: Learn how to launch the Chromium browser with Playwright. Also, gain a better understanding of some important functions like “BrowserContext,” which allows you to run multiple browser sessions, and “newPage” which interacts with a page.
  7. Handling Alerts and Dropdowns in Playwright : Playwright interact with different types of alerts and pop-ups, such as simple, confirmation, and prompt, and different types of dropdowns, such as single selector and multi-selector get your hands-on with handling alerts and dropdown in Playright testing.
  8. Playwright vs Puppeteer: Get to know about the difference between two testing frameworks and how they are different than one another, which browsers they support, and what features they provide.
  9. Run Playwright Tests on LambdaTest: Playwright testing with LambdaTest leverages test performance to the utmost. You can run multiple Playwright tests in Parallel with the LammbdaTest test cloud. Get a step-by-step guide to run your Playwright test on the LambdaTest platform.
  10. Playwright Python Tutorial: Playwright automation framework support all major languages such as Python, JavaScript, TypeScript, .NET and etc. However, there are various advantages to Python end-to-end testing with Playwright because of its versatile utility. Get the hang of Playwright python testing with this chapter.
  11. Playwright End To End Testing Tutorial: Get your hands on with Playwright end-to-end testing and learn to use some exciting features such as TraceViewer, Debugging, Networking, Component testing, Visual testing, and many more.
  12. Playwright Video Tutorial: Watch the video tutorials on Playwright testing from experts and get a consecutive in-depth explanation of Playwright automation testing.

Run Playwright Python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful