Skip to content

Commit 4d8aeb3

Browse files
authored
tests: add missing dialog tests (#124)
1 parent f7c2dc1 commit 4d8aeb3

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

tests/async/test_dialog.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
15+
import asyncio
16+
from asyncio import Future
17+
18+
import pytest
19+
20+
from playwright.dialog import Dialog
21+
from playwright.page import Page
22+
23+
24+
async def test_should_fire(page: Page, server):
25+
on_dialog_actions_complete: Future[None] = asyncio.Future()
26+
27+
async def on_dialog(dialog: Dialog):
28+
assert dialog.type == "alert"
29+
assert dialog.defaultValue == ""
30+
assert dialog.message == "yo"
31+
on_dialog_actions_complete.set_result(await dialog.accept())
32+
33+
page.on("dialog", lambda dialog: asyncio.create_task(on_dialog(dialog)))
34+
await page.evaluate("alert('yo')")
35+
await on_dialog_actions_complete
36+
37+
38+
async def test_should_allow_accepting_prompts(page: Page, server):
39+
on_dialog_actions_complete: Future[None] = asyncio.Future()
40+
41+
async def on_dialog(dialog: Dialog):
42+
assert dialog.type == "prompt"
43+
assert dialog.defaultValue == "yes."
44+
assert dialog.message == "question?"
45+
on_dialog_actions_complete.set_result(await dialog.accept("answer!"))
46+
47+
page.on("dialog", lambda dialog: asyncio.create_task(on_dialog(dialog)))
48+
assert await page.evaluate("prompt('question?', 'yes.')") == "answer!"
49+
await on_dialog_actions_complete
50+
51+
52+
async def test_should_dismiss_the_prompt(page: Page, server):
53+
on_dialog_actions_complete: Future[None] = asyncio.Future()
54+
55+
async def on_dialog(dialog: Dialog):
56+
on_dialog_actions_complete.set_result(await dialog.dismiss())
57+
58+
page.on("dialog", lambda dialog: asyncio.create_task(on_dialog(dialog)))
59+
assert await page.evaluate("prompt('question?')") is None
60+
await on_dialog_actions_complete
61+
62+
63+
async def test_should_accept_the_confirm_prompt(page: Page, server):
64+
on_dialog_actions_complete: Future[None] = asyncio.Future()
65+
66+
async def on_dialog(dialog: Dialog):
67+
on_dialog_actions_complete.set_result(await dialog.accept())
68+
69+
page.on("dialog", lambda dialog: asyncio.create_task(on_dialog(dialog)))
70+
assert await page.evaluate("confirm('boolean?')") is True
71+
await on_dialog_actions_complete
72+
73+
74+
async def test_should_dismiss_the_confirm_prompt(page: Page, server):
75+
on_dialog_actions_complete: Future[None] = asyncio.Future()
76+
77+
async def on_dialog(dialog: Dialog):
78+
on_dialog_actions_complete.set_result(await dialog.dismiss())
79+
80+
page.on("dialog", lambda dialog: asyncio.create_task(on_dialog(dialog)))
81+
assert await page.evaluate("confirm('boolean?')") is False
82+
await on_dialog_actions_complete
83+
84+
85+
# TODO: Logger support not yet here
86+
# // it.fail(CHANNEL)('should log prompt actions', async({browser}) => {
87+
# // const messages = [];
88+
# // const context = await browser.newContext({
89+
# // logger: {
90+
# // isEnabled: () => true,
91+
# // log: (name, severity, message) => messages.push(message),
92+
# // }
93+
# // });
94+
# // const page = await context.newPage();
95+
# // const promise = page.evaluate(() => confirm('01234567890123456789012345678901234567890123456789012345678901234567890123456789'));
96+
# // const dialog = await page.waitForEvent('dialog');
97+
# // expect(messages.join()).toContain('confirm "0123456789012345678901234567890123456789012345678…" was shown');
98+
# // await dialog.accept('123');
99+
# // await promise;
100+
# // expect(messages.join()).toContain('confirm "0123456789012345678901234567890123456789012345678…" was accepted');
101+
# // await context.close();
102+
# // });
103+
104+
105+
@pytest.mark.skip_browser("webkit")
106+
async def test_should_be_able_to_close_context_with_open_alert(browser):
107+
context = await browser.newContext()
108+
page = await context.newPage()
109+
alertFuture = asyncio.create_task(page.waitForEvent("dialog"))
110+
await page.evaluate("() => setTimeout(() => alert('hello'), 0)", None)
111+
await alertFuture
112+
await context.close()

0 commit comments

Comments
 (0)