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

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

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(TestConstants.NestedFramesDumpResult, FrameUtils.DumpFrames(Page.MainFrame));
21        }
22
23        [Fact]
24        public async Task ShouldSendEventsWhenFramesAreManipulatedDynamically()
25        {
26            await Page.GoToAsync(TestConstants.EmptyPage);
27            // validate frameattached events
28            var attachedFrames = new List<Frame>();
29
30            Page.FrameAttached += (sender, e) => attachedFrames.Add(e.Frame);
31
32            await FrameUtils.AttachFrameAsync(Page, "frame1", "./assets/frame.html");
33
34            Assert.Single(attachedFrames);
35            Assert.Contains("/assets/frame.html", attachedFrames[0].Url);
36
37            // validate framenavigated events
38            var navigatedFrames = new List<Frame>();
39            Page.FrameNavigated += (sender, e) => navigatedFrames.Add(e.Frame);
40
41            await FrameUtils.NavigateFrameAsync(Page, "frame1", "./empty.html");
42            Assert.Single(navigatedFrames);
43            Assert.Equal(TestConstants.EmptyPage, navigatedFrames[0].Url);
44
45            // validate framedetached events
46            var detachedFrames = new List<Frame>();
47            Page.FrameDetached += (sender, e) => detachedFrames.Add(e.Frame);
48
49            await FrameUtils.DetachFrameAsync(Page, "frame1");
50            Assert.Single(navigatedFrames);
51            Assert.True(navigatedFrames[0].Detached);
52        }
53
54        [Fact]
55        public async Task ShouldPersistMainFrameOnCrossProcessNavigation()
56        {
57            await Page.GoToAsync(TestConstants.EmptyPage);
58            var mainFrame = Page.MainFrame;
59            await Page.GoToAsync(TestConstants.CrossProcessUrl + "/empty.html");
60            Assert.Equal(mainFrame, Page.MainFrame);
61        }
62
63        [Fact]
64        public async Task ShouldNotSendAttachDetachEventsForMainFrame()
65        {
66            var hasEvents = false;
67            Page.FrameAttached += (sender, e) => hasEvents = true;
68            Page.FrameDetached += (sender, e) => hasEvents = true;
69
70            await Page.GoToAsync(TestConstants.EmptyPage);
71            Assert.False(hasEvents);
72        }
73
74        [Fact]
75        public async Task ShouldDetachChildFramesOnNavigation()
76        {
77            var attachedFrames = new List<Frame>();
78            var detachedFrames = new List<Frame>();
79            var navigatedFrames = new List<Frame>();
80
81            Page.FrameAttached += (sender, e) => attachedFrames.Add(e.Frame);
82            Page.FrameDetached += (sender, e) => detachedFrames.Add(e.Frame);
83            Page.FrameNavigated += (sender, e) => navigatedFrames.Add(e.Frame);
84
85            await Page.GoToAsync(TestConstants.ServerUrl + "/frames/nested-frames.html");
86            Assert.Equal(4, attachedFrames.Count);
87            Assert.Empty(detachedFrames);
88            Assert.Equal(5, navigatedFrames.Count);
89
90            attachedFrames.Clear();
91            detachedFrames.Clear();
92            navigatedFrames.Clear();
93
94            await Page.GoToAsync(TestConstants.EmptyPage);
95            Assert.Empty(attachedFrames);
96            Assert.Equal(4, detachedFrames.Count);
97            Assert.Single(navigatedFrames);
98        }
99
100        [Fact]
101        public async Task ShouldReportFrameName()
102        {
103            await FrameUtils.AttachFrameAsync(Page, "theFrameId", TestConstants.EmptyPage);
104            await Page.EvaluateFunctionAsync(@"url => {
105                const frame = document.createElement('iframe');
106                frame.name = 'theFrameName';
107                frame.src = url;
108                document.body.appendChild(frame);
109                return new Promise(x => frame.onload = x);
110            }", TestConstants.EmptyPage);
111
112            Assert.Equal(string.Empty, Page.Frames.ElementAt(0).Name);
113            Assert.Equal("theFrameId", Page.Frames.ElementAt(1).Name);
114            Assert.Equal("theFrameName", Page.Frames.ElementAt(2).Name);
115        }
116
117        [Fact]
118        public async Task ShouldReportFrameParent()
119        {
120            await FrameUtils.AttachFrameAsync(Page, "frame1", TestConstants.EmptyPage);
121            await FrameUtils.AttachFrameAsync(Page, "frame2", TestConstants.EmptyPage);
122
123            Assert.Null(Page.Frames.ElementAt(0).ParentFrame);
124            Assert.Equal(Page.MainFrame, Page.Frames.ElementAt(1).ParentFrame);
125            Assert.Equal(Page.MainFrame, Page.Frames.ElementAt(2).ParentFrame);
126        }
127    }
128}
129
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
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)