How to use LocatorQueryTests class of Microsoft.Playwright.Tests.Locator package

Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.Locator.LocatorQueryTests

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

LocatorQueryTests.cs

Source: LocatorQueryTests.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;
26using System.Text.RegularExpressions;
27using System.Threading.Tasks;
28using NUnit.Framework;
29
30namespace Microsoft.Playwright.Tests.Locator
31{
32    public class LocatorQueryTests : PageTestEx
33    {
34        [PlaywrightTest("locator-query.spec.ts", "should respect first() and last()")]
35        public async Task ShouldRespectFirstAndLast()
36        {
37            await Page.SetContentAsync(@"
38                <section>
39                    <div><p>A</p></div>
40                    <div><p>A</p><p>A</p></div>
41                    <div><p>A</p><p>A</p><p>A</p></div>
42                </section>");
43            Assert.AreEqual(6, await Page.Locator("div >> p").CountAsync());
44            Assert.AreEqual(6, await Page.Locator("div").Locator("p").CountAsync());
45            Assert.AreEqual(1, await Page.Locator("div").First.Locator("p").CountAsync());
46            Assert.AreEqual(3, await Page.Locator("div").Last.Locator("p").CountAsync());
47        }
48
49        [PlaywrightTest("locator-query.spec.ts", "should respect nth()")]
50        public async Task ShouldRespectNth()
51        {
52            await Page.SetContentAsync(@"
53                <section>
54                    <div><p>A</p></div>
55                    <div><p>A</p><p>A</p></div>
56                    <div><p>A</p><p>A</p><p>A</p></div>
57                </section>");
58            Assert.AreEqual(1, await Page.Locator("div >> p").Nth(0).CountAsync());
59            Assert.AreEqual(2, await Page.Locator("div").Nth(1).Locator("p").CountAsync());
60            Assert.AreEqual(3, await Page.Locator("div").Nth(2).Locator("p").CountAsync());
61        }
62
63        [PlaywrightTest("locator-query.spec.ts", "should throw on capture w/ nth()")]
64        public async Task ShouldThrowOnCaptureWithNth()
65        {
66            await Page.SetContentAsync("<section><div><p>A</p></div></section>");
67            var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Page.Locator("*css=div >> p").Nth(1).ClickAsync());
68            StringAssert.Contains("Can't query n-th element", exception.Message);
69        }
70
71        [PlaywrightTest("locator-query.spec.ts", "should throw on due to strictness")]
72        public async Task ShouldThrowDueToStrictness()
73        {
74            await Page.SetContentAsync("<div>A</div><div>B</div>");
75            var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Page.Locator("div").IsVisibleAsync());
76            StringAssert.Contains("strict mode violation", exception.Message);
77        }
78
79        [PlaywrightTest("locator-query.spec.ts", "should throw on due to strictness 2")]
80        public async Task ShouldThrowDueToStrictness2()
81        {
82            await Page.SetContentAsync("<select><option>One</option><option>Two</option></select>");
83            var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Page.Locator("option").EvaluateAsync("() => {}"));
84            StringAssert.Contains("strict mode violation", exception.Message);
85        }
86
87        [PlaywrightTest("locator-query.spec.ts", "should filter by text")]
88        public async Task ShouldFilterByText()
89        {
90            await Page.SetContentAsync("<div>Foobar</div><div>Bar</div>");
91            StringAssert.Contains(await Page.Locator("div", new() { HasTextString = "Foo" }).TextContentAsync(), "Foobar");
92        }
93
94        [PlaywrightTest("locator-query.spec.ts", "should filter by text 2")]
95        public async Task ShouldFilterByText2()
96        {
97            await Page.SetContentAsync("<div>foo <span>hello world</span> bar</div>");
98            StringAssert.Contains(await Page.Locator("div", new() { HasTextString = "hello world" }).TextContentAsync(), "foo hello world bar");
99        }
100
101        [PlaywrightTest("locator-query.spec.ts", "should filter by regex")]
102        public async Task ShouldFilterByRegex()
103        {
104            await Page.SetContentAsync("<div>Foobar</div><div>Bar</div>");
105            StringAssert.Contains(await Page.Locator("div", new() { HasTextRegex = new Regex("Foo.*") }).InnerTextAsync(), "Foobar");
106        }
107
108        [PlaywrightTest("locator-query.spec.ts", "should filter by text with quotes")]
109        public async Task ShouldFilterByTextWithQuotes()
110        {
111            await Page.SetContentAsync("<div>Hello \"world\"</div><div>Hello world</div>");
112            StringAssert.Contains(await Page.Locator("div", new() { HasTextString = "Hello \"world\"" }).InnerTextAsync(), "Hello \"world\"");
113        }
114
115        [PlaywrightTest("locator-query.spec.ts", "should filter by regex with quotes")]
116        public async Task ShouldFilterByRegexWithQuotes()
117        {
118            await Page.SetContentAsync("<div>Hello \"world\"</div><div>Hello world</div>");
119            StringAssert.Contains(await Page.Locator("div", new() { HasTextRegex = new Regex("Hello \"world\"") }).InnerTextAsync(), "Hello \"world\"");
120        }
121
122        [PlaywrightTest("locator-query.spec.ts", "should filter by regex and regexp flags")]
123        public async Task ShouldFilterByRegexandRegexpFlags()
124        {
125            await Page.SetContentAsync("<div>Hello \"world\"</div><div>Hello world</div>");
126            StringAssert.Contains(await Page.Locator("div", new() { HasTextRegex = new Regex("hElLo \"wOrld\"", RegexOptions.IgnoreCase) }).InnerTextAsync(), "Hello \"world\"");
127        }
128
129        [PlaywrightTest("locator-query.spec.ts", "should support has:locator")]
130        public async Task ShouldSupportHasLocator()
131        {
132            await Page.SetContentAsync("<div><span>hello</span></div><div><span>world</span></div>");
133            Assert.AreEqual(1, await Page.Locator("div", new() { Has = Page.Locator("text=world") }).CountAsync());
134            Assert.AreEqual("<div><span>world</span></div>", await Page.Locator("div", new() { Has = Page.Locator("text=world") }).EvaluateAsync<string>("e => e.outerHTML"));
135            Assert.AreEqual(1, await Page.Locator("div", new() { Has = Page.Locator("text=hello") }).CountAsync());
136            Assert.AreEqual("<div><span>hello</span></div>", await Page.Locator("div", new() { Has = Page.Locator("text=hello") }).EvaluateAsync<string>("e => e.outerHTML"));
137            Assert.AreEqual(2, await Page.Locator("div", new() { Has = Page.Locator("xpath=./span") }).CountAsync());
138            Assert.AreEqual(1, await Page.Locator("div", new() { Has = Page.Locator("span", new() { HasTextString = "wor" }) }).CountAsync());
139            Assert.AreEqual("<div><span>world</span></div>", await Page.Locator("div", new() { Has = Page.Locator("span", new() { HasTextString = "wor" }) }).EvaluateAsync<string>("e => e.outerHTML"));
140            Assert.AreEqual(1, await Page.Locator("div", new() { HasTextString = "wor", Has = Page.Locator("span") }).CountAsync());
141        }
142
143        [PlaywrightTest("locator-query.spec.ts", "should enforce same frame for has:locator'")]
144        public async Task ShouldEnforceSameFrameForHasLocator()
145        {
146            await Page.GotoAsync(Server.Prefix + "/frames/two-frames.html");
147            var child = Page.Frames[1];
148            var exception = await PlaywrightAssert.ThrowsAsync<ArgumentException>(() =>
149            {
150                Page.Locator("div", new() { Has = child.Locator("span") });
151                return Task.CompletedTask;
152            });
153            Assert.AreEqual(exception.Message, "Inner \"has\" locator must belong to the same frame.");
154        }
155    }
156}
157
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)