How to use give_it_a_chance_to_resolve 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 asyncio
16
17import pytest
18
19from playwright.async_api import Error
20
21
22async def give_it_a_chance_to_resolve(page):
23    for i in range(5):
24        await page.evaluate(
25            "() => new Promise(f => requestAnimationFrame(() => requestAnimationFrame(f)))"
26        )
27
28
29async def wait_for_state(div, state, done):
30    await div.wait_for_element_state(state)
31    done[0] = True
32
33
34async def wait_for_state_to_throw(div, state):
35    with pytest.raises(Error) as exc_info:
36        await div.wait_for_element_state(state)
37    return exc_info
38
39
40async def test_should_wait_for_visible(page):
41    await page.set_content('<div style="display:none">content</div>')
42    div = await page.query_selector("div")
43    done = [False]
44    promise = asyncio.create_task(wait_for_state(div, "visible", done))
45    await give_it_a_chance_to_resolve(page)
46    assert done[0] is False
47    await div.evaluate('div => div.style.display = "block"')
48    await promise
49
50
51async def test_should_wait_for_already_visible(page):
52    await page.set_content("<div>content</div>")
53    div = await page.query_selector("div")
54    await div.wait_for_element_state("visible")
55
56
57async def test_should_timeout_waiting_for_visible(page):
58    await page.set_content('<div style="display:none">content</div>')
59    div = await page.query_selector("div")
60    with pytest.raises(Error) as exc_info:
61        await div.wait_for_element_state("visible", timeout=1000)
62    assert "Timeout 1000ms exceeded" in exc_info.value.message
63
64
65async def test_should_throw_waiting_for_visible_when_detached(page):
66    await page.set_content('<div style="display:none">content</div>')
67    div = await page.query_selector("div")
68    promise = asyncio.create_task(wait_for_state_to_throw(div, "visible"))
69    await div.evaluate("div => div.remove()")
70    exc_info = await promise
71    assert "Element is not attached to the DOM" in exc_info.value.message
72
73
74async def test_should_wait_for_hidden(page):
75    await page.set_content("<div>content</div>")
76    div = await page.query_selector("div")
77    done = [False]
78    promise = asyncio.create_task(wait_for_state(div, "hidden", done))
79    await give_it_a_chance_to_resolve(page)
80    assert done[0] is False
81    await div.evaluate('div => div.style.display = "none"')
82    await promise
83
84
85async def test_should_wait_for_already_hidden(page):
86    await page.set_content("<div></div>")
87    div = await page.query_selector("div")
88    await div.wait_for_element_state("hidden")
89
90
91async def test_should_wait_for_hidden_when_detached(page):
92    await page.set_content("<div>content</div>")
93    div = await page.query_selector("div")
94    done = [False]
95    promise = asyncio.create_task(wait_for_state(div, "hidden", done))
96    await give_it_a_chance_to_resolve(page)
97    assert done[0] is False
98    await div.evaluate("div => div.remove()")
99    await promise
100
101
102async def test_should_wait_for_enabled_button(page, server):
103    await page.set_content("<button disabled><span>Target</span></button>")
104    span = await page.query_selector("text=Target")
105    done = [False]
106    promise = asyncio.create_task(wait_for_state(span, "enabled", done))
107    await give_it_a_chance_to_resolve(page)
108    assert done[0] is False
109    await span.evaluate("span => span.parentElement.disabled = false")
110    await promise
111
112
113async def test_should_throw_waiting_for_enabled_when_detached(page):
114    await page.set_content("<button disabled>Target</button>")
115    button = await page.query_selector("button")
116    promise = asyncio.create_task(wait_for_state_to_throw(button, "enabled"))
117    await button.evaluate("button => button.remove()")
118    exc_info = await promise
119    assert "Element is not attached to the DOM" in exc_info.value.message
120
121
122async def test_should_wait_for_disabled_button(page):
123    await page.set_content("<button><span>Target</span></button>")
124    span = await page.query_selector("text=Target")
125    done = [False]
126    promise = asyncio.create_task(wait_for_state(span, "disabled", done))
127    await give_it_a_chance_to_resolve(page)
128    assert done[0] is False
129    await span.evaluate("span => span.parentElement.disabled = true")
130    await promise
131
132
133async def test_should_wait_for_editable_input(page, server):
134    await page.set_content("<input readonly>")
135    input = await page.query_selector("input")
136    done = [False]
137    promise = asyncio.create_task(wait_for_state(input, "editable", done))
138    await give_it_a_chance_to_resolve(page)
139    assert done[0] is False
140    await input.evaluate("input => input.readOnly = false")
141    await promise
142
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)