How to use ShouldSupportStarCaptureWhenMultiplePathsMatch method of Microsoft.Playwright.Tests.EvalOnSelectorAllTests class

Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.EvalOnSelectorAllTests.ShouldSupportStarCaptureWhenMultiplePathsMatch

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

EvalOnSelectorAllTests.cs

Source: EvalOnSelectorAllTests.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.Threading.Tasks;
26using Microsoft.Playwright.NUnit;
27using NUnit.Framework;
28
29namespace Microsoft.Playwright.Tests
30{
31    public class EvalOnSelectorAllTests : PageTestEx
32    {
33        [PlaywrightTest("eval-on-selector-all.spec.ts", "should work with css selector")]
34        public async Task ShouldWorkWithCssSelector()
35        {
36            await Page.SetContentAsync("<div>hello</div><div>beautiful</div><div>world!</div>");
37            int divsCount = await Page.EvalOnSelectorAllAsync<int>("css=div", "divs => divs.length");
38            Assert.AreEqual(3, divsCount);
39        }
40
41        [PlaywrightTest("eval-on-selector-all.spec.ts", "should work with text selector")]
42        public async Task ShouldWorkWithTextSelector()
43        {
44            await Page.SetContentAsync("<div>hello</div><div>beautiful</div><div>beautiful</div><div>world!</div>");
45            int divsCount = await Page.EvalOnSelectorAllAsync<int>("text=beautiful", "divs => divs.length");
46            Assert.AreEqual(2, divsCount);
47        }
48
49        [PlaywrightTest("eval-on-selector-all.spec.ts", "should work with xpath selector")]
50        public async Task ShouldWorkWithXpathSelector()
51        {
52            await Page.SetContentAsync("<div>hello</div><div>beautiful</div><div>world!</div>");
53            int divsCount = await Page.EvalOnSelectorAllAsync<int>("xpath=/html/body/div", "divs => divs.length");
54            Assert.AreEqual(3, divsCount);
55        }
56
57        [PlaywrightTest("eval-on-selector-all.spec.ts", "should auto-detect css selector")]
58        public async Task ShouldAutoDetectCssSelector()
59        {
60            await Page.SetContentAsync("<div>hello</div><div>beautiful</div><div>world!</div>");
61            int divsCount = await Page.EvalOnSelectorAllAsync<int>("div", "divs => divs.length");
62            Assert.AreEqual(3, divsCount);
63        }
64
65        [PlaywrightTest("eval-on-selector-all.spec.ts", "should support >> syntax")]
66        public async Task ShouldSupportDoubleGreaterThanSyntax()
67        {
68            await Page.SetContentAsync("<div><span>hello</span></div><div>beautiful</div><div><span>wo</span><span>rld!</span></div><span>Not this one</span>");
69            int spansCount = await Page.EvalOnSelectorAllAsync<int>("css=div >> css=span", "spans => spans.length");
70            Assert.AreEqual(3, spansCount);
71        }
72
73        [PlaywrightTest("eval-on-selector-all.spec.ts", "should should support * capture")]
74        public async Task ShouldSupportStarCapture()
75        {
76            await Page.SetContentAsync("<section><div><span>a</span></div></section><section><div><span>b</span></div></section>");
77            Assert.AreEqual(1, await Page.EvalOnSelectorAllAsync<int>("*css=div >> \"b\"", "divs => divs.length"));
78            Assert.AreEqual(1, await Page.EvalOnSelectorAllAsync<int>("section >> *css=div >> \"b\"", "divs => divs.length"));
79            Assert.AreEqual(4, await Page.EvalOnSelectorAllAsync<int>("section >> *", "divs => divs.length"));
80
81            await Page.SetContentAsync("<section><div><span>a</span><span>a</span></div></section>");
82            Assert.AreEqual(1, await Page.EvalOnSelectorAllAsync<int>("*css=div >> \"a\"", "divs => divs.length"));
83            Assert.AreEqual(1, await Page.EvalOnSelectorAllAsync<int>("section >> *css=div >> \"a\"", "divs => divs.length"));
84
85            await Page.SetContentAsync("<div><span>a</span></div><div><span>a</span></div><section><div><span>a</span></div></section>");
86            Assert.AreEqual(3, await Page.EvalOnSelectorAllAsync<int>("*css=div >> \"a\"", "divs => divs.length"));
87            Assert.AreEqual(1, await Page.EvalOnSelectorAllAsync<int>("section >> *css=div >> \"a\"", "divs => divs.length"));
88        }
89
90        [PlaywrightTest("eval-on-selector-all.spec.ts", "should support * capture when multiple paths match")]
91        public async Task ShouldSupportStarCaptureWhenMultiplePathsMatch()
92        {
93            await Page.SetContentAsync("<div><div><span></span></div></div><div></div>");
94            Assert.AreEqual(2, await Page.EvalOnSelectorAllAsync<int>("*css=div >> span", "els => els.length"));
95
96            await Page.SetContentAsync("<div><div><span></span></div><span></span><span></span></div><div></div>");
97            Assert.AreEqual(2, await Page.EvalOnSelectorAllAsync<int>("*css=div >> span", "els => els.length"));
98        }
99    }
100}
101
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 ShouldSupportStarCaptureWhenMultiplePathsMatch code on LambdaTest Cloud Grid

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

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)