How to use test_should_dismiss_the_confirm_prompt 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_dialog.py

Source: test_dialog.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 Dialog, Page
18
19
20async def test_should_fire(page: Page, server):
21    result = []
22
23    async def on_dialog(dialog: Dialog):
24        result.append(True)
25        assert dialog.type == "alert"
26        assert dialog.default_value == ""
27        assert dialog.message == "yo"
28        await dialog.accept()
29
30    page.on("dialog", on_dialog)
31    await page.evaluate("alert('yo')")
32    assert result
33
34
35async def test_should_allow_accepting_prompts(page: Page, server):
36    result = []
37
38    async def on_dialog(dialog: Dialog):
39        result.append(True)
40        assert dialog.type == "prompt"
41        assert dialog.default_value == "yes."
42        assert dialog.message == "question?"
43        await dialog.accept("answer!")
44
45    page.on("dialog", on_dialog)
46    assert await page.evaluate("prompt('question?', 'yes.')") == "answer!"
47    assert result
48
49
50async def test_should_dismiss_the_prompt(page: Page, server):
51    result = []
52
53    async def on_dialog(dialog: Dialog):
54        result.append(True)
55        await dialog.dismiss()
56
57    page.on("dialog", on_dialog)
58    assert await page.evaluate("prompt('question?')") is None
59    assert result
60
61
62async def test_should_accept_the_confirm_prompt(page: Page, server):
63    result = []
64
65    async def on_dialog(dialog: Dialog):
66        result.append(True)
67        await dialog.accept()
68
69    page.on("dialog", on_dialog)
70    assert await page.evaluate("confirm('boolean?')") is True
71    assert result
72
73
74async def test_should_dismiss_the_confirm_prompt(page: Page, server):
75    result = []
76
77    async def on_dialog(dialog: Dialog):
78        result.append(True)
79        await dialog.dismiss()
80
81    page.on("dialog", on_dialog)
82    assert await page.evaluate("confirm('boolean?')") is False
83    assert result
84
85
86@pytest.mark.skip_browser("webkit")
87async def test_should_be_able_to_close_context_with_open_alert(browser):
88    context = await browser.new_context()
89    page = await context.new_page()
90    async with page.expect_event("dialog"):
91        await page.evaluate("() => setTimeout(() => alert('hello'), 0)", None)
92    await context.close()
93
94
95async def test_should_auto_dismiss_the_prompt_without_listeners(page):
96    result = await page.evaluate('() => prompt("question?")')
97    assert not result
98
99
100async def test_should_auto_dismiss_the_alert_without_listeners(page):
101    await page.set_content(
102        '<div onclick="window.alert(123); window._clicked=true">Click me</div>'
103    )
104    await page.click("div")
105    assert await page.evaluate('"window._clicked"')
106
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)