How to use ShouldAutoDetectXpathSelector method of Microsoft.Playwright.Tests.QuerySelectorTests class

Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.QuerySelectorTests.ShouldAutoDetectXpathSelector

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

QuerySelectorTests.cs

Source: QuerySelectorTests.cs Github

copy
1/*
2 * MIT License
3 *
4 * Copyright (c) Microsoft Corporation.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25using System.Linq;
26using System.Threading.Tasks;
27using Microsoft.Playwright.NUnit;
28using NUnit.Framework;
29
30namespace Microsoft.Playwright.Tests
31{
32    public class QuerySelectorTests : PageTestEx
33    {
34        [PlaywrightTest("queryselector.spec.ts", "should query existing elements")]
35        public async Task ShouldQueryExistingElements()
36        {
37            await Page.SetContentAsync("<div>A</div><br/><div>B</div>");
38            var elements = await Page.QuerySelectorAllAsync("div");
39            Assert.AreEqual(2, elements.Count());
40            var tasks = elements.Select(element => Page.EvaluateAsync<string>("e => e.textContent", element));
41            Assert.AreEqual(new[] { "A", "B" }, await TaskUtils.WhenAll(tasks));
42        }
43
44        [PlaywrightTest("queryselector.spec.ts", "should return empty array if nothing is found")]
45        public async Task ShouldReturnEmptyArrayIfNothingIsFound()
46        {
47            await Page.GotoAsync(Server.EmptyPage);
48            var elements = await Page.QuerySelectorAllAsync("div");
49            Assert.IsEmpty(elements);
50        }
51
52        [PlaywrightTest("queryselector.spec.ts", "xpath should query existing element")]
53        public async Task XpathShouldQueryExistingElement()
54        {
55            await Page.SetContentAsync("<section>test</section>");
56            var elements = await Page.QuerySelectorAllAsync("xpath=/html/body/section");
57            Assert.NotNull(elements.FirstOrDefault());
58            Assert.That(elements, Has.Count.EqualTo(1));
59        }
60
61        [PlaywrightTest("queryselector.spec.ts", "should return empty array for non-existing element")]
62        public async Task ShouldReturnEmptyArrayForNonExistingElement()
63        {
64            var elements = await Page.QuerySelectorAllAsync("//html/body/non-existing-element");
65            Assert.IsEmpty(elements);
66        }
67
68        [PlaywrightTest("queryselector.spec.ts", "should return multiple elements")]
69        public async Task ShouldReturnMultipleElements()
70        {
71            await Page.SetContentAsync("<div></div><div></div>");
72            var elements = await Page.QuerySelectorAllAsync("xpath=/html/body/div");
73            Assert.AreEqual(2, elements.Count());
74        }
75
76        [PlaywrightTest("queryselector.spec.ts", "should query existing element with css selector")]
77        public async Task ShouldQueryExistingElementWithCssSelector()
78        {
79            await Page.SetContentAsync("<section>test</section>");
80            var element = await Page.QuerySelectorAsync("css=section");
81            Assert.NotNull(element);
82        }
83
84        [PlaywrightTest("queryselector.spec.ts", "should query existing element with text selector")]
85        public async Task ShouldQueryExistingElementWithTextSelector()
86        {
87            await Page.SetContentAsync("<section>test</section>");
88            var element = await Page.QuerySelectorAsync("text=\"test\"");
89            Assert.NotNull(element);
90        }
91
92        [PlaywrightTest("queryselector.spec.ts", "should query existing element with xpath selector")]
93        public async Task ShouldQueryExistingElementWithXpathSelector()
94        {
95            await Page.SetContentAsync("<section>test</section>");
96            var element = await Page.QuerySelectorAsync("xpath=/html/body/section");
97            Assert.NotNull(element);
98        }
99
100        [PlaywrightTest("queryselector.spec.ts", "should return null for non-existing element")]
101        public async Task ShouldReturnNullForNonExistingElement()
102        {
103            var element = await Page.QuerySelectorAsync("non-existing-element");
104            Assert.Null(element);
105        }
106
107        [PlaywrightTest("queryselector.spec.ts", "should auto-detect xpath selector")]
108        public async Task ShouldAutoDetectXpathSelector()
109        {
110            await Page.SetContentAsync("<section>test</section>");
111            var element = await Page.QuerySelectorAsync("//html/body/section");
112            Assert.NotNull(element);
113        }
114
115        [PlaywrightTest("queryselector.spec.ts", "should auto-detect xpath selector with starting parenthesis")]
116        public async Task ShouldAutoDetectXpathSelectorWithStartingParenthesis()
117        {
118            await Page.SetContentAsync("<section>test</section>");
119            var element = await Page.QuerySelectorAsync("(//section)[1]");
120            Assert.NotNull(element);
121        }
122
123        [PlaywrightTest("queryselector.spec.ts", "should auto-detect text selector")]
124        public async Task ShouldAutoDetectTextSelector()
125        {
126            await Page.SetContentAsync("<section>test</section>");
127            var element = await Page.QuerySelectorAsync("\"test\"");
128            Assert.NotNull(element);
129        }
130
131        [PlaywrightTest("queryselector.spec.ts", "should auto-detect css selector")]
132        public async Task ShouldAutoDetectCssSelector()
133        {
134            await Page.SetContentAsync("<section>test</section>");
135            var element = await Page.QuerySelectorAsync("section");
136            Assert.NotNull(element);
137        }
138
139        [PlaywrightTest("queryselector.spec.ts", "should support >> syntax")]
140        public async Task ShouldSupportDoubleGreaterThanSyntax()
141        {
142            await Page.SetContentAsync("<section><div>test</div></section>");
143            var element = await Page.QuerySelectorAsync("css=section >> css=div");
144            Assert.NotNull(element);
145        }
146    }
147}
148
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)