How to use test_frame_element method in Playwright Python

Best Python code snippet using playwright-python

Run Playwright Python automation tests on LambdaTest cloud grid

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

iframe.py

Source: iframe.py Github

copy
1import pytest
2
3from tests.support.asserts import assert_success
4from tests.support.image import png_dimensions
5
6from . import element_dimensions
7
8DEFAULT_CONTENT = "<div id='content'>Lorem ipsum dolor sit amet.</div>"
9
10REFERENCE_CONTENT = "<div id='outer'>{}</div>".format(DEFAULT_CONTENT)
11REFERENCE_STYLE = """
12    <style>
13      #outer {
14        display: block;
15        margin: 0;
16        border: 0;
17        width: 200px;
18        height: 200px;
19      }
20      #content {
21        display: block;
22        margin: 0;
23        border: 0;
24        width: 100px;
25        height: 100px;
26        background: green;
27      }
28    </style>
29"""
30
31OUTER_IFRAME_STYLE = """
32    <style>
33      iframe {
34        display: block;
35        margin: 0;
36        border: 0;
37        width: 200px;
38        height: 200px;
39      }
40    </style>
41"""
42
43INNER_IFRAME_STYLE = """
44    <style>
45      body {
46        margin: 0;
47      }
48      div {
49        display: block;
50        margin: 0;
51        border: 0;
52        width: 100px;
53        height: 100px;
54        background: green;
55      }
56    </style>
57"""
58
59
60def take_element_screenshot(session, element_id):
61    return session.transport.send(
62        "GET",
63        "session/{session_id}/element/{element_id}/screenshot".format(
64            session_id=session.session_id,
65            element_id=element_id,
66        )
67    )
68
69
70def test_frame_element(session, inline, iframe):
71    # Create a reference element which looks exactly like the frame's content
72    session.url = inline("{0}{1}".format(REFERENCE_STYLE, REFERENCE_CONTENT))
73
74    # Capture the inner content as reference image
75    ref_el = session.find.css("#content", all=False)
76    ref_screenshot = ref_el.screenshot()
77    ref_dimensions = element_dimensions(session, ref_el)
78
79    assert png_dimensions(ref_screenshot) == ref_dimensions
80
81    # Capture the frame's element
82    iframe_content = "{0}{1}".format(INNER_IFRAME_STYLE, DEFAULT_CONTENT)
83    session.url = inline("""{0}{1}""".format(OUTER_IFRAME_STYLE, iframe(iframe_content)))
84
85    frame = session.find.css("iframe", all=False)
86    session.switch_frame(frame)
87    div = session.find.css("div", all=False)
88    div_dimensions = element_dimensions(session, div)
89    assert div_dimensions == ref_dimensions
90
91    response = take_element_screenshot(session, div.id)
92    div_screenshot = assert_success(response)
93
94    assert png_dimensions(div_screenshot) == ref_dimensions
95    assert div_screenshot == ref_screenshot
96
97
98@pytest.mark.parametrize("domain", ["", "alt"], ids=["same_origin", "cross_origin"])
99def test_source_origin(session, inline, iframe, domain):
100    # Create a reference element which looks exactly like the iframe
101    session.url = inline("{0}{1}".format(REFERENCE_STYLE, REFERENCE_CONTENT))
102
103    div = session.find.css("div", all=False)
104    div_dimensions = element_dimensions(session, div)
105
106    response = take_element_screenshot(session, div.id)
107    reference_screenshot = assert_success(response)
108    assert png_dimensions(reference_screenshot) == div_dimensions
109
110    iframe_content = "{0}{1}".format(INNER_IFRAME_STYLE, DEFAULT_CONTENT)
111    session.url = inline("""{0}{1}""".format(
112        OUTER_IFRAME_STYLE, iframe(iframe_content, domain=domain)))
113
114    frame_element = session.find.css("iframe", all=False)
115    frame_dimensions = element_dimensions(session, frame_element)
116
117    response = take_element_screenshot(session, frame_element.id)
118    screenshot = assert_success(response)
119    assert png_dimensions(screenshot) == frame_dimensions
120
121    assert screenshot == reference_screenshot
122
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Python Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Python on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)