How to use TextContentShouldWork method of Microsoft.Playwright.Tests.Locator.LocatorConvenienceTests class

Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.Locator.LocatorConvenienceTests.TextContentShouldWork

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

LocatorConvenienceTests.cs

Source: LocatorConvenienceTests.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 NUnit.Framework;
28
29namespace Microsoft.Playwright.Tests.Locator
30{
31    public class LocatorConvenienceTests : PageTestEx
32    {
33        [PlaywrightTest("locator-convenience.spec.ts", "should have a nice preview")]
34        public async Task ShouldHaveANicePreview()
35        {
36            await Page.GotoAsync(Server.Prefix + "/dom.html");
37            var outer = Page.Locator("#outer");
38            var inner = outer.Locator("#inner");
39            var check = Page.Locator("#check");
40
41            var text = await inner.EvaluateHandleAsync("e => e.firstChild");
42            await Page.EvaluateAsync("() => 1");
43
44            Assert.AreEqual("[email protected]#outer", outer.ToString());
45            Assert.AreEqual("[email protected]#outer >> #inner", inner.ToString());
46            Assert.AreEqual("[email protected]#text=Text,↵more text", text.ToString());
47            Assert.AreEqual("[email protected]#check", check.ToString());
48        }
49
50        [PlaywrightTest("locator-convenience.spec.ts", "getAttribute should work")]
51        public async Task GetAttributeShouldWork()
52        {
53            await Page.GotoAsync(Server.Prefix + "/dom.html");
54            var locator = Page.Locator("#outer");
55            Assert.AreEqual("value", await locator.GetAttributeAsync("name"));
56            Assert.IsNull(await locator.GetAttributeAsync("foo"));
57            Assert.AreEqual("value", await Page.GetAttributeAsync("#outer", "name"));
58            Assert.IsNull(await Page.GetAttributeAsync("#outer", "foo"));
59        }
60
61        [PlaywrightTest("locator-convenience.spec.ts", "inputValue should work")]
62        public async Task InputValueShouldWork()
63        {
64            await Page.GotoAsync(Server.Prefix + "/dom.html");
65
66            await Page.SelectOptionAsync("#select", "foo");
67            Assert.AreEqual("foo", await Page.InputValueAsync("#select"));
68
69            await Page.FillAsync("#textarea", "text value");
70            Assert.AreEqual("text value", await Page.InputValueAsync("#textarea"));
71
72            await Page.FillAsync("#input", "input value");
73            Assert.AreEqual("input value", await Page.InputValueAsync("#input"));
74
75            var locator = Page.Locator("#input");
76            Assert.AreEqual("input value", await locator.InputValueAsync());
77
78            var e = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(async () => await Page.InputValueAsync("#inner"));
79            StringAssert.Contains("Node is not an <input>, <textarea> or <select> element", e.Message);
80
81            var locator2 = Page.Locator("#inner");
82            e = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(async () => await locator2.InputValueAsync());
83
84            StringAssert.Contains("Node is not an <input>, <textarea> or <select> element", e.Message);
85        }
86
87        [PlaywrightTest("locator-convenience.spec.ts", "innerHTML should work")]
88        public async Task InnerHTMLShouldWork()
89        {
90            await Page.GotoAsync(Server.Prefix + "/dom.html");
91            var locator = Page.Locator("#outer");
92            Assert.AreEqual("<div id=\"inner\">Text,\nmore text</div>", await locator.InnerHTMLAsync());
93            Assert.AreEqual("<div id=\"inner\">Text,\nmore text</div>", await Page.InnerHTMLAsync("#outer"));
94        }
95
96        [PlaywrightTest("locator-convenience.spec.ts", "innerText should work")]
97        public async Task InnerTextShouldWork()
98        {
99            await Page.GotoAsync(Server.Prefix + "/dom.html");
100            var locator = Page.Locator("#inner");
101            Assert.AreEqual("Text, more text", await locator.InnerTextAsync());
102            Assert.AreEqual("Text, more text", await Page.InnerTextAsync("#inner"));
103        }
104
105
106        [PlaywrightTest("locator-convenience.spec.ts", "innerText should throw")]
107        public async Task InnerTextShouldThrow()
108        {
109            await Page.SetContentAsync("<svg>text</svg>");
110            var e = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(async () => await Page.InnerTextAsync("svg"));
111            StringAssert.Contains("Node is not an HTMLElement", e.Message);
112
113            var locator = Page.Locator("svg");
114            e = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(async () => await locator.InnerTextAsync());
115            StringAssert.Contains("Node is not an HTMLElement", e.Message);
116        }
117
118
119        [PlaywrightTest("locator-convenience.spec.ts", "textContent should work")]
120        public async Task TextContentShouldWork()
121        {
122            await Page.GotoAsync(Server.Prefix + "/dom.html");
123            var locator = Page.Locator("#inner");
124            Assert.AreEqual("Text,\nmore text", await locator.TextContentAsync());
125            Assert.AreEqual("Text,\nmore text", await Page.TextContentAsync("#inner"));
126        }
127
128        [PlaywrightTest("locator-convenience.spec.ts", "textContent should be atomic")]
129        public async Task TextContentShouldBeAtomic()
130        {
131            string script = @"
132({
133    query(root, selector) {
134      const result = root.querySelector(selector);
135      if (result)
136        Promise.resolve().then(() => result.textContent = 'modified');
137      return result;
138    },
139    queryAll(root, selector) {
140      const result = Array.from(root.querySelectorAll(selector));
141      for (const e of result)
142        Promise.resolve().then(() => e.textContent = 'modified');
143      return result;
144    }
145 })";
146            await Playwright.Selectors.RegisterAsync("textContentFromLocators", new() { Script = script });
147            await Page.SetContentAsync("<div>Hello</div>");
148            var tc = await Page.TextContentAsync("textContentFromLocators=div");
149            Assert.AreEqual("Hello", tc);
150            Assert.AreEqual("modified", await Page.EvaluateAsync<string>("() => document.querySelector('div').innerText"));
151        }
152
153
154        [PlaywrightTest("locator-convenience.spec.ts", "isVisible and isHidden should work")]
155        public async Task IsVisibleAndIsHiddenShouldWork()
156        {
157            await Page.SetContentAsync("<div>Hi</div><span></span>");
158
159            var div = Page.Locator("div");
160            Assert.IsTrue(await div.IsVisibleAsync());
161            Assert.IsFalse(await div.IsHiddenAsync());
162            Assert.IsTrue(await Page.IsVisibleAsync("div"));
163            Assert.IsFalse(await Page.IsHiddenAsync("div"));
164
165            var span = Page.Locator("span");
166            Assert.IsFalse(await span.IsVisibleAsync());
167            Assert.IsTrue(await span.IsHiddenAsync());
168            Assert.IsFalse(await Page.IsVisibleAsync("span"));
169            Assert.IsTrue(await Page.IsHiddenAsync("span"));
170
171            Assert.IsFalse(await Page.IsVisibleAsync("no-such-element"));
172            Assert.IsTrue(await Page.IsHiddenAsync("no-such-element"));
173        }
174
175        [PlaywrightTest("locator-convenience.spec.ts", "isEnabled and isDisabled should work")]
176        public async Task IsEnabledAndIsDisabledShouldWork()
177        {
178            await Page.SetContentAsync(@"
179<button disabled>button1</button>
180<button>button2</button>
181<div>div</div>
182");
183            var div = Page.Locator("div");
184            Assert.IsTrue(await div.IsEnabledAsync());
185            Assert.IsFalse(await div.IsDisabledAsync());
186            Assert.IsTrue(await Page.IsEnabledAsync("div"));
187            Assert.IsFalse(await Page.IsDisabledAsync("div"));
188
189            var button1 = Page.Locator(":text(\"button1\")");
190            Assert.IsFalse(await button1.IsEnabledAsync());
191            Assert.IsTrue(await button1.IsDisabledAsync());
192            Assert.IsFalse(await Page.IsEnabledAsync(":text(\"button1\")"));
193            Assert.IsTrue(await Page.IsDisabledAsync(":text(\"button1\")"));
194
195            var button2 = Page.Locator(":text(\"button2\")");
196            Assert.IsTrue(await button2.IsEnabledAsync());
197            Assert.IsFalse(await button2.IsDisabledAsync());
198            Assert.IsTrue(await Page.IsEnabledAsync(":text(\"button2\")"));
199            Assert.IsFalse(await Page.IsDisabledAsync(":text(\"button2\")"));
200        }
201
202        [PlaywrightTest("locator-convenience.spec.ts", "isEditable should work")]
203        public async Task IsEditableShouldWork()
204        {
205            await Page.SetContentAsync("<input id=input1 disabled><textarea></textarea><input id=input2>");
206            await Page.EvaluateAsync("t => t.readOnly = true", await Page.QuerySelectorAsync("textarea"));
207
208            var input1 = Page.Locator("#input1");
209            Assert.IsFalse(await input1.IsEditableAsync());
210            Assert.IsFalse(await Page.IsEditableAsync("#input1"));
211
212            var input2 = Page.Locator("#input2");
213            Assert.IsTrue(await input2.IsEditableAsync());
214            Assert.IsTrue(await Page.IsEditableAsync("#input2"));
215
216            var textarea = Page.Locator("textarea");
217            Assert.IsFalse(await textarea.IsEditableAsync());
218            Assert.IsFalse(await Page.IsEditableAsync("textarea"));
219        }
220
221        [PlaywrightTest("locator-convenience.spec.ts", "isChecked should work")]
222        public async Task IsCheckedShouldWork()
223        {
224            await Page.SetContentAsync("<input type='checkbox' checked><div>Not a checkbox</div>");
225            var element = Page.Locator("input");
226            Assert.IsTrue(await element.IsCheckedAsync());
227            Assert.IsTrue(await Page.IsCheckedAsync("input"));
228            await element.EvaluateAsync<bool>("input => input.checked = false");
229
230            Assert.IsFalse(await element.IsCheckedAsync());
231            Assert.IsFalse(await Page.IsCheckedAsync("input"));
232
233            var e = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(async () => await Page.IsCheckedAsync("div"));
234            StringAssert.Contains("Not a checkbox or radio button", e.Message);
235        }
236
237        [PlaywrightTest("locator-convenience.spec.ts", "allTextContents should work")]
238        public async Task AllTextContentsShouldWork()
239        {
240            await Page.SetContentAsync("<div>A</div><div>B</div><div>C</div>");
241            CollectionAssert.AreEqual(new string[] { "A", "B", "C" }, (await Page.Locator("div").AllTextContentsAsync()).ToArray());
242        }
243
244        [PlaywrightTest("locator-convenience.spec.ts", "allInnerTexts should work")]
245        public async Task AllInnerTextsShouldWork()
246        {
247            await Page.SetContentAsync("<div>A</div><div>B</div><div>C</div>");
248            CollectionAssert.AreEqual(new string[] { "A", "B", "C" }, (await Page.Locator("div").AllInnerTextsAsync()).ToArray());
249        }
250
251        [PlaywrightTest("locator-convenience.spec.ts", "should return page")]
252        public async Task ShouldReturnPageInstance()
253        {
254            await Page.GotoAsync(Server.Prefix + "/frames/two-frames.html");
255            var outer = Page.Locator("#outer");
256            Assert.AreEqual(outer.Page, Page);
257
258            var inner = outer.Locator("#inner");
259            Assert.AreEqual(inner.Page, Page);
260
261            var inFrame = Page.Frames[1].Locator("div");
262            Assert.AreEqual(inFrame.Page, Page);
263        }
264    }
265}
266
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 TextContentShouldWork code on LambdaTest Cloud Grid

Execute automation tests with TextContentShouldWork 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)