How to use PageWaitForTests class of PuppeteerSharp.Tests.WaitForTests package

Best Puppeteer-sharp code snippet using PuppeteerSharp.Tests.WaitForTests.PageWaitForTests

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

PageWaitForTests.cs

Source: PageWaitForTests.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.WaitForTests
9{
10    [Collection(TestConstants.TestFixtureCollectionName)]
11    public class PageWaitForTests : PuppeteerPageBaseTest
12    {
13        public PageWaitForTests(ITestOutputHelper output) : base(output)
14        {
15        }
16
17        [PuppeteerTest("waittask.spec.ts", "Page.waitFor", "should wait for selector")]
18        [PuppeteerFact]
19        public async Task ShouldWaitForSelector()
20        {
21            var found = false;
22            var waitFor = Page.WaitForSelectorAsync("div").ContinueWith(_ => found = true);
23            await Page.GoToAsync(TestConstants.EmptyPage);
24
25            Assert.False(found);
26
27            await Page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
28            await waitFor;
29            Assert.True(found);
30        }
31
32        [PuppeteerTest("waittask.spec.ts", "Page.waitFor", "should wait for an xpath")]
33        [PuppeteerFact]
34        public async Task ShouldWaitForAnXpath()
35        {
36            var found = false;
37            var waitFor = Page.WaitForXPathAsync("//div").ContinueWith(_ => found = true);
38            await Page.GoToAsync(TestConstants.EmptyPage);
39            Assert.False(found);
40            await Page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
41            await waitFor;
42            Assert.True(found);
43        }
44
45        [PuppeteerTest("waittask.spec.ts", "Page.waitFor", "should not allow you to select an element with single slash xpath")]
46        [PuppeteerFact]
47        public async Task ShouldNotAllowYouToSelectAnElementWithSingleSlashXpath()
48        {
49            await Page.SetContentAsync("<div>some text</div>");
50            var exception = await Assert.ThrowsAsync<EvaluationFailedException>(() =>
51                Page.WaitForSelectorAsync("/html/body/div"));
52            Assert.NotNull(exception);
53        }
54
55        [PuppeteerTest("waittask.spec.ts", "Page.waitFor", "should timeout")]
56        [PuppeteerFact]
57        public async Task ShouldTimeout()
58        {
59            var startTime = DateTime.Now;
60            var timeout = 42;
61            await Page.WaitForTimeoutAsync(timeout);
62            Assert.True((DateTime.Now - startTime).TotalMilliseconds > timeout / 2);
63        }
64
65        [PuppeteerTest("waittask.spec.ts", "Page.waitFor", "should work with multiline body")]
66        [PuppeteerFact]
67        public async Task ShouldWorkWithMultilineBody()
68        {
69            var result = await Page.WaitForExpressionAsync(@"
70                (() => true)()
71            ");
72            Assert.True(await result.JsonValueAsync<bool>());
73        }
74
75        [PuppeteerTest("waittask.spec.ts", "Page.waitFor", "should wait for predicate")]
76        [PuppeteerFact]
77        public Task ShouldWaitForPredicate()
78            => Task.WhenAll(
79                Page.WaitForFunctionAsync("() => window.innerWidth < 100"),
80                Page.SetViewportAsync(new ViewPortOptions { Width = 10, Height = 10 }));
81
82        [PuppeteerTest("waittask.spec.ts", "Page.waitFor", "should wait for predicate with arguments")]
83        [PuppeteerFact]
84        public async Task ShouldWaitForPredicateWithArguments()
85            => await Page.WaitForFunctionAsync("(arg1, arg2) => arg1 !== arg2", new WaitForFunctionOptions(), 1, 2);
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

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

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)