How to use ShouldWork method of PuppeteerSharp.Tests.PageTests.PageEventsPopupTests class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Tests.PageTests.PageEventsPopupTests.ShouldWork

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

PageEventsPopupTests.cs

Source: PageEventsPopupTests.cs Github

copy
1using System.Threading.Tasks;
2using PuppeteerSharp.Tests.Attributes;
3using PuppeteerSharp.Xunit;
4using Xunit;
5using Xunit.Abstractions;
6
7namespace PuppeteerSharp.Tests.PageTests
8{
9    [Collection(TestConstants.TestFixtureCollectionName)]
10    public class PageEventsPopupTests : PuppeteerPageBaseTest
11    {
12        public PageEventsPopupTests(ITestOutputHelper output) : base(output)
13        {
14        }
15
16        [PuppeteerTest("page.spec.ts", "Page.Events.Popup", "should work")]
17        [SkipBrowserFact(skipFirefox: true)]
18        public async Task ShouldWork()
19        {
20            var popupTaskSource = new TaskCompletionSource<Page>();
21            Page.Popup += (_, e) => popupTaskSource.TrySetResult(e.PopupPage);
22
23            await Task.WhenAll(
24                popupTaskSource.Task,
25                Page.EvaluateExpressionAsync("window.open('about:blank')"));
26
27            Assert.False(await Page.EvaluateExpressionAsync<bool>("!!window.opener"));
28            Assert.True(await popupTaskSource.Task.Result.EvaluateExpressionAsync<bool>("!!window.opener"));
29        }
30
31        [PuppeteerTest("page.spec.ts", "Page.Events.Popup", "should work with noopener")]
32        [SkipBrowserFact(skipFirefox: true)]
33        public async Task ShouldWorkWithNoopener()
34        {
35            var popupTaskSource = new TaskCompletionSource<Page>();
36            Page.Popup += (_, e) => popupTaskSource.TrySetResult(e.PopupPage);
37
38            await Task.WhenAll(
39                popupTaskSource.Task,
40                Page.EvaluateExpressionAsync("window.open('about:blank', null, 'noopener')"));
41
42            Assert.False(await Page.EvaluateExpressionAsync<bool>("!!window.opener"));
43            Assert.False(await popupTaskSource.Task.Result.EvaluateExpressionAsync<bool>("!!window.opener"));
44        }
45
46        [PuppeteerTest("page.spec.ts", "Page.Events.Popup", "should work with clicking target=_blank and without rel=opener")]
47        [SkipBrowserFact(skipFirefox: true)]
48        public async Task ShouldWorkWithClickingTargetBlankWithoutRelOpener()
49        {
50            await Page.GoToAsync(TestConstants.EmptyPage);
51            await Page.SetContentAsync("<a target=_blank href='/one-style.html'>yo</a>");
52
53            var popupTaskSource = new TaskCompletionSource<Page>();
54            Page.Popup += (_, e) => popupTaskSource.TrySetResult(e.PopupPage);
55
56            await Task.WhenAll(
57                popupTaskSource.Task,
58                Page.ClickAsync("a"));
59
60            Assert.False(await Page.EvaluateExpressionAsync<bool>("!!window.opener"));
61            Assert.False(await popupTaskSource.Task.Result.EvaluateExpressionAsync<bool>("!!window.opener"));
62        }
63
64        [PuppeteerTest("page.spec.ts", "Page.Events.Popup", "should work with clicking target=_blank and with rel=opener")]
65        [SkipBrowserFact(skipFirefox: true)]
66        public async Task ShouldWorkWithFakeClickingTargetBlankAndReNoopener()
67        {
68            await Page.GoToAsync(TestConstants.EmptyPage);
69            await Page.SetContentAsync("<a target=_blank rel=noopener href='/one-style.html'>yo</a>");
70
71            var popupTaskSource = new TaskCompletionSource<Page>();
72            Page.Popup += (_, e) => popupTaskSource.TrySetResult(e.PopupPage);
73
74            await Task.WhenAll(
75                popupTaskSource.Task,
76                Page.QuerySelectorAsync("a").EvaluateFunctionAsync("a => a.click()"));
77
78            Assert.False(await Page.EvaluateExpressionAsync<bool>("!!window.opener"));
79            Assert.False(await popupTaskSource.Task.Result.EvaluateExpressionAsync<bool>("!!window.opener"));
80        }
81
82        [PuppeteerTest("page.spec.ts", "Page.Events.Popup", "should work with fake-clicking target=_blank and rel=noopener")]
83        [SkipBrowserFact(skipFirefox: true)]
84        public async Task ShouldWorkWithFakeClickingTargetBlankAndRelNoopener()
85        {
86            await Page.GoToAsync(TestConstants.EmptyPage);
87            await Page.SetContentAsync("<a target=_blank rel=noopener href='/one-style.html'>yo</a>");
88
89            var popupTaskSource = new TaskCompletionSource<Page>();
90            Page.Popup += (_, e) => popupTaskSource.TrySetResult(e.PopupPage);
91
92            await Task.WhenAll(
93                popupTaskSource.Task,
94                Page.QuerySelectorAsync("a").EvaluateFunctionAsync("a => a.click()"));
95
96            Assert.False(await Page.EvaluateExpressionAsync<bool>("!!window.opener"));
97            Assert.False(await popupTaskSource.Task.Result.EvaluateExpressionAsync<bool>("!!window.opener"));
98        }
99
100        [PuppeteerTest("page.spec.ts", "Page.Events.Popup", "should work with clicking target=_blank and rel=noopener")]
101        [SkipBrowserFact(skipFirefox: true)]
102        public async Task ShouldWorkWithClickingTargetBlankAndRelNoopener()
103        {
104            await Page.GoToAsync(TestConstants.EmptyPage);
105            await Page.SetContentAsync("<a target=_blank rel=noopener href='/one-style.html'>yo</a>");
106
107            var popupTaskSource = new TaskCompletionSource<Page>();
108            Page.Popup += (_, e) => popupTaskSource.TrySetResult(e.PopupPage);
109
110            await Task.WhenAll(
111                popupTaskSource.Task,
112                Page.ClickAsync("a"));
113
114            Assert.False(await Page.EvaluateExpressionAsync<bool>("!!window.opener"));
115            Assert.False(await popupTaskSource.Task.Result.EvaluateExpressionAsync<bool>("!!window.opener"));
116        }
117    }
118}
119
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 ShouldWork code on LambdaTest Cloud Grid

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