Best Python code snippet using playwright-python
test_interception.py
Source:test_interception.py
...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!"...
test_locators.py
Source:test_locators.py
...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,...
test_emulation_focus.py
Source:test_emulation_focus.py
...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),...
conftest.py
Source:conftest.py
...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 )...
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!!