How to use ShouldWaitForEnabledButton method of Microsoft.Playwright.Tests.ElementHandleWaitForElementStateTests class

Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.ElementHandleWaitForElementStateTests.ShouldWaitForEnabledButton

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

ElementHandleWaitForElementStateTests.cs

Source: ElementHandleWaitForElementStateTests.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    ///<playwright-file>elementhandle-wait-for-element-state.spec.ts</playwright-file>
33    public class ElementHandleWaitForElementStateTests : PageTestEx
34    {
35        [PlaywrightTest("elementhandle-wait-for-element-state.spec.ts", "should wait for visible")]
36        public async Task ShouldWaitForVisible()
37        {
38            await Page.SetContentAsync("<div style='display:none'>content</div>");
39            var div = await Page.QuerySelectorAsync("div");
40            var task = div.WaitForElementStateAsync(ElementState.Visible);
41            await GiveItAChanceToResolve(Page);
42            Assert.False(task.IsCompleted);
43            await div.EvaluateAsync("div => div.style.display = 'block'");
44            await task;
45        }
46
47        [PlaywrightTest("elementhandle-wait-for-element-state.spec.ts", "should wait for already visible")]
48        public async Task ShouldWaitForAlreadyVisible()
49        {
50            await Page.SetContentAsync("<div>content</div>");
51            var div = await Page.QuerySelectorAsync("div");
52            await div.WaitForElementStateAsync(ElementState.Visible);
53        }
54
55        [PlaywrightTest("elementhandle-wait-for-element-state.spec.ts", "should timeout waiting for visible")]
56        public async Task ShouldTimeoutWaitingForVisible()
57        {
58            await Page.SetContentAsync("<div style='display:none'>content</div>");
59            var div = await Page.QuerySelectorAsync("div");
60            var exception = await PlaywrightAssert.ThrowsAsync<TimeoutException>(() => div.WaitForElementStateAsync(ElementState.Visible, new() { Timeout = 1000 }));
61            StringAssert.Contains("Timeout 1000ms exceeded", exception.Message);
62        }
63
64        [PlaywrightTest("elementhandle-wait-for-element-state.spec.ts", "should throw waiting for visible when detached")]
65        public async Task ShouldThrowWaitingForVisibleWhenDetached()
66        {
67            await Page.SetContentAsync("<div style='display:none'>content</div>");
68            var div = await Page.QuerySelectorAsync("div");
69            var task = div.WaitForElementStateAsync(ElementState.Visible);
70            await div.EvaluateAsync("div => div.remove()");
71            var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => task);
72            StringAssert.Contains("Element is not attached to the DOM", exception.Message);
73        }
74
75        [PlaywrightTest("elementhandle-wait-for-element-state.spec.ts", "should wait for hidden")]
76        public async Task ShouldWaitForHidden()
77        {
78            await Page.SetContentAsync("<div>content</div>");
79            var div = await Page.QuerySelectorAsync("div");
80            var task = div.WaitForElementStateAsync(ElementState.Hidden);
81            await GiveItAChanceToResolve(Page);
82            Assert.False(task.IsCompleted);
83            await div.EvaluateAsync("div => div.style.display = 'none'");
84            await task;
85        }
86
87        [PlaywrightTest("elementhandle-wait-for-element-state.spec.ts", "should wait for already hidden")]
88        public async Task ShouldWaitForAlreadyHidden()
89        {
90            await Page.SetContentAsync("<div></div>");
91            var div = await Page.QuerySelectorAsync("div");
92            await div.WaitForElementStateAsync(ElementState.Hidden);
93        }
94
95        [PlaywrightTest("elementhandle-wait-for-element-state.spec.ts", "should throw waiting for hidden when detached")]
96        public async Task ShouldThrowWaitingForHiddenWhenDetached()
97        {
98            await Page.SetContentAsync("<div>content</div>");
99            var div = await Page.QuerySelectorAsync("div");
100            var task = div.WaitForElementStateAsync(ElementState.Hidden);
101            await GiveItAChanceToResolve(Page);
102            Assert.False(task.IsCompleted);
103            await div.EvaluateAsync("div => div.remove()");
104            await task;
105        }
106
107        [PlaywrightTest("elementhandle-wait-for-element-state.spec.ts", "should wait for enabled button")]
108        public async Task ShouldWaitForEnabledButton()
109        {
110            await Page.SetContentAsync("<button disabled><span>Target</span></button>");
111            var span = await Page.QuerySelectorAsync("text=Target");
112            var task = span.WaitForElementStateAsync(ElementState.Enabled);
113            await GiveItAChanceToResolve(Page);
114            Assert.False(task.IsCompleted);
115            await span.EvaluateAsync("span => span.parentElement.disabled = false");
116            await task;
117        }
118
119        [PlaywrightTest("elementhandle-wait-for-element-state.spec.ts", "should throw waiting for enabled when detached")]
120        public async Task ShouldThrowWaitingForEnabledWhenDetached()
121        {
122            await Page.SetContentAsync("<button disabled>Target</button>");
123            var button = await Page.QuerySelectorAsync("button");
124            var task = button.WaitForElementStateAsync(ElementState.Enabled);
125            await button.EvaluateAsync("button => button.remove()");
126            var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => task);
127            StringAssert.Contains("Element is not attached to the DOM", exception.Message);
128        }
129
130        [PlaywrightTest("elementhandle-wait-for-element-state.spec.ts", "should wait for disabled button")]
131        public async Task ShouldWaitForDisabledButton()
132        {
133            await Page.SetContentAsync("<button><span>Target</span></button>");
134            var span = await Page.QuerySelectorAsync("text=Target");
135            var task = span.WaitForElementStateAsync(ElementState.Disabled);
136            await GiveItAChanceToResolve(Page);
137            Assert.False(task.IsCompleted);
138            await span.EvaluateAsync("span => span.parentElement.disabled = true");
139            await task;
140        }
141
142        [PlaywrightTest("elementhandle-wait-for-element-state.spec.ts", "should wait for stable position")]
143        [Skip(SkipAttribute.Targets.Firefox)]
144        public async Task ShouldWaitForStablePosition()
145        {
146            await Page.GotoAsync(Server.Prefix + "/input/button.html");
147            var button = await Page.QuerySelectorAsync("button");
148            await Page.EvalOnSelectorAsync("button", @"button => {
149                button.style.transition = 'margin 10000ms linear 0s';
150                button.style.marginLeft = '20000px';
151            }");
152
153            var task = button.WaitForElementStateAsync(ElementState.Stable);
154            await GiveItAChanceToResolve(Page);
155            Assert.False(task.IsCompleted);
156            await button.EvaluateAsync("button => button.style.transition = ''");
157            await task;
158        }
159
160        private async Task GiveItAChanceToResolve(IPage page)
161        {
162            for (int i = 0; i < 5; i++)
163            {
164                await page.EvaluateAsync("() => new Promise(f => requestAnimationFrame(() => requestAnimationFrame(f)))");
165            }
166        }
167    }
168}
169
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 ShouldWaitForEnabledButton code on LambdaTest Cloud Grid

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