Best Python code snippet using playwright-python
test_dispatch_event.py
Source:test_dispatch_event.py
...93 )94 await watchdog95 assert await page.evaluate("() => window.clicked")96async def test_should_be_atomic(selectors, page, utils):97 await utils.register_selector_engine(98 selectors,99 "dispatch_event",100 """{101 create(root, target) { },102 query(root, selector) {103 const result = root.querySelector(selector);104 if (result)105 Promise.resolve().then(() => result.onclick = "");106 return result;107 },108 queryAll(root, selector) {109 const result = Array.from(root.query_selector_all(selector));110 for (const e of result)111 Promise.resolve().then(() => result.onclick = "");...
test_queryselector.py
Source:test_queryselector.py
...33 await context.close()34async def test_selectors_register_should_work_with_path(35 selectors, page: Page, utils, assetdir36):37 await utils.register_selector_engine(38 selectors, "foo", path=assetdir / "sectionselectorengine.js"39 )40 await page.set_content("<section></section>")41 assert await page.eval_on_selector("foo=whatever", "e => e.nodeName") == "SECTION"42async def test_selectors_register_should_work_in_main_and_isolated_world(43 selectors, page: Page, utils44):45 dummy_selector_script = """{46 create(root, target) { },47 query(root, selector) {48 return window.__answer;49 },50 queryAll(root, selector) {51 return window['__answer'] ? [window['__answer'], document.body, document.documentElement] : [];52 }53 }"""54 await utils.register_selector_engine(selectors, "main", dummy_selector_script)55 await utils.register_selector_engine(56 selectors, "isolated", dummy_selector_script, content_script=True57 )58 await page.set_content("<div><span><section></section></span></div>")59 await page.evaluate('() => window.__answer = document.querySelector("span")')60 # Works in main if asked.61 assert await page.eval_on_selector("main=ignored", "e => e.nodeName") == "SPAN"62 assert (63 await page.eval_on_selector("css=div >> main=ignored", "e => e.nodeName")64 == "SPAN"65 )66 assert await page.eval_on_selector_all(67 "main=ignored", "es => window.__answer !== undefined"68 )69 assert (70 await page.eval_on_selector_all(71 "main=ignored", "es => es.filter(e => e).length"72 )73 == 374 )75 # Works in isolated by default.76 assert await page.query_selector("isolated=ignored") is None77 assert await page.query_selector("css=div >> isolated=ignored") is None78 # $$eval always works in main, to avoid adopting nodes one by one.79 assert await page.eval_on_selector_all(80 "isolated=ignored", "es => window.__answer !== undefined"81 )82 assert (83 await page.eval_on_selector_all(84 "isolated=ignored", "es => es.filter(e => e).length"85 )86 == 387 )88 # At least one engine in main forces all to be in main.89 assert (90 await page.eval_on_selector(91 "main=ignored >> isolated=ignored", "e => e.nodeName"92 )93 == "SPAN"94 )95 assert (96 await page.eval_on_selector(97 "isolated=ignored >> main=ignored", "e => e.nodeName"98 )99 == "SPAN"100 )101 # Can be chained to css.102 assert (103 await page.eval_on_selector("main=ignored >> css=section", "e => e.nodeName")104 == "SECTION"105 )106async def test_selectors_register_should_handle_errors(selectors, page: Page, utils):107 with pytest.raises(Error) as exc:108 await page.query_selector("neverregister=ignored")109 assert (110 'Unknown engine "neverregister" while parsing selector neverregister=ignored'111 in exc.value.message112 )113 dummy_selector_engine_script = """{114 create(root, target) {115 return target.nodeName;116 },117 query(root, selector) {118 return root.querySelector('dummy');119 },120 queryAll(root, selector) {121 return Array.from(root.query_selector_all('dummy'));122 }123 }"""124 with pytest.raises(Error) as exc:125 await selectors.register("$", dummy_selector_engine_script)126 assert (127 exc.value.message128 == "Selector engine name may only contain [a-zA-Z0-9_] characters"129 )130 # Selector names are case-sensitive.131 await utils.register_selector_engine(132 selectors, "dummy", dummy_selector_engine_script133 )134 await utils.register_selector_engine(135 selectors, "duMMy", dummy_selector_engine_script136 )137 with pytest.raises(Error) as exc:138 await selectors.register("dummy", dummy_selector_engine_script)139 assert exc.value.message == '"dummy" selector engine has been already registered'140 with pytest.raises(Error) as exc:141 await selectors.register("css", dummy_selector_engine_script)...
utils.py
Source:utils.py
...55 assert cast(ViewportSize, page.viewport_size)["width"] == width56 assert cast(ViewportSize, page.viewport_size)["height"] == height57 assert page.evaluate("window.innerWidth") == width58 assert page.evaluate("window.innerHeight") == height59 def register_selector_engine(self, selectors: Selectors, *args, **kwargs) -> None:60 try:61 selectors.register(*args, **kwargs)62 except Error as exc:63 if "has been already registered" not in exc.message:64 raise exc...
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.
Get 100 minutes of automation test minutes FREE!!