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

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

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

FrameWaitForXPathTests.cs

Source: FrameWaitForXPathTests.cs Github

copy
1using System;
2using System.Linq;
3using System.Threading.Tasks;
4using Xunit;
5using Xunit.Abstractions;
6using PuppeteerSharp.Helpers;
7using PuppeteerSharp.Xunit;
8using PuppeteerSharp.Tests.Attributes;
9
10namespace PuppeteerSharp.Tests.WaitTaskTests
11{
12    [Collection(TestConstants.TestFixtureCollectionName)]
13    public class FrameWaitForXPathTests : PuppeteerPageBaseTest
14    {
15        const string addElement = "tag => document.body.appendChild(document.createElement(tag))";
16
17        public FrameWaitForXPathTests(ITestOutputHelper output) : base(output)
18        {
19        }
20
21        [PuppeteerTest("waittask.spec.ts", "Frame.waitForXPath", "should support some fancy xpath")]
22        [PuppeteerFact]
23        public async Task ShouldSupportSomeFancyXpath()
24        {
25            await Page.SetContentAsync("<p>red herring</p><p>hello  world  </p>");
26            var waitForXPath = Page.WaitForXPathAsync("//p[normalize-space(.)=\"hello world\"]");
27            Assert.Equal("hello  world  ", await Page.EvaluateFunctionAsync<string>("x => x.textContent", await waitForXPath));
28        }
29
30        [PuppeteerTest("waittask.spec.ts", "Frame.waitForXPath", "should run in specified frame")]
31        [PuppeteerFact]
32        public async Task ShouldRunInSpecifiedFrame()
33        {
34            await FrameUtils.AttachFrameAsync(Page, "frame1", TestConstants.EmptyPage);
35            await FrameUtils.AttachFrameAsync(Page, "frame2", TestConstants.EmptyPage);
36            var frame1 = Page.Frames.First(f => f.Name == "frame1");
37            var frame2 = Page.Frames.First(f => f.Name == "frame2");
38            var waitForXPathPromise = frame2.WaitForXPathAsync("//div");
39            await frame1.EvaluateFunctionAsync(addElement, "div");
40            await frame2.EvaluateFunctionAsync(addElement, "div");
41            var eHandle = await waitForXPathPromise;
42            Assert.Equal(frame2, eHandle.ExecutionContext.Frame);
43        }
44
45        [PuppeteerTest("waittask.spec.ts", "Frame.waitForXPath", "should throw when frame is detached")]
46        [SkipBrowserFact(skipFirefox: true)]
47        public async Task ShouldThrowWhenFrameIsDetached()
48        {
49            await FrameUtils.AttachFrameAsync(Page, "frame1", TestConstants.EmptyPage);
50            var frame = Page.FirstChildFrame();
51            var waitPromise = frame.WaitForXPathAsync("//*[@class=\"box\"]");
52            await FrameUtils.DetachFrameAsync(Page, "frame1");
53            var exception = await Assert.ThrowsAnyAsync<Exception>(() => waitPromise);
54            Assert.Contains("waitForFunction failed: frame got detached.", exception.Message);
55        }
56
57        [PuppeteerTest("waittask.spec.ts", "Frame.waitForXPath", "hidden should wait for display: none")]
58        [PuppeteerFact]
59        public async Task HiddenShouldWaitForDisplayNone()
60        {
61            var divHidden = false;
62            await Page.SetContentAsync("<div style='display: block;'></div>");
63            var waitForXPath = Page.WaitForXPathAsync("//div", new WaitForSelectorOptions { Hidden = true })
64                .ContinueWith(_ => divHidden = true);
65            await Page.WaitForXPathAsync("//div"); // do a round trip
66            Assert.False(divHidden);
67            await Page.EvaluateExpressionAsync("document.querySelector('div').style.setProperty('display', 'none')");
68            Assert.True(await waitForXPath.WithTimeout());
69            Assert.True(divHidden);
70        }
71
72        [PuppeteerTest("waittask.spec.ts", "Frame.waitForXPath", "should return the element handle")]
73        [PuppeteerFact]
74        public async Task ShouldReturnTheElementHandle()
75        {
76            var waitForXPath = Page.WaitForXPathAsync("//*[@class=\"zombo\"]");
77            await Page.SetContentAsync("<div class='zombo'>anything</div>");
78            Assert.Equal("anything", await Page.EvaluateFunctionAsync<string>("x => x.textContent", await waitForXPath));
79        }
80
81        [PuppeteerTest("waittask.spec.ts", "Frame.waitForXPath", "should allow you to select a text node")]
82        [PuppeteerFact]
83        public async Task ShouldAllowYouToSelectATextNode()
84        {
85            await Page.SetContentAsync("<div>some text</div>");
86            var text = await Page.WaitForXPathAsync("//div/text()");
87            Assert.Equal(3 /* Node.TEXT_NODE */, await (await text.GetPropertyAsync("nodeType")).JsonValueAsync<int>());
88        }
89
90        [PuppeteerTest("waittask.spec.ts", "Frame.waitForXPath", "should allow you to select an element with single slash")]
91        [PuppeteerFact]
92        public async Task ShouldAllowYouToSelectAnElementWithSingleSlash()
93        {
94            await Page.SetContentAsync("<div>some text</div>");
95            var waitForXPath = Page.WaitForXPathAsync("/html/body/div");
96            Assert.Equal("some text", await Page.EvaluateFunctionAsync<string>("x => x.textContent", await waitForXPath));
97        }
98
99        [PuppeteerTest("waittask.spec.ts", "Frame.waitForXPath", "should respect timeout")]
100        [PuppeteerFact]
101        public async Task ShouldRespectTimeout()
102        {
103            var exception = await Assert.ThrowsAsync<WaitTaskTimeoutException>(()
104                    => Page.WaitForXPathAsync("//div", new WaitForSelectorOptions { Timeout = 10 }));
105
106            Assert.Contains("waiting for XPath '//div' failed: timeout", exception.Message);
107        }
108    }
109}
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)