How to use ShouldThrowErrorIfNoElementIsFound method of Microsoft.Playwright.Tests.EvalOnSelectorTests class

Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.EvalOnSelectorTests.ShouldThrowErrorIfNoElementIsFound

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

EvalOnSelectorTests.cs

Source: EvalOnSelectorTests.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 EvalOnSelectorTests : PageTestEx
32    {
33        [PlaywrightTest("eval-on-selector.spec.ts", "should work with css selector")]
34        public async Task ShouldWorkWithCssSelector()
35        {
36            await Page.SetContentAsync("<section id=\"testAttribute\">43543</section>");
37            string idAttribute = await Page.EvalOnSelectorAsync<string>("css=section", "e => e.id");
38            Assert.AreEqual("testAttribute", idAttribute);
39        }
40
41        [PlaywrightTest("eval-on-selector.spec.ts", "should work with id selector")]
42        public async Task ShouldWorkWithIdSelector()
43        {
44            await Page.SetContentAsync("<section id=\"testAttribute\">43543</section>");
45            string idAttribute = await Page.EvalOnSelectorAsync<string>("id=testAttribute", "e => e.id");
46            Assert.AreEqual("testAttribute", idAttribute);
47        }
48
49        [PlaywrightTest("eval-on-selector.spec.ts", "should work with data-test selector")]
50        public async Task ShouldWorkWithDataTestSelector()
51        {
52            await Page.SetContentAsync("<section data-test=foo id=\"testAttribute\">43543</section>");
53            string idAttribute = await Page.EvalOnSelectorAsync<string>("data-test=foo", "e => e.id");
54            Assert.AreEqual("testAttribute", idAttribute);
55        }
56
57        [PlaywrightTest("eval-on-selector.spec.ts", "should work with data-testid selector")]
58        public async Task ShouldWorkWithDataTestidSelector()
59        {
60            await Page.SetContentAsync("<section data-testid=foo id=\"testAttribute\">43543</section>");
61            string idAttribute = await Page.EvalOnSelectorAsync<string>("data-testid=foo", "e => e.id");
62            Assert.AreEqual("testAttribute", idAttribute);
63        }
64
65        [PlaywrightTest("eval-on-selector.spec.ts", "should work with data-test-id selector")]
66        public async Task ShouldWorkWithDataTestIdSelector()
67        {
68            await Page.SetContentAsync("<section data-test-id=foo id=\"testAttribute\">43543</section>");
69            string idAttribute = await Page.EvalOnSelectorAsync<string>("data-test-id=foo", "e => e.id");
70            Assert.AreEqual("testAttribute", idAttribute);
71        }
72
73        [PlaywrightTest("eval-on-selector.spec.ts", "should work with text selector")]
74        public async Task ShouldWorkWithTextSelector()
75        {
76            await Page.SetContentAsync("<section id=\"testAttribute\">43543</section>");
77            string idAttribute = await Page.EvalOnSelectorAsync<string>("text=\"43543\"", "e => e.id");
78            Assert.AreEqual("testAttribute", idAttribute);
79        }
80
81        [PlaywrightTest("eval-on-selector.spec.ts", "should work with xpath selector")]
82        public async Task ShouldWorkWithXpathSelector()
83        {
84            await Page.SetContentAsync("<section id=\"testAttribute\">43543</section>");
85            string idAttribute = await Page.EvalOnSelectorAsync<string>("xpath=/html/body/section", "e => e.id");
86            Assert.AreEqual("testAttribute", idAttribute);
87        }
88
89        [PlaywrightTest("eval-on-selector.spec.ts", "should work with text selector")]
90        public async Task ShouldWorkWithTextSelector2()
91        {
92            await Page.SetContentAsync("<section id=\"testAttribute\">43543</section>");
93            string idAttribute = await Page.EvalOnSelectorAsync<string>("text=43543", "e => e.id");
94            Assert.AreEqual("testAttribute", idAttribute);
95        }
96
97        [PlaywrightTest("eval-on-selector.spec.ts", "should auto-detect css selector")]
98        public async Task ShouldAutoDetectCssSelector()
99        {
100            await Page.SetContentAsync("<section id=\"testAttribute\">43543</section>");
101            string idAttribute = await Page.EvalOnSelectorAsync<string>("section", "e => e.id");
102            Assert.AreEqual("testAttribute", idAttribute);
103        }
104
105        [PlaywrightTest("eval-on-selector.spec.ts", "should auto-detect nested selectors")]
106        public async Task ShouldAutoDetectNestedSelectors()
107        {
108            await Page.SetContentAsync("<div foo=bar><section>43543<span>Hello<div id=target></div></span></section></div>");
109            string idAttribute = await Page.EvalOnSelectorAsync<string>("div[foo=bar] > section >> \"Hello\" >> div", "e => e.id");
110            Assert.AreEqual("target", idAttribute);
111        }
112
113        [PlaywrightTest("eval-on-selector.spec.ts", "should auto-detect css selector with attributes")]
114        public async Task ShouldAutoDetectCssSelectorWithAttributes()
115        {
116            await Page.SetContentAsync("<section id=\"testAttribute\">43543</section>");
117            string idAttribute = await Page.EvalOnSelectorAsync<string>("section[id=\"testAttribute\"]", "e => e.id");
118            Assert.AreEqual("testAttribute", idAttribute);
119        }
120
121        [PlaywrightTest("eval-on-selector.spec.ts", "should accept arguments")]
122        public async Task ShouldAcceptArguments()
123        {
124            await Page.SetContentAsync("<section>hello</section>");
125            string text = await Page.EvalOnSelectorAsync<string>("section", "(e, suffix) => e.textContent + suffix", " world!");
126            Assert.AreEqual("hello world!", text);
127        }
128
129        [PlaywrightTest("eval-on-selector.spec.ts", "should accept ElementHandles as arguments")]
130        public async Task ShouldAcceptElementHandlesAsArguments()
131        {
132            await Page.SetContentAsync("<section>hello</section><div> world</div>");
133            var divHandle = await Page.QuerySelectorAsync("div");
134            string text = await Page.EvalOnSelectorAsync<string>("section", "(e, div) => e.textContent + div.textContent", divHandle);
135            Assert.AreEqual("hello world", text);
136        }
137
138        [PlaywrightTest("eval-on-selector.spec.ts", "should throw error if no element is found")]
139        public async Task ShouldThrowErrorIfNoElementIsFound()
140        {
141            var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(()
142                => Page.EvalOnSelectorAsync("section", "e => e.id"));
143            StringAssert.Contains("failed to find element matching selector \"section\"", exception.Message);
144        }
145
146        [PlaywrightTest("eval-on-selector.spec.ts", "should support >> syntax")]
147        public async Task ShouldSupportDoubleGreaterThanSyntax()
148        {
149            await Page.SetContentAsync("<section><div>hello</div></section>");
150            string text = await Page.EvalOnSelectorAsync<string>("css=section >> css=div", "(e, suffix) => e.textContent + suffix", " world!");
151            Assert.AreEqual("hello world!", text);
152        }
153
154        [PlaywrightTest("eval-on-selector.spec.ts", "should support >> syntax with different engines")]
155        public async Task ShouldSupportDoubleGreaterThanSyntaxWithDifferentEngines()
156        {
157            await Page.SetContentAsync("<section><div><span>hello</span></div></section>");
158            string text = await Page.EvalOnSelectorAsync<string>("xpath=/html/body/section >> css=div >> text=\"hello\"", "(e, suffix) => e.textContent + suffix", " world!");
159            Assert.AreEqual("hello world!", text);
160        }
161
162        [PlaywrightTest("eval-on-selector.spec.ts", "should support spaces with >> syntax")]
163        public async Task ShouldSupportSpacesWithDoubleGreaterThanSyntax()
164        {
165            await Page.GotoAsync(Server.Prefix + "/deep-shadow.html");
166            string text = await Page.EvalOnSelectorAsync<string>(" css = div >>css=div>>css   = span  ", "e => e.textContent");
167            Assert.AreEqual("Hello from root2", text);
168        }
169
170        [PlaywrightTest("eval-on-selector.spec.ts", "should not stop at first failure with >> syntax")]
171        public async Task ShouldNotStopAtFirstFailureWithDoubleGraterThanSyntax()
172        {
173            await Page.SetContentAsync("<div><span>Next</span><button>Previous</button><button>Next</button></div>");
174            string text = await Page.EvalOnSelectorAsync<string>("button >> \"Next\"", "(e) => e.outerHTML");
175            Assert.AreEqual("<button>Next</button>", text);
176        }
177
178        [PlaywrightTest("eval-on-selector.spec.ts", "should support * capture")]
179        public async Task ShouldSupportStarCapture()
180        {
181            await Page.SetContentAsync("<section><div><span>a</span></div></section><section><div><span>b</span></div></section>");
182            Assert.AreEqual("<div><span>b</span></div>", await Page.EvalOnSelectorAsync<string>("*css=div >> \"b\"", "(e) => e.outerHTML"));
183            Assert.AreEqual("<div><span>b</span></div>", await Page.EvalOnSelectorAsync<string>("section >> *css=div >> \"b\"", "(e) => e.outerHTML"));
184            Assert.AreEqual("<span>b</span>", await Page.EvalOnSelectorAsync<string>("css=div >> *text=\"b\"", "(e) => e.outerHTML"));
185            Assert.NotNull(await Page.QuerySelectorAsync("*"));
186        }
187
188        [PlaywrightTest("eval-on-selector.spec.ts", "should throw on multiple * captures")]
189        public async Task ShouldThrowOnMultipleStarCaptures()
190        {
191            var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Page.EvalOnSelectorAsync<string>("*css=div >> *css=span", "(e) => e.outerHTML"));
192            Assert.AreEqual("Only one of the selectors can capture using * modifier", exception.Message);
193        }
194
195        [PlaywrightTest("eval-on-selector.spec.ts", "should throw on malformed * capture")]
196        public async Task ShouldThrowOnMalformedStarCapture()
197        {
198            var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Page.EvalOnSelectorAsync<string>("*=div", "(e) => e.outerHTML"));
199            Assert.AreEqual("Unknown engine \"\" while parsing selector *=div", exception.Message);
200        }
201
202        [PlaywrightTest("eval-on-selector.spec.ts", "should work with spaces in css attributes")]
203        public async Task ShouldWorkWithSpacesInCssAttributes()
204        {
205            await Page.SetContentAsync("<div><input placeholder=\"Select date\"></div>");
206            Assert.NotNull(await Page.WaitForSelectorAsync("[placeholder = \"Select date\"]"));
207            Assert.NotNull(await Page.WaitForSelectorAsync("[placeholder = 'Select date']"));
208            Assert.NotNull(await Page.WaitForSelectorAsync("input[placeholder = \"Select date\"]"));
209            Assert.NotNull(await Page.WaitForSelectorAsync("input[placeholder = 'Select date']"));
210            Assert.NotNull(await Page.QuerySelectorAsync("[placeholder = \"Select date\"]"));
211            Assert.NotNull(await Page.QuerySelectorAsync("[placeholder = 'Select date']"));
212            Assert.NotNull(await Page.QuerySelectorAsync("input[placeholder = \"Select date\"]"));
213            Assert.NotNull(await Page.QuerySelectorAsync("input[placeholder = 'Select date']"));
214            Assert.AreEqual("<input placeholder=\"Select date\">", await Page.EvalOnSelectorAsync<string>("[placeholder = \"Select date\"]", "e => e.outerHTML"));
215            Assert.AreEqual("<input placeholder=\"Select date\">", await Page.EvalOnSelectorAsync<string>("[placeholder = 'Select date']", "e => e.outerHTML"));
216            Assert.AreEqual("<input placeholder=\"Select date\">", await Page.EvalOnSelectorAsync<string>("input[placeholder = \"Select date\"]", "e => e.outerHTML"));
217            Assert.AreEqual("<input placeholder=\"Select date\">", await Page.EvalOnSelectorAsync<string>("input[placeholder = 'Select date']", "e => e.outerHTML"));
218            Assert.AreEqual("<input placeholder=\"Select date\">", await Page.EvalOnSelectorAsync<string>("css =[placeholder = \"Select date\"]", "e => e.outerHTML"));
219            Assert.AreEqual("<input placeholder=\"Select date\">", await Page.EvalOnSelectorAsync<string>("css =[placeholder = 'Select date']", "e => e.outerHTML"));
220            Assert.AreEqual("<input placeholder=\"Select date\">", await Page.EvalOnSelectorAsync<string>("css = input[placeholder = \"Select date\"]", "e => e.outerHTML"));
221            Assert.AreEqual("<input placeholder=\"Select date\">", await Page.EvalOnSelectorAsync<string>("css = input[placeholder = 'Select date']", "e => e.outerHTML"));
222            Assert.AreEqual("<input placeholder=\"Select date\">", await Page.EvalOnSelectorAsync<string>("div >> [placeholder = \"Select date\"]", "e => e.outerHTML"));
223            Assert.AreEqual("<input placeholder=\"Select date\">", await Page.EvalOnSelectorAsync<string>("div >> [placeholder = 'Select date']", "e => e.outerHTML"));
224        }
225
226        [PlaywrightTest("eval-on-selector.spec.ts", "should work with quotes in css attributes")]
227        public async Task ShouldWorkWihQuotesInCssAttributes()
228        {
229            await Page.SetContentAsync("<div><input placeholder=\"Select&quot;date\"></div>");
230            Assert.NotNull(await Page.QuerySelectorAsync("[placeholder = \"Select\\\"date\"]"));
231            Assert.NotNull(await Page.QuerySelectorAsync("[placeholder = 'Select\"date']"));
232
233            await Page.SetContentAsync("<div><input placeholder=\"Select &quot; date\"></div>");
234            Assert.NotNull(await Page.QuerySelectorAsync("[placeholder = \"Select \\\" date\"]"));
235            Assert.NotNull(await Page.QuerySelectorAsync("[placeholder = 'Select \" date']"));
236
237            await Page.SetContentAsync("<div><input placeholder=\"Select&apos;date\"></div>");
238            Assert.NotNull(await Page.QuerySelectorAsync("[placeholder = \"Select'date\"]"));
239            Assert.NotNull(await Page.QuerySelectorAsync("[placeholder = 'Select\\'date']"));
240
241            await Page.SetContentAsync("<div><input placeholder=\"Select &apos; date\"></div>");
242            Assert.NotNull(await Page.QuerySelectorAsync("[placeholder = \"Select ' date\"]"));
243            Assert.NotNull(await Page.QuerySelectorAsync("[placeholder = 'Select \\' date']"));
244        }
245
246        [PlaywrightTest("eval-on-selector.spec.ts", "should work with quotes in css attributes when missing")]
247        public async Task ShouldWorkWihQuotesInCssAttributesWhenMissing()
248        {
249            var inputTask = Page.WaitForSelectorAsync("[placeholder = \"Select\\\"date\"]");
250            Assert.Null(await Page.QuerySelectorAsync("[placeholder = \"Select\\\"date\"]"));
251            await Page.SetContentAsync("<div><input placeholder=\"Select&quot;date\"></div>");
252            await inputTask;
253        }
254    }
255}
256
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)