How to use ShouldNotBeVisibleInBrowserPages method of PuppeteerSharp.Tests.PageTests.CloseTests class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Tests.PageTests.CloseTests.ShouldNotBeVisibleInBrowserPages

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

CloseTests.cs

Source: CloseTests.cs Github

copy
1using System.Threading.Tasks;
2using Xunit;
3using Xunit.Abstractions;
4
5namespace PuppeteerSharp.Tests.PageTests
6{
7    [Collection("PuppeteerLoaderFixture collection")]
8    public class CloseTests : PuppeteerPageBaseTest
9    {
10        public CloseTests(ITestOutputHelper output) : base(output) { }
11
12        [Fact]
13        public async Task ShouldRejectAllPromisesWhenPageIsClosed()
14        {
15            var exceptionTask = Assert.ThrowsAsync<TargetClosedException>(() => Page.EvaluateFunctionAsync("() => new Promise(r => {})"));
16
17            await Task.WhenAll(
18                exceptionTask,
19                Page.CloseAsync()
20            );
21
22            var exception = await exceptionTask;
23            Assert.Contains("Protocol error", exception.Message);
24            Assert.Equal("Target.detachedFromTarget", exception.CloseReason);
25        }
26
27        [Fact]
28        public async Task ShouldNotBeVisibleInBrowserPages()
29        {
30            Assert.Contains(Page, await Browser.PagesAsync());
31            await Page.CloseAsync();
32            Assert.DoesNotContain(Page, await Browser.PagesAsync());
33        }
34
35        [Fact]
36        public async Task ShouldRunBeforeunloadIfAskedFor()
37        {
38            await Page.GoToAsync(TestConstants.ServerUrl + "/beforeunload.html");
39
40            var dialogTask = new TaskCompletionSource<bool>();
41            Page.Dialog += async (sender, e) =>
42            {
43                Assert.Equal(DialogType.BeforeUnload, e.Dialog.DialogType);
44                Assert.Equal(string.Empty, e.Dialog.Message);
45                Assert.Equal(string.Empty, e.Dialog.DefaultValue);
46
47                await e.Dialog.Accept();
48                dialogTask.TrySetResult(true);
49            };
50
51            var closeTask = new TaskCompletionSource<bool>();
52            Page.Close += (sender, e) => closeTask.TrySetResult(true);
53
54            // We have to interact with a page so that 'beforeunload' handlers
55            // fire.
56            await Page.ClickAsync("body");
57            await Page.CloseAsync(new PageCloseOptions { RunBeforeUnload = true });
58
59            await Task.WhenAll(
60                dialogTask.Task,
61                closeTask.Task
62            );
63        }
64
65        [Fact]
66        public async Task ShouldNotRunBeforeunloadByDefault()
67        {
68            await Page.GoToAsync(TestConstants.ServerUrl + "/beforeunload.html");
69            await Page.ClickAsync("body");
70            await Page.CloseAsync();
71        }
72
73        [Fact]
74        public async Task ShouldSetThePageCloseState()
75        {
76            Assert.False(Page.IsClosed);
77            await Page.CloseAsync();
78            Assert.True(Page.IsClosed);
79        }
80
81        [Fact(Timeout = 10000)]
82        public async Task ShouldCloseWhenConnectionBreaksPrematurely()
83        {
84            Browser.Connection.Dispose();
85            await Page.CloseAsync();
86        }
87    }
88}
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 ShouldNotBeVisibleInBrowserPages code on LambdaTest Cloud Grid

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