How to use TextContentShouldBeAtomic method of Microsoft.Playwright.Tests.ElementHandleConvenienceTests class

Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.ElementHandleConvenienceTests.TextContentShouldBeAtomic

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

ElementHandleConvenienceTests.cs

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

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