How to use ShouldWaitForVisible method of Microsoft.Playwright.Tests.ElementHandleSelectTextTests class

Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.ElementHandleSelectTextTests.ShouldWaitForVisible

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

ElementHandleSelectTextTests.cs

Source: ElementHandleSelectTextTests.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;
26using System.Threading.Tasks;
27using Microsoft.Playwright.NUnit;
28using NUnit.Framework;
29
30namespace Microsoft.Playwright.Tests
31{
32    public class ElementHandleSelectTextTests : PageTestEx
33    {
34        [PlaywrightTest("elementhandle-select-text.spec.ts", "should select textarea")]
35        public async Task ShouldSelectTextarea()
36        {
37            await Page.GotoAsync(Server.Prefix + "/input/textarea.html");
38            var textarea = await Page.QuerySelectorAsync("textarea");
39            await textarea.EvaluateAsync("textarea => textarea.value = 'some value'");
40            await textarea.SelectTextAsync();
41
42            if (TestConstants.IsFirefox)
43            {
44                Assert.AreEqual(0, await textarea.EvaluateAsync<int>("el => el.selectionStart"));
45                Assert.AreEqual(10, await textarea.EvaluateAsync<int>("el => el.selectionEnd"));
46            }
47            else
48            {
49                Assert.AreEqual("some value", await Page.EvaluateAsync<string>("() => window.getSelection().toString()"));
50            }
51        }
52
53        [PlaywrightTest("elementhandle-select-text.spec.ts", "should select input")]
54        public async Task ShouldSelectInput()
55        {
56            await Page.GotoAsync(Server.Prefix + "/input/textarea.html");
57            var input = await Page.QuerySelectorAsync("input");
58            await input.EvaluateAsync("input => input.value = 'some value'");
59            await input.SelectTextAsync();
60
61            if (TestConstants.IsFirefox)
62            {
63                Assert.AreEqual(0, await input.EvaluateAsync<int>("el => el.selectionStart"));
64                Assert.AreEqual(10, await input.EvaluateAsync<int>("el => el.selectionEnd"));
65            }
66            else
67            {
68                Assert.AreEqual("some value", await Page.EvaluateAsync<string>("() => window.getSelection().toString()"));
69            }
70        }
71
72        [PlaywrightTest("elementhandle-select-text.spec.ts", "should select plain div")]
73        public async Task ShouldSelectPlainDiv()
74        {
75            await Page.GotoAsync(Server.Prefix + "/input/textarea.html");
76            var div = await Page.QuerySelectorAsync("div.plain");
77            await div.EvaluateAsync("input => input.value = 'some value'");
78            await div.SelectTextAsync();
79
80            Assert.AreEqual("Plain div", await Page.EvaluateAsync<string>("() => window.getSelection().toString()"));
81        }
82
83        [PlaywrightTest("elementhandle-select-text.spec.ts", "should timeout waiting for invisible element")]
84        public async Task ShouldTimeoutWaitingForInvisibleElement()
85        {
86            await Page.GotoAsync(Server.Prefix + "/input/textarea.html");
87            var textarea = await Page.QuerySelectorAsync("textarea");
88            await textarea.EvaluateAsync("e => e.style.display = 'none'");
89
90            var exception = await PlaywrightAssert.ThrowsAsync<TimeoutException>(() => textarea.SelectTextAsync(new() { Timeout = 3000 }));
91            StringAssert.Contains("element is not visible", exception.Message);
92        }
93
94        [PlaywrightTest("elementhandle-select-text.spec.ts", "should wait for visible")]
95        public async Task ShouldWaitForVisible()
96        {
97            await Page.GotoAsync(Server.Prefix + "/input/textarea.html");
98            var textarea = await Page.QuerySelectorAsync("textarea");
99            await textarea.EvaluateAsync("textarea => textarea.value = 'some value'");
100            await textarea.EvaluateAsync("e => e.style.display = 'none'");
101
102            var task = textarea.SelectTextAsync(new() { Timeout = 3000 });
103            await Page.EvaluateAsync("() => new Promise(f => setTimeout(f, 1000))");
104            Assert.False(task.IsCompleted);
105            await textarea.EvaluateAsync("e => e.style.display = 'block'");
106            await task;
107        }
108    }
109}
110
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 ShouldWaitForVisible code on LambdaTest Cloud Grid

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