How to use register_selector_engine method in Playwright Python

Best Python code snippet using playwright-python

test_dispatch_event.py

Source:test_dispatch_event.py Github

copy

Full Screen

...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 = "");...

Full Screen

Full Screen

test_queryselector.py

Source:test_queryselector.py Github

copy

Full Screen

...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)...

Full Screen

Full Screen

utils.py

Source:utils.py Github

copy

Full Screen

...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...

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