How to use test_should_throw_waiting_for_enabled_when_detached method in Playwright Python

Best Python code snippet using playwright-python

Run Playwright Python automation tests on LambdaTest cloud grid

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

test_element_handle_wait_for_element_state.py

Source: test_element_handle_wait_for_element_state.py Github

copy
1# Copyright (c) Microsoft Corporation.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import pytest
16
17from playwright.async_api import Error
18
19
20def test_should_wait_for_visible(page):
21    page.set_content('<div id=div style="display:none">content</div>')
22    div = page.query_selector("div")
23    page.evaluate('setTimeout(() => div.style.display = "block", 250)')
24    assert div.is_visible() is False
25    div.wait_for_element_state("visible")
26    assert div.is_visible()
27
28
29def test_should_wait_for_already_visible(page):
30    page.set_content("<div>content</div>")
31    div = page.query_selector("div")
32    div.wait_for_element_state("visible")
33
34
35def test_should_timeout_waiting_for_visible(page):
36    page.set_content('<div style="display:none">content</div>')
37    div = page.query_selector("div")
38    with pytest.raises(Error) as exc_info:
39        div.wait_for_element_state("visible", timeout=1000)
40    assert "Timeout 1000ms exceeded" in exc_info.value.message
41
42
43def test_should_throw_waiting_for_visible_when_detached(page):
44    page.set_content('<div id=div style="display:none">content</div>')
45    div = page.query_selector("div")
46    page.evaluate("setTimeout(() => div.remove(), 250)")
47    with pytest.raises(Error) as exc_info:
48        div.wait_for_element_state("visible")
49    assert "Element is not attached to the DOM" in exc_info.value.message
50
51
52def test_should_wait_for_hidden(page):
53    page.set_content("<div id=div>content</div>")
54    div = page.query_selector("div")
55    page.evaluate('setTimeout(() => div.style.display = "none", 250)')
56    assert div.is_hidden() is False
57    div.wait_for_element_state("hidden")
58    assert div.is_hidden()
59
60
61def test_should_wait_for_already_hidden(page):
62    page.set_content("<div></div>")
63    div = page.query_selector("div")
64    div.wait_for_element_state("hidden")
65
66
67def test_should_wait_for_hidden_when_detached(page):
68    page.set_content("<div id=div>content</div>")
69    div = page.query_selector("div")
70    page.evaluate("setTimeout(() => div.remove(), 250)")
71    div.wait_for_element_state("hidden")
72    assert div.is_hidden()
73
74
75def test_should_wait_for_enabled_button(page, server):
76    page.set_content("<button id=button disabled><span>Target</span></button>")
77    span = page.query_selector("text=Target")
78    page.evaluate("setTimeout(() => button.disabled = false, 250)")
79    assert span.is_enabled() is False
80    span.wait_for_element_state("enabled")
81    assert span.is_enabled()
82
83
84def test_should_throw_waiting_for_enabled_when_detached(page):
85    page.set_content("<button id=button disabled>Target</button>")
86    button = page.query_selector("button")
87    page.evaluate("setTimeout(() => button.remove(), 250)")
88    with pytest.raises(Error) as exc_info:
89        button.wait_for_element_state("enabled")
90    assert "Element is not attached to the DOM" in exc_info.value.message
91
92
93def test_should_wait_for_disabled_button(page):
94    page.set_content("<button id=button><span>Target</span></button>")
95    span = page.query_selector("text=Target")
96    assert span.is_disabled() is False
97    page.evaluate("setTimeout(() => button.disabled = true, 250)")
98    span.wait_for_element_state("disabled")
99    assert span.is_disabled()
100
101
102def test_should_wait_for_editable_input(page, server):
103    page.set_content("<input id=input readonly>")
104    input = page.query_selector("input")
105    page.evaluate("setTimeout(() => input.readOnly = false, 250)")
106    assert input.is_editable() is False
107    input.wait_for_element_state("editable")
108    assert input.is_editable()
109
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

Run Python Tests on LambdaTest Cloud Grid

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