How to use ShouldSendEventsWhenFramesAreManipulatedDynamically method of Microsoft.Playwright.Tests.FrameHierarchyTests class

Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.FrameHierarchyTests.ShouldSendEventsWhenFramesAreManipulatedDynamically

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

FrameHierarchyTests.cs

Source: FrameHierarchyTests.cs Github

copy
1/*
2 * MIT License
3 *
4 * Copyright (c) Microsoft Corporation.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25using System.Collections.Generic;
26using System.Linq;
27using System.Threading.Tasks;
28using Microsoft.Playwright.NUnit;
29using NUnit.Framework;
30
31namespace Microsoft.Playwright.Tests
32{
33    public class FrameHierarchyTests : PageTestEx
34    {
35        [PlaywrightTest("frame-hierarchy.spec.ts", "should handle nested frames")]
36        public async Task ShouldHandleNestedFrames()
37        {
38            await Page.GotoAsync(Server.Prefix + "/frames/nested-frames.html");
39            Assert.AreEqual(TestConstants.NestedFramesDumpResult, FrameUtils.DumpFrames(Page.MainFrame));
40        }
41
42        [PlaywrightTest("frame-hierarchy.spec.ts", "should send events when frames are manipulated dynamically")]
43        public async Task ShouldSendEventsWhenFramesAreManipulatedDynamically()
44        {
45            await Page.GotoAsync(Server.EmptyPage);
46            // validate frameattached events
47            var attachedFrames = new List<IFrame>();
48            Page.FrameAttached += (_, e) => attachedFrames.Add(e);
49            await FrameUtils.AttachFrameAsync(Page, "frame1", "./assets/frame.html");
50            Assert.That(attachedFrames, Has.Count.EqualTo(1));
51            StringAssert.Contains("/assets/frame.html", attachedFrames[0].Url);
52
53            // validate framenavigated events
54            var navigatedFrames = new List<IFrame>();
55            Page.FrameNavigated += (_, e) => navigatedFrames.Add(e);
56            await Page.EvaluateAsync(@"() => {
57                const frame = document.getElementById('frame1');
58                frame.src = './empty.html';
59                return new Promise(x => frame.onload = x);
60            }");
61            Assert.That(navigatedFrames, Has.Count.EqualTo(1));
62            Assert.AreEqual(Server.EmptyPage, navigatedFrames[0].Url);
63
64            // validate framedetached events
65            var detachedFrames = new List<IFrame>();
66            Page.FrameDetached += (_, e) => detachedFrames.Add(e);
67            await FrameUtils.DetachFrameAsync(Page, "frame1");
68            Assert.That(detachedFrames, Has.Count.EqualTo(1));
69            Assert.True(detachedFrames[0].IsDetached);
70        }
71
72        [PlaywrightTest("frame-hierarchy.spec.ts", @"should send ""framenavigated"" when navigating on anchor URLs")]
73        public async Task ShouldSendFrameNavigatedWhenNavigatingOnAnchorURLs()
74        {
75            await Page.GotoAsync(Server.EmptyPage);
76
77            await TaskUtils.WhenAll(
78                Page.WaitForNavigationAsync(),
79                Page.GotoAsync(Server.EmptyPage + "#foo"));
80
81            Assert.AreEqual(Server.EmptyPage + "#foo", Page.Url);
82        }
83
84        [PlaywrightTest("frame-hierarchy.spec.ts", "should persist mainFrame on cross-process navigation")]
85        public async Task ShouldPersistMainFrameOnCrossProcessNavigation()
86        {
87            await Page.GotoAsync(Server.EmptyPage);
88            var mainFrame = Page.MainFrame;
89            await Page.GotoAsync(Server.CrossProcessPrefix + "/empty.html");
90            Assert.AreEqual(mainFrame, Page.MainFrame);
91        }
92
93        [PlaywrightTest("frame-hierarchy.spec.ts", "should not send attach/detach events for main frame")]
94        public async Task ShouldNotSendAttachDetachEventsForMainFrame()
95        {
96            bool hasEvents = false;
97            Page.FrameAttached += (_, _) => hasEvents = true;
98            Page.FrameDetached += (_, _) => hasEvents = true;
99            await Page.GotoAsync(Server.EmptyPage);
100            Assert.False(hasEvents);
101        }
102
103        [PlaywrightTest("frame-hierarchy.spec.ts", "should detach child frames on navigation")]
104        public async Task ShouldDetachChildFramesOnNavigation()
105        {
106            var attachedFrames = new List<IFrame>();
107            var detachedFrames = new List<IFrame>();
108            var navigatedFrames = new List<IFrame>();
109            Page.FrameAttached += (_, e) => attachedFrames.Add(e);
110            Page.FrameDetached += (_, e) => detachedFrames.Add(e);
111            Page.FrameNavigated += (_, e) => navigatedFrames.Add(e);
112            await Page.GotoAsync(Server.Prefix + "/frames/nested-frames.html");
113            Assert.AreEqual(4, attachedFrames.Count);
114            Assert.IsEmpty(detachedFrames);
115            Assert.AreEqual(5, navigatedFrames.Count);
116
117            attachedFrames.Clear();
118            detachedFrames.Clear();
119            navigatedFrames.Clear();
120            await Page.GotoAsync(Server.EmptyPage);
121            Assert.IsEmpty(attachedFrames);
122            Assert.AreEqual(4, detachedFrames.Count);
123            Assert.That(navigatedFrames, Has.Count.EqualTo(1));
124        }
125
126        [PlaywrightTest("frame-hierarchy.spec.ts", "should support framesets")]
127        public async Task ShouldSupportFramesets()
128        {
129            var attachedFrames = new List<IFrame>();
130            var detachedFrames = new List<IFrame>();
131            var navigatedFrames = new List<IFrame>();
132            Page.FrameAttached += (_, e) => attachedFrames.Add(e);
133            Page.FrameDetached += (_, e) => detachedFrames.Add(e);
134            Page.FrameNavigated += (_, e) => navigatedFrames.Add(e);
135            await Page.GotoAsync(Server.Prefix + "/frames/frameset.html");
136            Assert.AreEqual(4, attachedFrames.Count);
137            Assert.IsEmpty(detachedFrames);
138            Assert.AreEqual(5, navigatedFrames.Count);
139
140            attachedFrames.Clear();
141            detachedFrames.Clear();
142            navigatedFrames.Clear();
143            await Page.GotoAsync(Server.EmptyPage);
144            Assert.IsEmpty(attachedFrames);
145            Assert.AreEqual(4, detachedFrames.Count);
146            Assert.That(navigatedFrames, Has.Count.EqualTo(1));
147        }
148
149        [PlaywrightTest("frame-hierarchy.spec.ts", "should report frame from-inside shadow DOM")]
150        public async Task ShouldReportFrameFromInsideShadowDOM()
151        {
152            await Page.GotoAsync(Server.Prefix + "/shadow.html");
153            await Page.EvaluateAsync(@"async url => {
154                const frame = document.createElement('iframe');
155                frame.src = url;
156                document.body.shadowRoot.appendChild(frame);
157                await new Promise(x => frame.onload = x);
158            }", Server.EmptyPage);
159            Assert.AreEqual(2, Page.Frames.Count);
160            Assert.AreEqual(Server.EmptyPage, Page.Frames.ElementAt(1).Url);
161        }
162
163        [PlaywrightTest("frame-hierarchy.spec.ts", "should report frame.name()")]
164        public async Task ShouldReportFrameName()
165        {
166            await FrameUtils.AttachFrameAsync(Page, "theFrameId", Server.EmptyPage);
167            await Page.EvaluateAsync(@"url => {
168                const frame = document.createElement('iframe');
169                frame.name = 'theFrameName';
170                frame.src = url;
171                document.body.appendChild(frame);
172                return new Promise(x => frame.onload = x);
173            }", Server.EmptyPage);
174            Assert.IsEmpty(Page.Frames.First().Name);
175            Assert.AreEqual("theFrameId", Page.Frames.ElementAt(1).Name);
176            Assert.AreEqual("theFrameName", Page.Frames.ElementAt(2).Name);
177        }
178
179        [PlaywrightTest("frame-hierarchy.spec.ts", "should report frame.parent()")]
180        public async Task ShouldReportFrameParent()
181        {
182            await FrameUtils.AttachFrameAsync(Page, "frame1", Server.EmptyPage);
183            await FrameUtils.AttachFrameAsync(Page, "frame2", Server.EmptyPage);
184            Assert.Null(Page.Frames.First().ParentFrame);
185            Assert.AreEqual(Page.MainFrame, Page.Frames.ElementAt(1).ParentFrame);
186            Assert.AreEqual(Page.MainFrame, Page.Frames.ElementAt(2).ParentFrame);
187        }
188
189        [PlaywrightTest("frame-hierarchy.spec.ts", "should report different frame instance when frame re-attaches")]
190        public async Task ShouldReportDifferentFrameInstanceWhenFrameReAttaches()
191        {
192            var frame1 = await FrameUtils.AttachFrameAsync(Page, "frame1", Server.EmptyPage);
193            await Page.EvaluateAsync(@"() => {
194                window.frame = document.querySelector('#frame1');
195                window.frame.remove();
196            }");
197            Assert.True(frame1.IsDetached);
198
199            var frameEvent = new TaskCompletionSource<IFrame>();
200            Page.FrameNavigated += (_, frame) => frameEvent.TrySetResult(frame);
201
202            var (frame2, _) = await TaskUtils.WhenAll(
203              frameEvent.Task,
204              Page.EvaluateAsync("() => document.body.appendChild(window.frame)")
205            );
206
207            Assert.False(frame2.IsDetached);
208            Assert.That(frame1, Is.Not.EqualTo(frame2));
209        }
210    }
211}
212
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

Trigger ShouldSendEventsWhenFramesAreManipulatedDynamically code on LambdaTest Cloud Grid

Execute automation tests with ShouldSendEventsWhenFramesAreManipulatedDynamically 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)