Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.SelectorsTextTests.Query
SelectorsTextTests.cs
Source:SelectorsTextTests.cs
...28{29 public class SelectorsTextTests : PageTestEx30 {31 [PlaywrightTest("selectors-text.spec.ts", "query")]32 public async Task Query()33 {34 await Page.SetContentAsync("<div>yo</div><div>ya</div><div>\nye </div>");35 Assert.AreEqual("<div>ya</div>", await Page.EvalOnSelectorAsync<string>("text=ya", "e => e.outerHTML"));36 Assert.AreEqual("<div>ya</div>", await Page.EvalOnSelectorAsync<string>("text=\"ya\"", "e => e.outerHTML"));37 Assert.AreEqual("<div>ya</div>", await Page.EvalOnSelectorAsync<string>("text=/^[ay]+$/", "e => e.outerHTML"));38 Assert.AreEqual("<div>ya</div>", await Page.EvalOnSelectorAsync<string>("text=/Ya/i", "e => e.outerHTML"));39 Assert.AreEqual("<div>\nye </div>", await Page.EvalOnSelectorAsync<string>("text=ye", "e => e.outerHTML"));40 await Page.SetContentAsync("<div> ye </div><div>ye</div>");41 Assert.AreEqual("<div> ye </div>", await Page.EvalOnSelectorAsync<string>("text=\"ye\"", "e => e.outerHTML"));42 await Page.SetContentAsync("<div>yo</div><div>\"ya</div><div> hello world! </div>");43 Assert.AreEqual("<div>\"ya</div>", await Page.EvalOnSelectorAsync<string>("text=\"\\\"ya\"", "e => e.outerHTML"));44 Assert.AreEqual("<div> hello world! </div>", await Page.EvalOnSelectorAsync<string>("text=/hello/", "e => e.outerHTML"));45 Assert.AreEqual("<div> hello world! </div>", await Page.EvalOnSelectorAsync<string>("text=/^\\s*heLLo/i", "e => e.outerHTML"));46 await Page.SetContentAsync("<div>yo<div>ya</div>hey<div>hey</div></div>");47 Assert.AreEqual("<div>hey</div>", await Page.EvalOnSelectorAsync<string>("text=hey", "e => e.outerHTML"));48 Assert.AreEqual("<div>ya</div>", await Page.EvalOnSelectorAsync<string>("text=\"yo\" >> text =\"ya\"", "e => e.outerHTML"));49 Assert.AreEqual("<div>ya</div>", await Page.EvalOnSelectorAsync<string>("text='yo' >> text =\"ya\"", "e => e.outerHTML"));50 Assert.AreEqual("<div>ya</div>", await Page.EvalOnSelectorAsync<string>("text=\"yo\" >> text='ya'", "e => e.outerHTML"));51 Assert.AreEqual("<div>ya</div>", await Page.EvalOnSelectorAsync<string>("text='yo' >> text='ya'", "e => e.outerHTML"));52 Assert.AreEqual("<div>ya</div>", await Page.EvalOnSelectorAsync<string>("'yo' >> \"ya\"", "e => e.outerHTML"));53 Assert.AreEqual("<div>ya</div>", await Page.EvalOnSelectorAsync<string>("\"yo\" >> 'ya'", "e => e.outerHTML"));54 await Page.SetContentAsync("<div>yo<span id=\"s1\"></span></div><div>yo<span id=\"s2\"></span><span id=\"s3\"></span></div>");55 Assert.AreEqual("<div>yo<span id=\"s1\"></span></div>\n<div>yo<span id=\"s2\"></span><span id=\"s3\"></span></div>", await Page.EvalOnSelectorAllAsync<string>("text=yo", "es => es.map(e => e.outerHTML).join('\\n')"));56 await Page.SetContentAsync("<div>'</div><div>\"</div><div>\\</div><div>x</div>");57 Assert.AreEqual("<div>'</div>", await Page.EvalOnSelectorAsync<string>("text='\\''", "e => e.outerHTML"));58 Assert.AreEqual("<div>\"</div>", await Page.EvalOnSelectorAsync<string>("text='\"'", "e => e.outerHTML"));59 Assert.AreEqual("<div>\"</div>", await Page.EvalOnSelectorAsync<string>("text=\"\\\"\"", "e => e.outerHTML"));60 Assert.AreEqual("<div>'</div>", await Page.EvalOnSelectorAsync<string>("text=\"'\"", "e => e.outerHTML"));61 Assert.AreEqual("<div>x</div>", await Page.EvalOnSelectorAsync<string>("text=\"\\x\"", "e => e.outerHTML"));62 Assert.AreEqual("<div>x</div>", await Page.EvalOnSelectorAsync<string>("text='\\x'", "e => e.outerHTML"));63 Assert.AreEqual("<div>\\</div>", await Page.EvalOnSelectorAsync<string>("text='\\\\'", "e => e.outerHTML"));64 Assert.AreEqual("<div>\\</div>", await Page.EvalOnSelectorAsync<string>("text=\"\\\\\"", "e => e.outerHTML"));65 Assert.AreEqual("<div>\"</div>", await Page.EvalOnSelectorAsync<string>("text=\"", "e => e.outerHTML"));66 Assert.AreEqual("<div>'</div>", await Page.EvalOnSelectorAsync<string>("text='", "e => e.outerHTML"));67 Assert.AreEqual("<div>x</div>", await Page.EvalOnSelectorAsync<string>("\"x\"", "e => e.outerHTML"));68 Assert.AreEqual("<div>x</div>", await Page.EvalOnSelectorAsync<string>("'x'", "e => e.outerHTML"));69 await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Page.QuerySelectorAsync("\""));70 await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Page.QuerySelectorAsync("'"));71 await Page.SetContentAsync("<div> ' </div><div> \" </div>");72 Assert.AreEqual("<div> \" </div>", await Page.EvalOnSelectorAsync<string>("text=\"", "e => e.outerHTML"));73 Assert.AreEqual("<div> ' </div>", await Page.EvalOnSelectorAsync<string>("text='", "e => e.outerHTML"));74 await Page.SetContentAsync("<div>Hi''>>foo=bar</div>");75 Assert.AreEqual("<div>Hi''>>foo=bar</div>", await Page.EvalOnSelectorAsync<string>("text=\"Hi''>>foo=bar\"", "e => e.outerHTML"));76 await Page.SetContentAsync("<div>Hi'\">>foo=bar</div> ");77 Assert.AreEqual("<div>Hi'\">>foo=bar</div>", await Page.EvalOnSelectorAsync<string>("text=\"Hi'\\\">>foo=bar\"", "e => e.outerHTML"));78 await Page.SetContentAsync("<div>Hi>><span></span></div>");79 Assert.AreEqual("<span></span>", await Page.EvalOnSelectorAsync<string>("text=\"Hi>>\">>span", "e => e.outerHTML"));80 await Page.SetContentAsync("<div>a<br>b</div><div>a</div>");81 Assert.AreEqual("<div>a<br>b</div>", await Page.EvalOnSelectorAsync<string>("text=a", "e => e.outerHTML"));82 Assert.AreEqual("<div>a<br>b</div>", await Page.EvalOnSelectorAsync<string>("text=b", "e => e.outerHTML"));83 Assert.AreEqual("<div>a<br>b</div>", await Page.EvalOnSelectorAsync<string>("text=ab", "e => e.outerHTML"));84 Assert.Null(await Page.QuerySelectorAsync("text=abc"));85 Assert.AreEqual(2, await Page.EvalOnSelectorAllAsync<int>("text=a", "els => els.length"));86 Assert.AreEqual(1, await Page.EvalOnSelectorAllAsync<int>("text=b", "els => els.length"));87 Assert.AreEqual(1, await Page.EvalOnSelectorAllAsync<int>("text=ab", "els => els.length"));88 Assert.AreEqual(0, await Page.EvalOnSelectorAllAsync<int>("text=abc", "els => els.length"));89 await Page.SetContentAsync("<div></div><span></span>");90 await Page.EvalOnSelectorAsync("div", @"div =>91 {92 div.appendChild(document.createTextNode('hello'));93 div.appendChild(document.createTextNode('world'));94 }");95 await Page.EvalOnSelectorAsync("span", @"span =>96 {97 span.appendChild(document.createTextNode('hello'));98 span.appendChild(document.createTextNode('world'));99 }");100 Assert.AreEqual("<div>helloworld</div>", await Page.EvalOnSelectorAsync<string>("text=lowo", "e => e.outerHTML"));101 Assert.AreEqual("<div>helloworld</div><span>helloworld</span>", await Page.EvalOnSelectorAllAsync<string>("text=lowo", "els => els.map(e => e.outerHTML).join('')"));102 }103 [PlaywrightTest("selectors-text.spec.ts", "should be case sensitive if quotes are specified")]104 public async Task ShouldBeCaseSensitiveIfQuotesAreSpecified()105 {106 await Page.SetContentAsync("<div>yo</div><div>ya</div><div>\nye </div>");107 Assert.AreEqual("<div>ya</div>", await Page.EvalOnSelectorAsync<string>("text=ya", "e => e.outerHTML"));108 Assert.Null(await Page.QuerySelectorAsync("text=\"yA\""));109 }110 [PlaywrightTest("selectors-text.spec.ts", "should search for a substring without quotes")]111 public async Task ShouldSearchForASubstringWithoutQuotes()112 {113 await Page.SetContentAsync("<div>textwithsubstring</div>");114 Assert.AreEqual("<div>textwithsubstring</div>", await Page.EvalOnSelectorAsync<string>("text=with", "e => e.outerHTML"));115 Assert.Null(await Page.QuerySelectorAsync("text=\"with\""));116 }117 [PlaywrightTest("selectors-text.spec.ts", "should skip head, script and style")]118 public async Task ShouldSkipHeadScriptAndStyle()119 {120 await Page.SetContentAsync(@"121 <head>122 <title>title</title>123 <script>var script</script>124 <style>.style {}</style>125 </head>126 <body>127 <script>var script</script>128 <style>.style {}</style>129 <div>title script style</div>130 </body>");131 var head = await Page.QuerySelectorAsync("head");132 var title = await Page.QuerySelectorAsync("title");133 var script = await Page.QuerySelectorAsync("body script");134 var style = await Page.QuerySelectorAsync("body style");135 foreach (string text in new[] { "title", "script", "style" })136 {137 Assert.AreEqual("DIV", await Page.EvalOnSelectorAsync<string>($"text={text}", "e => e.nodeName"));138 Assert.AreEqual("DIV", await Page.EvalOnSelectorAllAsync<string>($"text={text}", "els => els.map(e => e.nodeName).join('|')"));139 foreach (var root in new[] { head, title, script, style })140 {141 Assert.Null(await root.QuerySelectorAsync($"text={text}"));142 Assert.AreEqual(0, await root.EvalOnSelectorAllAsync<int>($"text={text}", "els => els.length"));143 }144 }145 }146 [PlaywrightTest("selectors-text.spec.ts", "should match input[type=button|submit]")]147 public async Task ShouldMatchInputTypeButtonSubmit()148 {149 await Page.SetContentAsync("<input type=\"submit\" value=\"hello\"><input type=\"button\" value=\"world\">");150 Assert.AreEqual("<input type=\"submit\" value=\"hello\">", await Page.EvalOnSelectorAsync<string>("text=hello", "e => e.outerHTML"));151 Assert.AreEqual("<input type=\"button\" value=\"world\">", await Page.EvalOnSelectorAsync<string>("text=world", "e => e.outerHTML"));152 }153 [PlaywrightTest("selectors-text.spec.ts", "should work for open shadow roots")]154 public async Task ShouldWorkForOpenShadowRoots()155 {156 await Page.GotoAsync(Server.Prefix + "/deep-shadow.html");157 Assert.AreEqual("Hello from root1", await Page.EvalOnSelectorAsync<string>("text=root1", "e => e.textContent"));158 Assert.AreEqual("Hello from root2", await Page.EvalOnSelectorAsync<string>("text=root2", "e => e.textContent"));159 Assert.AreEqual("Hello from root3", await Page.EvalOnSelectorAsync<string>("text=root3", "e => e.textContent"));160 Assert.AreEqual("Hello from root3", await Page.EvalOnSelectorAsync<string>("#root1 >> text=from root3", "e => e.textContent"));161 Assert.AreEqual("Hello from root2", await Page.EvalOnSelectorAsync<string>("#target >> text=from root2", "e => e.textContent"));162 Assert.Null(await Page.QuerySelectorAsync("text:light=root1"));163 Assert.Null(await Page.QuerySelectorAsync("text:light=root2"));164 Assert.Null(await Page.QuerySelectorAsync("text:light=root3"));165 }166 [PlaywrightTest("selectors-text.spec.ts", "should prioritize light dom over shadow dom in the same parent")]167 public async Task ShouldPrioritizeLightDomOverShadowDomInTheSameParent()168 {169 await Page.EvaluateAsync(@"170 const div = document.createElement('div');171 document.body.appendChild(div);172 div.attachShadow({ mode: 'open' });173 const shadowSpan = document.createElement('span');174 shadowSpan.textContent = 'Hello from shadow';175 div.shadowRoot.appendChild(shadowSpan);176 const lightSpan = document.createElement('span');177 lightSpan.textContent = 'Hello from light';178 div.appendChild(lightSpan);...
Query
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.Playwright;7{8 {9 static async Task Main(string[] args)10 {11 using var playwright = await Playwright.CreateAsync();12 await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions13 {14 });15 var page = await browser.NewPageAsync();16 var element = await page.QuerySelectorAsync("text=English");17 await element.ClickAsync();18 var element1 = await page.QuerySelectorAsync("text=English");19 await element1.ClickAsync();20 var element2 = await page.QuerySelectorAsync("text=English");21 await element2.ClickAsync();22 var element3 = await page.QuerySelectorAsync("text=English");23 await element3.ClickAsync();24 var element4 = await page.QuerySelectorAsync("text=English");25 await element4.ClickAsync();26 }27 }28}
Query
Using AI Code Generation
1var result = await Playwright.SelectorsTextTests.Query();2var result = await Playwright.SelectorsTextTests.QueryAll();3var result = await Playwright.SelectorsTextTests.QueryAllArray();4var result = await Playwright.SelectorsTextTests.QueryArray();5var result = await Playwright.SelectorsTextTests.QueryAllArrayAsync();6var result = await Playwright.SelectorsTextTests.QueryArrayAsync();7var result = await Playwright.SelectorsTextTests.QueryAsync();8var result = await Playwright.SelectorsTextTests.QueryAllAsync();9var result = await Playwright.SelectorsTextTests.QueryAllArray();10var result = await Playwright.SelectorsTextTests.QueryArray();11var result = await Playwright.SelectorsTextTests.QueryAllArrayAsync();12var result = await Playwright.SelectorsTextTests.QueryArrayAsync();13var result = await Playwright.SelectorsTextTests.QueryAsync();
Query
Using AI Code Generation
1{2 [Parallelizable(ParallelScope.Self)]3 {4 [PlaywrightTest("selectors-text.spec.ts", "should work with text=")]5 [Test, Timeout(TestConstants.DefaultTestTimeout)]6 public async Task ShouldWorkWithText()7 {8 await Page.SetContentAsync("<div>ee!</div>");9 var div = await Page.QuerySelectorAsync("text=ee!");10 Assert.AreEqual("DIV", div.TagName);11 }12 }13}14{15 [Parallelizable(ParallelScope.Self)]16 {17 [PlaywrightTest("selectors-text.spec.ts", "should work with text=/regex/")]18 [Test, Timeout(TestConstants.DefaultTestTimeout)]19 public async Task ShouldWorkWithTextRegex()20 {21 await Page.SetContentAsync("<div>ee!</div>");22 var div = await Page.QuerySelectorAsync("text=/e+/");23 Assert.AreEqual("DIV", div.TagName);24 }25 }26}27{28 [Parallelizable(ParallelScope.Self)]29 {30 [PlaywrightTest("selectors-text.spec.ts", "should work with text=/regex/i")]31 [Test, Timeout(TestConstants.DefaultTestTimeout)]32 public async Task ShouldWorkWithTextRegexI()33 {34 await Page.SetContentAsync("<div>ee!</div>");35 var div = await Page.QuerySelectorAsync("text=/E+/i");36 Assert.AreEqual("DIV", div.TagName);37 }38 }39}40{41 [Parallelizable(ParallelScope.Self)]42 {43 [PlaywrightTest("selectors-text.spec.ts", "should work with text=^")]44 [Test, Timeout(TestConstants.DefaultTestTimeout)]45 public async Task ShouldWorkWithTextCaret()46 {47 await Page.SetContentAsync("<div>ee!</div>");48 var div = await Page.QuerySelectorAsync("text=^ee");49 Assert.AreEqual("DIV", div.TagName);50 }51 }52}53{54 [Parallelizable(ParallelScope.Self)]
Query
Using AI Code Generation
1using Microsoft.Playwright.Tests;2SelectorsTextTests.SelectorsTextTests obj = new SelectorsTextTests.SelectorsTextTests();3obj.Query();4using Microsoft.Playwright.Tests;5SelectorsTextTests.SelectorsTextTests obj = new SelectorsTextTests.SelectorsTextTests();6obj.QueryAll();7using Microsoft.Playwright.Tests;8SelectorsTextTests.SelectorsTextTests obj = new SelectorsTextTests.SelectorsTextTests();9obj.QueryAllArray();10using Microsoft.Playwright.Tests;11SelectorsTextTests.SelectorsTextTests obj = new SelectorsTextTests.SelectorsTextTests();12obj.QueryArray();13using Microsoft.Playwright.Tests;14SelectorsTextTests.SelectorsTextTests obj = new SelectorsTextTests.SelectorsTextTests();15obj.QueryAllAsync();16using Microsoft.Playwright.Tests;17SelectorsTextTests.SelectorsTextTests obj = new SelectorsTextTests.SelectorsTextTests();18obj.QueryAsync();19using Microsoft.Playwright.Tests;20SelectorsTextTests.SelectorsTextTests obj = new SelectorsTextTests.SelectorsTextTests();21obj.QueryAllArrayAsync();22using Microsoft.Playwright.Tests;23SelectorsTextTests.SelectorsTextTests obj = new SelectorsTextTests.SelectorsTextTests();24obj.QueryArrayAsync();25using Microsoft.Playwright.Tests;26SelectorsTextTests.SelectorsTextTests obj = new SelectorsTextTests.SelectorsTextTests();27obj.QueryAllWithShadowDom();
Query
Using AI Code Generation
1await page.QuerySelectorAsync("text=hello");2await page.QuerySelectorAsync("text=hello world");3await page.QuerySelectorAsync("text='hello world'");4await page.QuerySelectorAsync("text=\"hello world\"");5await page.QuerySelectorAsync("text=/hello world/");6await page.QuerySelectorAsync("text=/hello.*world/");
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!