How to use test_should_not_send_attach_detach_events_for_main_frame 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.

test_frames.py

Source: test_frames.py Github

copy
1# Copyright (c) Microsoft Corporation.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import asyncio
16
17from playwright.async_api import Error
18
19
20async def test_evaluate_handle(page, server):
21    await page.goto(server.EMPTY_PAGE)
22    main_frame = page.main_frame
23    window_handle = await main_frame.evaluate_handle("window")
24    assert window_handle
25
26
27async def test_frame_element(page, server, utils):
28    await page.goto(server.EMPTY_PAGE)
29    frame1 = await utils.attach_frame(page, "frame1", server.EMPTY_PAGE)
30    await utils.attach_frame(page, "frame2", server.EMPTY_PAGE)
31    frame3 = await utils.attach_frame(page, "frame3", server.EMPTY_PAGE)
32    frame1handle1 = await page.query_selector("#frame1")
33    frame1handle2 = await frame1.frame_element()
34    frame3handle1 = await page.query_selector("#frame3")
35    frame3handle2 = await frame3.frame_element()
36    assert await frame1handle1.evaluate("(a, b) => a === b", frame1handle2)
37    assert await frame3handle1.evaluate("(a, b) => a === b", frame3handle2)
38    assert await frame1handle1.evaluate("(a, b) => a === b", frame3handle1) is False
39
40
41async def test_frame_element_with_content_frame(page, server, utils):
42    await page.goto(server.EMPTY_PAGE)
43    frame = await utils.attach_frame(page, "frame1", server.EMPTY_PAGE)
44    handle = await frame.frame_element()
45    content_frame = await handle.content_frame()
46    assert content_frame == frame
47
48
49async def test_frame_element_throw_when_detached(page, server, utils):
50    await page.goto(server.EMPTY_PAGE)
51    frame1 = await utils.attach_frame(page, "frame1", server.EMPTY_PAGE)
52    await page.eval_on_selector("#frame1", "e => e.remove()")
53    error = None
54    try:
55        await frame1.frame_element()
56    except Error as e:
57        error = e
58    assert error.message == "Frame has been detached."
59
60
61async def test_evaluate_throw_for_detached_frames(page, server, utils):
62    frame1 = await utils.attach_frame(page, "frame1", server.EMPTY_PAGE)
63    await utils.detach_frame(page, "frame1")
64    error = None
65    try:
66        await frame1.evaluate("7 * 8")
67    except Error as e:
68        error = e
69    assert "Execution Context is not available in detached frame" in error.message
70
71
72async def test_evaluate_isolated_between_frames(page, server, utils):
73    await page.goto(server.EMPTY_PAGE)
74    await utils.attach_frame(page, "frame1", server.EMPTY_PAGE)
75    assert len(page.frames) == 2
76    [frame1, frame2] = page.frames
77    assert frame1 != frame2
78
79    await asyncio.gather(
80        frame1.evaluate("window.a = 1"), frame2.evaluate("window.a = 2")
81    )
82    [a1, a2] = await asyncio.gather(
83        frame1.evaluate("window.a"), frame2.evaluate("window.a")
84    )
85    assert a1 == 1
86    assert a2 == 2
87
88
89async def test_should_handle_nested_frames(page, server, utils):
90    await page.goto(server.PREFIX + "/frames/nested-frames.html")
91    assert utils.dump_frames(page.main_frame) == [
92        "http://localhost:<PORT>/frames/nested-frames.html",
93        "    http://localhost:<PORT>/frames/frame.html (aframe)",
94        "    http://localhost:<PORT>/frames/two-frames.html (2frames)",
95        "        http://localhost:<PORT>/frames/frame.html (dos)",
96        "        http://localhost:<PORT>/frames/frame.html (uno)",
97    ]
98
99
100async def test_should_send_events_when_frames_are_manipulated_dynamically(
101    page, server, utils
102):
103    await page.goto(server.EMPTY_PAGE)
104    # validate frameattached events
105    attached_frames = []
106    page.on("frameattached", lambda frame: attached_frames.append(frame))
107    await utils.attach_frame(page, "frame1", "./assets/frame.html")
108    assert len(attached_frames) == 1
109    assert "/assets/frame.html" in attached_frames[0].url
110
111    # validate framenavigated events
112    navigated_frames = []
113    page.on("framenavigated", lambda frame: navigated_frames.append(frame))
114    await page.evaluate(
115        """() => {
116            frame = document.getElementById('frame1')
117            frame.src = './empty.html'
118            return new Promise(x => frame.onload = x)
119        }"""
120    )
121
122    assert len(navigated_frames) == 1
123    assert navigated_frames[0].url == server.EMPTY_PAGE
124
125    # validate framedetached events
126    detached_frames = list()
127    page.on("framedetached", lambda frame: detached_frames.append(frame))
128    await utils.detach_frame(page, "frame1")
129    assert len(detached_frames) == 1
130    assert detached_frames[0].is_detached()
131
132
133async def test_framenavigated_when_navigating_on_anchor_urls(page, server):
134    await page.goto(server.EMPTY_PAGE)
135    async with page.expect_event("framenavigated"):
136        await page.goto(server.EMPTY_PAGE + "#foo")
137    assert page.url == server.EMPTY_PAGE + "#foo"
138
139
140async def test_persist_main_frame_on_cross_process_navigation(page, server):
141    await page.goto(server.EMPTY_PAGE)
142    main_frame = page.main_frame
143    await page.goto(server.CROSS_PROCESS_PREFIX + "/empty.html")
144    assert page.main_frame == main_frame
145
146
147async def test_should_not_send_attach_detach_events_for_main_frame(page, server):
148    has_events = list()
149    page.on("frameattached", lambda frame: has_events.append(True))
150    page.on("framedetached", lambda frame: has_events.append(True))
151    await page.goto(server.EMPTY_PAGE)
152    assert has_events == []
153
154
155async def test_detach_child_frames_on_navigation(page, server):
156    attached_frames = []
157    detached_frames = []
158    navigated_frames = []
159    page.on("frameattached", lambda frame: attached_frames.append(frame))
160    page.on("framedetached", lambda frame: detached_frames.append(frame))
161    page.on("framenavigated", lambda frame: navigated_frames.append(frame))
162    await page.goto(server.PREFIX + "/frames/nested-frames.html")
163    assert len(attached_frames) == 4
164    assert len(detached_frames) == 0
165    assert len(navigated_frames) == 5
166
167    attached_frames = []
168    detached_frames = []
169    navigated_frames = []
170    await page.goto(server.EMPTY_PAGE)
171    assert len(attached_frames) == 0
172    assert len(detached_frames) == 4
173    assert len(navigated_frames) == 1
174
175
176async def test_framesets(page, server):
177    attached_frames = []
178    detached_frames = []
179    navigated_frames = []
180    page.on("frameattached", lambda frame: attached_frames.append(frame))
181    page.on("framedetached", lambda frame: detached_frames.append(frame))
182    page.on("framenavigated", lambda frame: navigated_frames.append(frame))
183    await page.goto(server.PREFIX + "/frames/frameset.html")
184    assert len(attached_frames) == 4
185    assert len(detached_frames) == 0
186    assert len(navigated_frames) == 5
187
188    attached_frames = []
189    detached_frames = []
190    navigated_frames = []
191    await page.goto(server.EMPTY_PAGE)
192    assert len(attached_frames) == 0
193    assert len(detached_frames) == 4
194    assert len(navigated_frames) == 1
195
196
197async def test_frame_from_inside_shadow_dom(page, server):
198    await page.goto(server.PREFIX + "/shadow.html")
199    await page.evaluate(
200        """async url => {
201            frame = document.createElement('iframe');
202            frame.src = url;
203            document.body.shadowRoot.appendChild(frame);
204            await new Promise(x => frame.onload = x);
205        }""",
206        server.EMPTY_PAGE,
207    )
208    assert len(page.frames) == 2
209    assert page.frames[1].url == server.EMPTY_PAGE
210
211
212async def test_frame_name(page, server, utils):
213    await utils.attach_frame(page, "theFrameId", server.EMPTY_PAGE)
214    await page.evaluate(
215        """url => {
216            frame = document.createElement('iframe');
217            frame.name = 'theFrameName';
218            frame.src = url;
219            document.body.appendChild(frame);
220            return new Promise(x => frame.onload = x);
221        }""",
222        server.EMPTY_PAGE,
223    )
224    assert page.frames[0].name == ""
225    assert page.frames[1].name == "theFrameId"
226    assert page.frames[2].name == "theFrameName"
227
228
229async def test_frame_parent(page, server, utils):
230    await utils.attach_frame(page, "frame1", server.EMPTY_PAGE)
231    await utils.attach_frame(page, "frame2", server.EMPTY_PAGE)
232    assert page.frames[0].parent_frame is None
233    assert page.frames[1].parent_frame == page.main_frame
234    assert page.frames[2].parent_frame == page.main_frame
235
236
237async def test_should_report_different_frame_instance_when_frame_re_attaches(
238    page, server, utils
239):
240    frame1 = await utils.attach_frame(page, "frame1", server.EMPTY_PAGE)
241    await page.evaluate(
242        """() => {
243            window.frame = document.querySelector('#frame1')
244            window.frame.remove()
245        }"""
246    )
247
248    assert frame1.is_detached()
249    async with page.expect_event("frameattached") as frame2_info:
250        await page.evaluate("() => document.body.appendChild(window.frame)")
251
252    frame2 = await frame2_info.value
253    assert frame2.is_detached() is False
254    assert frame1 != frame2
255
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)