How to use ShouldReportDifferentFrameInstanceWhenFrameReAttaches method of PuppeteerSharp.Tests.FrameTests.FrameManagementTests class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Tests.FrameTests.FrameManagementTests.ShouldReportDifferentFrameInstanceWhenFrameReAttaches

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

FrameManagementTests.cs

Source: FrameManagementTests.cs Github

copy
1using System.Collections.Generic;
2using System.Threading.Tasks;
3using Xunit;
4using System.Linq;
5using Xunit.Abstractions;
6
7namespace PuppeteerSharp.Tests.FrameTests
8{
9    [Collection("PuppeteerLoaderFixture collection")]
10    public class FrameManagementTests : PuppeteerPageBaseTest
11    {
12        public FrameManagementTests(ITestOutputHelper output) : base(output)
13        {
14        }
15
16        [Fact]
17        public async Task ShouldHandleNestedFrames()
18        {
19            await Page.GoToAsync(TestConstants.ServerUrl + "/frames/nested-frames.html");
20            Assert.Equal(
21                TestConstants.NestedFramesDumpResult,
22                FrameUtils.DumpFrames(Page.MainFrame));
23        }
24
25        [Fact]
26        public async Task ShouldSendEventsWhenFramesAreManipulatedDynamically()
27        {
28            await Page.GoToAsync(TestConstants.EmptyPage);
29            // validate frameattached events
30            var attachedFrames = new List<Frame>();
31
32            Page.FrameAttached += (sender, e) => attachedFrames.Add(e.Frame);
33
34            await FrameUtils.AttachFrameAsync(Page, "frame1", "./Assets/frame.html");
35
36            Assert.Single(attachedFrames);
37            Assert.Contains("/Assets/frame.html", attachedFrames[0].Url);
38
39            // validate framenavigated events
40            var navigatedFrames = new List<Frame>();
41            Page.FrameNavigated += (sender, e) => navigatedFrames.Add(e.Frame);
42
43            await FrameUtils.NavigateFrameAsync(Page, "frame1", "./empty.html");
44            Assert.Single(navigatedFrames);
45            Assert.Equal(TestConstants.EmptyPage, navigatedFrames[0].Url);
46
47            // validate framedetached events
48            var detachedFrames = new List<Frame>();
49            Page.FrameDetached += (sender, e) => detachedFrames.Add(e.Frame);
50
51            await FrameUtils.DetachFrameAsync(Page, "frame1");
52            Assert.Single(navigatedFrames);
53            Assert.True(navigatedFrames[0].Detached);
54        }
55
56        [Fact]
57        public async Task ShouldSendFrameNavigatedWhenNavigatingOnAnchorURLs()
58        {
59            await Page.GoToAsync(TestConstants.EmptyPage);
60            var frameNavigated = new TaskCompletionSource<bool>();
61            Page.FrameNavigated += (sender, e) => frameNavigated.TrySetResult(true);
62            await Task.WhenAll(
63                Page.GoToAsync(TestConstants.EmptyPage + "#foo"),
64                frameNavigated.Task
65            );
66            Assert.Equal(TestConstants.EmptyPage + "#foo", Page.Url);
67        }
68
69        [Fact]
70        public async Task ShouldPersistMainFrameOnCrossProcessNavigation()
71        {
72            await Page.GoToAsync(TestConstants.EmptyPage);
73            var mainFrame = Page.MainFrame;
74            await Page.GoToAsync(TestConstants.CrossProcessUrl + "/empty.html");
75            Assert.Equal(mainFrame, Page.MainFrame);
76        }
77
78        [Fact]
79        public async Task ShouldNotSendAttachDetachEventsForMainFrame()
80        {
81            var hasEvents = false;
82            Page.FrameAttached += (sender, e) => hasEvents = true;
83            Page.FrameDetached += (sender, e) => hasEvents = true;
84
85            await Page.GoToAsync(TestConstants.EmptyPage);
86            Assert.False(hasEvents);
87        }
88
89        [Fact]
90        public async Task ShouldDetachChildFramesOnNavigation()
91        {
92            var attachedFrames = new List<Frame>();
93            var detachedFrames = new List<Frame>();
94            var navigatedFrames = new List<Frame>();
95
96            Page.FrameAttached += (sender, e) => attachedFrames.Add(e.Frame);
97            Page.FrameDetached += (sender, e) => detachedFrames.Add(e.Frame);
98            Page.FrameNavigated += (sender, e) => navigatedFrames.Add(e.Frame);
99
100            await Page.GoToAsync(TestConstants.ServerUrl + "/frames/nested-frames.html");
101            Assert.Equal(4, attachedFrames.Count);
102            Assert.Empty(detachedFrames);
103            Assert.Equal(5, navigatedFrames.Count);
104
105            attachedFrames.Clear();
106            detachedFrames.Clear();
107            navigatedFrames.Clear();
108
109            await Page.GoToAsync(TestConstants.EmptyPage);
110            Assert.Empty(attachedFrames);
111            Assert.Equal(4, detachedFrames.Count);
112            Assert.Single(navigatedFrames);
113        }
114
115        [Fact]
116        public async Task ShouldReportFrameFromInsideShadowDOM()
117        {
118            await Page.GoToAsync(TestConstants.ServerUrl + "/shadow.html");
119            await Page.EvaluateFunctionAsync(@"async url =>
120            {
121                const frame = document.createElement('iframe');
122                frame.src = url;
123                document.body.shadowRoot.appendChild(frame);
124                await new Promise(x => frame.onload = x);
125            }", TestConstants.EmptyPage);
126            Assert.Equal(2, Page.Frames.Length);
127            Assert.Single(Page.Frames, frame => frame.Url == TestConstants.EmptyPage);
128        }
129
130        [Fact]
131        public async Task ShouldReportFrameName()
132        {
133            await FrameUtils.AttachFrameAsync(Page, "theFrameId", TestConstants.EmptyPage);
134            await Page.EvaluateFunctionAsync(@"url => {
135                const frame = document.createElement('iframe');
136                frame.name = 'theFrameName';
137                frame.src = url;
138                document.body.appendChild(frame);
139                return new Promise(x => frame.onload = x);
140            }", TestConstants.EmptyPage);
141
142            Assert.Single(Page.Frames, frame => frame.Name == string.Empty);
143            Assert.Single(Page.Frames, frame => frame.Name == "theFrameId");
144            Assert.Single(Page.Frames, frame => frame.Name == "theFrameName");
145        }
146
147        [Fact]
148        public async Task ShouldReportFrameParent()
149        {
150            await FrameUtils.AttachFrameAsync(Page, "frame1", TestConstants.EmptyPage);
151            await FrameUtils.AttachFrameAsync(Page, "frame2", TestConstants.EmptyPage);
152
153            Assert.Single(Page.Frames, frame => frame.ParentFrame == null);
154            Assert.Equal(2, Page.Frames.Count(f => f.ParentFrame == Page.MainFrame));
155        }
156
157        [Fact]
158        public async Task ShouldReportDifferentFrameInstanceWhenFrameReAttaches()
159        {
160            var frame1 = await FrameUtils.AttachFrameAsync(Page, "frame1", TestConstants.EmptyPage);
161            await Page.EvaluateFunctionAsync(@"() => {
162                window.frame = document.querySelector('#frame1');
163                window.frame.remove();
164            }");
165            Assert.True(frame1.Detached);
166            var frame2tsc = new TaskCompletionSource<Frame>();
167            Page.FrameAttached += (sender, e) => frame2tsc.TrySetResult(e.Frame);
168            await Page.EvaluateExpressionAsync("document.body.appendChild(window.frame)");
169            var frame2 = await frame2tsc.Task;
170            Assert.False(frame2.Detached);
171            Assert.NotSame(frame1, frame2);
172        }
173    }
174}
175
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 ShouldReportDifferentFrameInstanceWhenFrameReAttaches code on LambdaTest Cloud Grid

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