How to use assert_to_be_golden method in Playwright Python

Best Python code snippet using playwright-python

test_interception.py

Source:test_interception.py Github

copy

Full Screen

...572 server.PREFIX,573 )574 img = await page.query_selector("img")575 assert img576 assert_to_be_golden(await img.screenshot(), "mock-binary-response.png")577async def test_request_fulfill_should_allow_mocking_svg_with_charset(578 page, server, assert_to_be_golden579):580 await page.route(581 "**/*",582 lambda route: route.fulfill(583 content_type="image/svg+xml ; charset=utf-8",584 body='<svg width="50" height="50" version="1.1" xmlns="http://www.w3.org/2000/svg"><rect x="10" y="10" width="30" height="30" stroke="black" fill="transparent" stroke-width="5"/></svg>',585 ),586 )587 await page.evaluate(588 """PREFIX => {589 const img = document.createElement('img');590 img.src = PREFIX + '/does-not-exist.svg';591 document.body.appendChild(img);592 return new Promise((f, r) => { img.onload = f; img.onerror = r; });593 }""",594 server.PREFIX,595 )596 img = await page.query_selector("img")597 assert_to_be_golden(await img.screenshot(), "mock-svg.png")598async def test_request_fulfill_should_work_with_file_path(599 page: Page, server, assert_to_be_golden, assetdir600):601 await page.route(602 "**/*",603 lambda route: route.fulfill(604 content_type="shouldBeIgnored", path=assetdir / "pptr.png"605 ),606 )607 await page.evaluate(608 """PREFIX => {609 const img = document.createElement('img');610 img.src = PREFIX + '/does-not-exist.png';611 document.body.appendChild(img);612 return new Promise(fulfill => img.onload = fulfill);613 }""",614 server.PREFIX,615 )616 img = await page.query_selector("img")617 assert img618 assert_to_be_golden(await img.screenshot(), "mock-binary-response.png")619async def test_request_fulfill_should_stringify_intercepted_request_response_headers(620 page, server621):622 await page.route(623 "**/*",624 lambda route: route.fulfill(625 status=200, headers={"foo": True}, body="Yo, page!"626 ),627 )628 response = await page.goto(server.EMPTY_PAGE)629 assert response.status == 200630 headers = response.headers631 assert headers["foo"] == "True"632 assert await page.evaluate("() => document.body.textContent") == "Yo, page!"...

Full Screen

Full Screen

test_locators.py

Source:test_locators.py Github

copy

Full Screen

...287 )288 page.goto(server.PREFIX + "/grid.html")289 page.evaluate("window.scrollBy(50, 100)")290 element = page.locator(".box:nth-of-type(3)")291 assert_to_be_golden(element.screenshot(), "screenshot-element-bounding-box.png")292def test_locators_should_return_bounding_box(page: Page, server: Server) -> None:293 page.set_viewport_size(294 {295 "width": 500,296 "height": 500,297 }298 )299 page.goto(server.PREFIX + "/grid.html")300 element = page.locator(".box:nth-of-type(13)")301 box = element.bounding_box()302 assert box == {303 "x": 100,304 "y": 50,305 "width": 50,...

Full Screen

Full Screen

test_emulation_focus.py

Source:test_emulation_focus.py Github

copy

Full Screen

...98 screenshots = await asyncio.gather(99 page.screenshot(),100 page2.screenshot(),101 )102 assert_to_be_golden(screenshots[0], "screenshot-sanity.png")103 assert_to_be_golden(screenshots[1], "grid-cell-0.png")104async def test_should_change_focused_iframe(page, server, utils):105 await page.goto(server.EMPTY_PAGE)106 [frame1, frame2] = await asyncio.gather(107 utils.attach_frame(page, "frame1", server.PREFIX + "/input/textarea.html"),108 utils.attach_frame(page, "frame2", server.PREFIX + "/input/textarea.html"),109 )110 logger = """() => {111 self._events = [];112 const element = document.querySelector('input');113 element.onfocus = element.onblur = (e) => self._events.push(e.type);114 }"""115 await asyncio.gather(116 frame1.evaluate(logger),117 frame2.evaluate(logger),...

Full Screen

Full Screen

conftest.py

Source:conftest.py Github

copy

Full Screen

...117 default=False,118 help="Run tests in headful mode.",119 )120@pytest.fixture(scope="session")121def assert_to_be_golden(browser_name: str):122 def compare(received_raw: bytes, golden_name: str):123 golden_file = (_dirname / f"golden-{browser_name}" / golden_name).read_bytes()124 received_image = Image.open(io.BytesIO(received_raw))125 golden_image = Image.open(io.BytesIO(golden_file))126 if golden_image.size != received_image.size:127 pytest.fail("Image size differs to golden image")128 return129 diff_pixels = pixelmatch(130 from_PIL_to_raw_data(received_image),131 from_PIL_to_raw_data(golden_image),132 golden_image.size[0],133 golden_image.size[1],134 threshold=0.2,135 )...

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