How to use ElementHandleXPathTests method of PuppeteerSharp.Tests.ElementHandleTests.ElementHandleXPathTests class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Tests.ElementHandleTests.ElementHandleXPathTests.ElementHandleXPathTests

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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


Source: ElementHandleXPathTests.cs Github

1using System.Threading.Tasks;
2using PuppeteerSharp.Tests.Attributes;
3using PuppeteerSharp.Xunit;
4using Xunit;
5using Xunit.Abstractions;
7namespace PuppeteerSharp.Tests.ElementHandleTests
9    [Collection(TestConstants.TestFixtureCollectionName)]
10    public class ElementHandleXPathTests : PuppeteerPageBaseTest
11    {
12        public ElementHandleXPathTests(ITestOutputHelper output) : base(output)
13        {
14        }
16        [PuppeteerTest("queryselector.spec.ts", "ElementHandle.$x", "should query existing element")]
17        [PuppeteerFact]
18        public async Task ShouldQueryExistingElement()
19        {
20            await Page.GoToAsync(TestConstants.ServerUrl + "/playground.html");
21            await Page.SetContentAsync("<html><body><div class=\"second\"><div class=\"inner\">A</div></div></body></html>");
22            var html = await Page.QuerySelectorAsync("html");
23            var second = await html.XPathAsync("./body/div[contains(@class, 'second')]");
24            var inner = await second[0].XPathAsync("./div[contains(@class, 'inner')]");
25            var content = await Page.EvaluateFunctionAsync<string>("e => e.textContent", inner[0]);
26            Assert.Equal("A", content);
27        }
29        [PuppeteerTest("queryselector.spec.ts", "ElementHandle.$x", "should return null for non-existing element")]
30        [PuppeteerFact]
31        public async Task ShouldReturnNullForNonExistingElement()
32        {
33            await Page.SetContentAsync("<html><body><div class=\"second\"><div class=\"inner\">B</div></div></body></html>");
34            var html = await Page.QuerySelectorAsync("html");
35            var second = await html.XPathAsync("/div[contains(@class, 'third')]");
36            Assert.Empty(second);
37        }
38    }
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 ElementHandleXPathTests code on LambdaTest Cloud Grid

Execute automation tests with ElementHandleXPathTests on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free

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

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)