How to use ShouldRejectWhenFrameDetaches method of PuppeteerSharp.Tests.NavigationTests.FrameGoToTests class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Tests.NavigationTests.FrameGoToTests.ShouldRejectWhenFrameDetaches

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

FrameGoToTests.cs

Source: FrameGoToTests.cs Github

copy
1using System.Collections.Generic;
2using System.Linq;
3using System.Net;
4using System.Threading.Tasks;
5using Microsoft.AspNetCore.Http;
6using PuppeteerSharp.Tests.Attributes;
7using PuppeteerSharp.Xunit;
8using Xunit;
9using Xunit.Abstractions;
10
11namespace PuppeteerSharp.Tests.NavigationTests
12{
13    [Collection(TestConstants.TestFixtureCollectionName)]
14    public class FrameGoToTests : PuppeteerPageBaseTest
15    {
16        public FrameGoToTests(ITestOutputHelper output) : base(output)
17        {
18        }
19
20        [PuppeteerTest("navigation.spec.ts", "Frame.goto", "should navigate subframes")]
21        [SkipBrowserFact(skipFirefox: true)]
22        public async Task ShouldNavigateSubFrames()
23        {
24            await Page.GoToAsync(TestConstants.ServerUrl + "/frames/one-frame.html");
25            Assert.Single(Page.Frames.Where(f => f.Url.Contains("/frames/one-frame.html")));
26            Assert.Single(Page.Frames.Where(f => f.Url.Contains("/frames/frame.html")));
27            var childFrame = Page.FirstChildFrame();
28            var response = await childFrame.GoToAsync(TestConstants.EmptyPage);
29            Assert.Equal(HttpStatusCode.OK, response.Status);
30            Assert.Same(response.Frame, childFrame);
31        }
32
33        [PuppeteerTest("navigation.spec.ts", "Frame.goto", "should reject when frame detaches")]
34        [SkipBrowserFact(skipFirefox: true)]
35        public async Task ShouldRejectWhenFrameDetaches()
36        {
37            await Page.GoToAsync(TestConstants.ServerUrl + "/frames/one-frame.html");
38            Server.SetRoute("/empty.html", _ => Task.Delay(10000));
39            var waitForRequestTask = Server.WaitForRequest("/empty.html");
40            var navigationTask = Page.FirstChildFrame().GoToAsync(TestConstants.EmptyPage);
41            await waitForRequestTask;
42            await Page.QuerySelectorAsync("iframe").EvaluateFunctionAsync("frame => frame.remove()");
43            var exception = await Assert.ThrowsAsync<NavigationException>(async () => await navigationTask);
44            Assert.Equal("Navigating frame was detached", exception.Message);
45        }
46
47        [PuppeteerTest("navigation.spec.ts", "Frame.goto", "should return matching responses")]
48        [SkipBrowserFact(skipFirefox: true)]
49        public async Task ShouldReturnMatchingResponses()
50        {
51            // Disable cache: otherwise, chromium will cache similar requests.
52            await Page.SetCacheEnabledAsync(false);
53            await Page.GoToAsync(TestConstants.EmptyPage);
54            // Attach three frames.
55            var matchingData = new MatchingResponseData[]
56            {
57                new MatchingResponseData{ FrameTask =  FrameUtils.AttachFrameAsync(Page, "frame1", TestConstants.EmptyPage)},
58                new MatchingResponseData{ FrameTask =  FrameUtils.AttachFrameAsync(Page, "frame2", TestConstants.EmptyPage)},
59                new MatchingResponseData{ FrameTask =  FrameUtils.AttachFrameAsync(Page, "frame3", TestConstants.EmptyPage)}
60            };
61
62            await Task.WhenAll(matchingData.Select(m => m.FrameTask));
63
64            // Navigate all frames to the same URL.
65            var requestHandler = new RequestDelegate(async (context) =>
66            {
67                if (int.TryParse(context.Request.Query["index"], out var index))
68                {
69                    await context.Response.WriteAsync(await matchingData[index].ServerResponseTcs.Task);
70                }
71            });
72
73            Server.SetRoute("/one-style.html?index=0", requestHandler);
74            Server.SetRoute("/one-style.html?index=1", requestHandler);
75            Server.SetRoute("/one-style.html?index=2", requestHandler);
76
77            for (var i = 0; i < 3; ++i)
78            {
79                var waitRequestTask = Server.WaitForRequest("/one-style.html");
80                matchingData[i].NavigationTask = matchingData[i].FrameTask.Result.GoToAsync($"{TestConstants.ServerUrl}/one-style.html?index={i}");
81                await waitRequestTask;
82            }
83            // Respond from server out-of-order.
84            var serverResponseTexts = new string[] { "AAA", "BBB", "CCC" };
85            for (var i = 0; i < 3; ++i)
86            {
87                matchingData[i].ServerResponseTcs.TrySetResult(serverResponseTexts[i]);
88                var response = await matchingData[i].NavigationTask;
89                Assert.Same(matchingData[i].FrameTask.Result, response.Frame);
90                Assert.Equal(serverResponseTexts[i], await response.TextAsync());
91            }
92        }
93
94        private class MatchingResponseData
95        {
96            public Task<Frame> FrameTask { get; internal set; }
97            public TaskCompletionSource<string> ServerResponseTcs { get; internal set; } = new TaskCompletionSource<string>();
98            public Task<Response> NavigationTask { get; internal set; }
99        }
100    }
101}
102
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 ShouldRejectWhenFrameDetaches code on LambdaTest Cloud Grid

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