How to use FrameWaitForFunctionTests class of PuppeteerSharp.Tests.WaitTaskTests package

Best Puppeteer-sharp code snippet using PuppeteerSharp.Tests.WaitTaskTests.FrameWaitForFunctionTests

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

FrameWaitForFunctionTests.cs

Source: FrameWaitForFunctionTests.cs Github

copy
1using System;
2using System.Threading.Tasks;
3using PuppeteerSharp.Tests.Attributes;
4using PuppeteerSharp.Xunit;
5using Xunit;
6using Xunit.Abstractions;
7
8namespace PuppeteerSharp.Tests.WaitTaskTests
9{
10    [Collection(TestConstants.TestFixtureCollectionName)]
11    public class FrameWaitForFunctionTests : PuppeteerPageBaseTest
12    {
13        public FrameWaitForFunctionTests(ITestOutputHelper output) : base(output)
14        {
15        }
16
17        [PuppeteerTest("waittask.spec.ts", "Frame.waitForFunction", "should work when resolved right before execution context disposal")]
18        [PuppeteerFact]
19        public async Task ShouldWorkWhenResolvedRightBeforeExecutionContextDisposal()
20        {
21            await Page.EvaluateFunctionOnNewDocumentAsync("() => window.__RELOADED = true");
22            await Page.WaitForFunctionAsync(@"() =>
23            {
24                if (!window.__RELOADED)
25                    window.location.reload();
26                return true;
27            }");
28        }
29
30        [PuppeteerTest("waittask.spec.ts", "Frame.waitForFunction", "should poll on interval")]
31        [PuppeteerFact]
32        public async Task ShouldPollOnInterval()
33        {
34            var success = false;
35            var startTime = DateTime.Now;
36            var polling = 100;
37            var watchdog = Page.WaitForFunctionAsync("() => window.__FOO === 'hit'", new WaitForFunctionOptions { PollingInterval = polling })
38                .ContinueWith(_ => success = true);
39            await Page.EvaluateExpressionAsync("window.__FOO = 'hit'");
40            Assert.False(success);
41            await Page.EvaluateExpressionAsync("document.body.appendChild(document.createElement('div'))");
42            await watchdog;
43            Assert.True((DateTime.Now - startTime).TotalMilliseconds > polling / 2);
44        }
45        
46        [PuppeteerTest("waittask.spec.ts", "Frame.waitForFunction", "should poll on interval async")]
47        [PuppeteerFact]
48        public async Task ShouldPollOnIntervalAsync()
49        {
50            var success = false;
51            var startTime = DateTime.Now;
52            var polling = 100;
53            var watchdog = Page.WaitForFunctionAsync("async () => window.__FOO === 'hit'", new WaitForFunctionOptions { PollingInterval = polling })
54                .ContinueWith(_ => success = true);
55            await Page.EvaluateFunctionAsync("async () => window.__FOO = 'hit'");
56            Assert.False(success);
57            await Page.EvaluateExpressionAsync("document.body.appendChild(document.createElement('div'))");
58            await watchdog;
59            Assert.True((DateTime.Now - startTime).TotalMilliseconds > polling / 2);
60        }
61
62        [PuppeteerTest("waittask.spec.ts", "Frame.waitForFunction", "should poll on mutation")]
63        [PuppeteerFact]
64        public async Task ShouldPollOnMutation()
65        {
66            var success = false;
67            var watchdog = Page.WaitForFunctionAsync("() => window.__FOO === 'hit'",
68                new WaitForFunctionOptions { Polling = WaitForFunctionPollingOption.Mutation })
69                .ContinueWith(_ => success = true);
70            await Page.EvaluateExpressionAsync("window.__FOO = 'hit'");
71            Assert.False(success);
72            await Page.EvaluateExpressionAsync("document.body.appendChild(document.createElement('div'))");
73            await watchdog;
74        }
75
76        [PuppeteerTest("waittask.spec.ts", "Frame.waitForFunction", "should poll on mutation async")]
77        [PuppeteerFact]
78        public async Task ShouldPollOnMutationAsync()
79        {
80            var success = false;
81            var watchdog = Page.WaitForFunctionAsync("async () => window.__FOO === 'hit'",
82                new WaitForFunctionOptions { Polling = WaitForFunctionPollingOption.Mutation })
83                .ContinueWith(_ => success = true);
84            await Page.EvaluateFunctionAsync("async () => window.__FOO = 'hit'");
85            Assert.False(success);
86            await Page.EvaluateExpressionAsync("document.body.appendChild(document.createElement('div'))");
87            await watchdog;
88        }
89
90        [PuppeteerTest("waittask.spec.ts", "Frame.waitForFunction", "should poll on raf")]
91        [PuppeteerFact]
92        public async Task ShouldPollOnRaf()
93        {
94            var watchdog = Page.WaitForFunctionAsync("() => window.__FOO === 'hit'",
95                new WaitForFunctionOptions { Polling = WaitForFunctionPollingOption.Raf });
96            await Page.EvaluateExpressionAsync("window.__FOO = 'hit'");
97            await watchdog;
98        }
99
100        [PuppeteerTest("waittask.spec.ts", "Frame.waitForFunction", "should poll on raf async")]
101        [PuppeteerFact]
102        public async Task ShouldPollOnRafAsync()
103        {
104            var watchdog = Page.WaitForFunctionAsync("async () => window.__FOO === 'hit'",
105                new WaitForFunctionOptions { Polling = WaitForFunctionPollingOption.Raf });
106            await Page.EvaluateFunctionAsync("async () => (globalThis.__FOO = 'hit')");
107            await watchdog;
108        }
109
110        [PuppeteerTest("waittask.spec.ts", "Frame.waitForFunction", "should work with strict CSP policy")]
111        [SkipBrowserFact(skipFirefox: true)]
112        public async Task ShouldWorkWithStrictCSPPolicy()
113        {
114            Server.SetCSP("/empty.html", "script-src " + TestConstants.ServerUrl);
115            await Page.GoToAsync(TestConstants.EmptyPage);
116            await Task.WhenAll(
117                Page.WaitForFunctionAsync("() => window.__FOO === 'hit'", new WaitForFunctionOptions
118                {
119                    Polling = WaitForFunctionPollingOption.Raf
120                }),
121                Page.EvaluateExpressionAsync("window.__FOO = 'hit'"));
122        }
123
124        [PuppeteerTest("waittask.spec.ts", "Frame.waitForFunction", "should throw negative polling interval")]
125        [PuppeteerFact]
126        public async Task ShouldThrowNegativePollingInterval()
127        {
128            var exception = await Assert.ThrowsAsync<ArgumentOutOfRangeException>(()
129                => Page.WaitForFunctionAsync("() => !!document.body", new WaitForFunctionOptions { PollingInterval = -10 }));
130
131            Assert.Contains("Cannot poll with non-positive interval", exception.Message);
132        }
133
134        [PuppeteerTest("waittask.spec.ts", "Frame.waitForFunction", "should return the success value as a JSHandle")]
135        [PuppeteerFact]
136        public async Task ShouldReturnTheSuccessValueAsAJSHandle()
137            => Assert.Equal(5, await (await Page.WaitForFunctionAsync("() => 5")).JsonValueAsync<int>());
138
139        [PuppeteerTest("waittask.spec.ts", "Frame.waitForFunction", "should return the window as a success value")]
140        [PuppeteerFact]
141        public async Task ShouldReturnTheWindowAsASuccessValue()
142            => Assert.NotNull(await Page.WaitForFunctionAsync("() => window"));
143
144        [PuppeteerTest("waittask.spec.ts", "Frame.waitForFunction", "should accept ElementHandle arguments")]
145        [PuppeteerFact]
146        public async Task ShouldAcceptElementHandleArguments()
147        {
148            await Page.SetContentAsync("<div></div>");
149            var div = await Page.QuerySelectorAsync("div");
150            var resolved = false;
151            var waitForFunction = Page.WaitForFunctionAsync("element => !element.parentElement", div)
152                .ContinueWith(_ => resolved = true);
153            Assert.False(resolved);
154            await Page.EvaluateFunctionAsync("element => element.remove()", div);
155            await waitForFunction;
156        }
157
158        [PuppeteerTest("waittask.spec.ts", "Frame.waitForFunction", "should respect timeout")]
159        [PuppeteerFact]
160        public async Task ShouldRespectTimeout()
161        {
162            var exception = await Assert.ThrowsAsync<WaitTaskTimeoutException>(()
163                => Page.WaitForExpressionAsync("false", new WaitForFunctionOptions { Timeout = 10 }));
164
165            Assert.Contains("waiting for function failed: timeout", exception.Message);
166        }
167
168        [PuppeteerTest("waittask.spec.ts", "Frame.waitForFunction", "should respect default timeout")]
169        [PuppeteerFact]
170        public async Task ShouldRespectDefaultTimeout()
171        {
172            Page.DefaultTimeout = 1;
173            var exception = await Assert.ThrowsAsync<WaitTaskTimeoutException>(()
174                => Page.WaitForExpressionAsync("false"));
175
176            Assert.Contains("waiting for function failed: timeout", exception.Message);
177        }
178
179        [PuppeteerTest("waittask.spec.ts", "Frame.waitForFunction", "should disable timeout when its set to 0")]
180        [PuppeteerFact]
181        public async Task ShouldDisableTimeoutWhenItsSetTo0()
182        {
183            var watchdog = Page.WaitForFunctionAsync(@"() => {
184                window.__counter = (window.__counter || 0) + 1;
185                return window.__injected;
186            }", new WaitForFunctionOptions { Timeout = 0, PollingInterval = 10 });
187            await Page.WaitForFunctionAsync("() => window.__counter > 10");
188            await Page.EvaluateExpressionAsync("window.__injected = true");
189            await watchdog;
190        }
191
192        [PuppeteerTest("waittask.spec.ts", "Frame.waitForFunction", "should survive cross-process navigation")]
193        [PuppeteerFact]
194        public async Task ShouldSurviveCrossProcessNavigation()
195        {
196            var fooFound = false;
197            var waitForFunction = Page.WaitForExpressionAsync("window.__FOO === 1")
198                .ContinueWith(_ => fooFound = true);
199            await Page.GoToAsync(TestConstants.EmptyPage);
200            Assert.False(fooFound);
201            await Page.ReloadAsync();
202            Assert.False(fooFound);
203            await Page.GoToAsync(TestConstants.CrossProcessUrl + "/grid.html");
204            Assert.False(fooFound);
205            await Page.EvaluateExpressionAsync("window.__FOO = 1");
206            await waitForFunction;
207            Assert.True(fooFound);
208        }
209
210        [PuppeteerTest("waittask.spec.ts", "Frame.waitForFunction", "should survive navigations")]
211        [PuppeteerFact]
212        public async Task ShouldSurviveNavigations()
213        {
214            var watchdog = Page.WaitForFunctionAsync("() => window.__done");
215            await Page.GoToAsync(TestConstants.EmptyPage);
216            await Page.GoToAsync(TestConstants.ServerUrl + "/consolelog.html");
217            await Page.EvaluateFunctionAsync("() => window.__done = true");
218            await watchdog;
219        }
220    }
221}
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)