Skip to content

Commit b6cda7d

Browse files
authored
test: fix flaky assertion tests (#1184)
1 parent 8942234 commit b6cda7d

File tree

3 files changed

+32
-42
lines changed

3 files changed

+32
-42
lines changed

playwright/_impl/_sync_base.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,13 @@ def __str__(self) -> str:
9696
return self._impl_obj.__str__()
9797

9898
def _sync(self, api_name: str, coro: Awaitable) -> Any:
99+
__tracebackhide__ = True
99100
g_self = greenlet.getcurrent()
100101
task = self._loop.create_task(coro)
101102
setattr(task, "__pw_api_name__", api_name)
102103
setattr(task, "__pw_stack_trace__", traceback.extract_stack())
103104

104-
def callback(result: Any) -> None:
105-
g_self.switch()
106-
107-
task.add_done_callback(callback)
105+
task.add_done_callback(lambda _: g_self.switch())
108106
while not task.done():
109107
self._dispatcher_fiber.switch()
110108
asyncio._set_running_loop(self._loop)

tests/async/test_assertions.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,7 @@
2323

2424
async def test_assertions_page_to_have_title(page: Page, server: Server) -> None:
2525
await page.goto(server.EMPTY_PAGE)
26-
await page.set_content(
27-
"""
28-
<script>
29-
document.title = 'new title';
30-
setTimeout(() => {
31-
document.title = 'great title';
32-
}, 2000);
33-
</script>
34-
"""
35-
)
26+
await page.set_content("<title>new title</title>")
3627
await expect(page).to_have_title("new title")
3728
await expect(page).to_have_title(re.compile("new title"))
3829
with pytest.raises(AssertionError):
@@ -46,27 +37,32 @@ async def test_assertions_page_to_have_title(page: Page, server: Server) -> None
4637
with pytest.raises(AssertionError):
4738
await expect(page).not_to_have_title("new title", timeout=100)
4839
await expect(page).not_to_have_title("great title", timeout=100)
40+
await page.evaluate(
41+
"""
42+
setTimeout(() => {
43+
document.title = 'great title';
44+
}, 2000);
45+
"""
46+
)
4947
await expect(page).to_have_title("great title")
5048
await expect(page).to_have_title(re.compile("great title"))
5149

5250

5351
async def test_assertions_page_to_have_url(page: Page, server: Server) -> None:
5452
await page.goto(server.EMPTY_PAGE)
55-
await page.set_content(
56-
"""
57-
<script>
58-
setTimeout(() => {
59-
window.location = window.location.origin + '/grid.html';
60-
}, 2000);
61-
</script>
62-
"""
63-
)
6453
await expect(page).to_have_url(server.EMPTY_PAGE)
6554
await expect(page).to_have_url(re.compile(r".*/empty\.html"))
6655
with pytest.raises(AssertionError):
6756
await expect(page).to_have_url("nooooo", timeout=100)
6857
with pytest.raises(AssertionError):
6958
await expect(page).to_have_url(re.compile("not-the-url"), timeout=100)
59+
await page.evaluate(
60+
"""
61+
setTimeout(() => {
62+
window.location = window.location.origin + '/grid.html';
63+
}, 2000);
64+
"""
65+
)
7066
await expect(page).to_have_url(server.PREFIX + "/grid.html")
7167
await expect(page).not_to_have_url(server.EMPTY_PAGE, timeout=100)
7268
with pytest.raises(AssertionError):

tests/sync/test_assertions.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,7 @@
2323

2424
def test_assertions_page_to_have_title(page: Page, server: Server) -> None:
2525
page.goto(server.EMPTY_PAGE)
26-
page.set_content(
27-
"""
28-
<script>
29-
document.title = 'new title';
30-
setTimeout(() => {
31-
document.title = 'great title';
32-
}, 2000);
33-
</script>
34-
"""
35-
)
26+
page.set_content("<title>new title</title>")
3627
expect(page).to_have_title("new title")
3728
expect(page).to_have_title(re.compile("new title"))
3829
with pytest.raises(AssertionError):
@@ -44,27 +35,32 @@ def test_assertions_page_to_have_title(page: Page, server: Server) -> None:
4435
with pytest.raises(AssertionError):
4536
expect(page).not_to_have_title("new title", timeout=100)
4637
expect(page).not_to_have_title("great title", timeout=100)
38+
page.evaluate(
39+
"""
40+
setTimeout(() => {
41+
document.title = 'great title';
42+
}, 2000);
43+
"""
44+
)
4745
expect(page).to_have_title("great title")
4846
expect(page).to_have_title(re.compile("great title"))
4947

5048

5149
def test_assertions_page_to_have_url(page: Page, server: Server) -> None:
5250
page.goto(server.EMPTY_PAGE)
53-
page.set_content(
54-
"""
55-
<script>
56-
setTimeout(() => {
57-
window.location = window.location.origin + '/grid.html';
58-
}, 2000);
59-
</script>
60-
"""
61-
)
6251
expect(page).to_have_url(server.EMPTY_PAGE)
6352
expect(page).to_have_url(re.compile(r".*/empty\.html"))
6453
with pytest.raises(AssertionError):
6554
expect(page).to_have_url("nooooo", timeout=100)
6655
with pytest.raises(AssertionError):
6756
expect(page).to_have_url(re.compile("not-the-url"), timeout=100)
57+
page.evaluate(
58+
"""
59+
setTimeout(() => {
60+
window.location = window.location.origin + '/grid.html';
61+
}, 2000);
62+
"""
63+
)
6864
expect(page).to_have_url(server.PREFIX + "/grid.html")
6965
expect(page).not_to_have_url(server.EMPTY_PAGE, timeout=100)
7066
with pytest.raises(AssertionError):

0 commit comments

Comments
 (0)