forked from microsoft/playwright-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_browsercontext_events.py
More file actions
198 lines (165 loc) · 6.26 KB
/
test_browsercontext_events.py
File metadata and controls
198 lines (165 loc) · 6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import asyncio
import pytest
from playwright.sync_api import Page
from ..server import HttpRequestWithPostBody, Server
async def test_console_event_should_work(page: Page) -> None:
[message, _] = await asyncio.gather(
page.context.wait_for_event("console"),
page.evaluate("() => console.log('hello')"),
)
assert message.text == "hello"
assert message.page == page
async def test_console_event_should_work_in_popup(page: Page) -> None:
[message, popup, _] = await asyncio.gather(
page.context.wait_for_event("console"),
page.wait_for_event("popup"),
page.evaluate(
"""() => {
const win = window.open('');
win.console.log('hello');
}"""
),
)
assert message.text == "hello"
assert message.page == popup
# console message from javascript: url is not reported at all
@pytest.mark.skip_browser("firefox")
async def test_console_event_should_work_in_popup_2(
page: Page, browser_name: str
) -> None:
[message, popup, _] = await asyncio.gather(
page.context.wait_for_event("console", lambda msg: msg.type == "log"),
page.context.wait_for_event("page"),
page.evaluate(
"""async () => {
const win = window.open('javascript:console.log("hello")');
await new Promise(f => setTimeout(f, 0));
win.close();
}"""
),
)
assert message.text == "hello"
assert message.page == popup
# console message from javascript: url is not reported at all
@pytest.mark.skip_browser("firefox")
async def test_console_event_should_work_in_immediately_closed_popup(
page: Page, browser_name: str
) -> None:
[message, popup, _] = await asyncio.gather(
page.context.wait_for_event("console"),
page.wait_for_event("popup"),
page.evaluate(
"""async () => {
const win = window.open();
win.console.log('hello');
win.close();
}"""
),
)
assert message.text == "hello"
assert message.page == popup
async def test_dialog_event_should_work1(page: Page) -> None:
prompt_task = None
async def open_dialog() -> None:
nonlocal prompt_task
prompt_task = asyncio.create_task(page.evaluate("() => prompt('hey?')"))
[dialog1, dialog2, _] = await asyncio.gather(
page.context.wait_for_event("dialog"),
page.wait_for_event("dialog"),
open_dialog(),
)
assert dialog1 == dialog2
assert dialog1.message == "hey?"
assert dialog1.page == page
await dialog1.accept("hello")
assert await prompt_task == "hello"
async def test_dialog_event_should_work_in_popup(page: Page) -> None:
prompt_task = None
async def open_dialog() -> None:
nonlocal prompt_task
prompt_task = asyncio.create_task(
page.evaluate("() => window.open('').prompt('hey?')")
)
[dialog, popup, _] = await asyncio.gather(
page.context.wait_for_event("dialog"),
page.wait_for_event("popup"),
open_dialog(),
)
assert dialog.message == "hey?"
assert dialog.page == popup
await dialog.accept("hello")
assert await prompt_task == "hello"
# console message from javascript: url is not reported at all
@pytest.mark.skip_browser("firefox")
async def test_dialog_event_should_work_in_popup_2(
page: Page, browser_name: str
) -> None:
promise = asyncio.create_task(
page.evaluate("() => window.open('javascript:prompt(\"hey?\")')")
)
dialog = await page.context.wait_for_event("dialog")
assert dialog.message == "hey?"
assert dialog.page is None
await dialog.accept("hello")
await promise
# console message from javascript: url is not reported at all
@pytest.mark.skip_browser("firefox")
async def test_dialog_event_should_work_in_immdiately_closed_popup(page: Page) -> None:
[message, popup, _] = await asyncio.gather(
page.context.wait_for_event("console"),
page.wait_for_event("popup"),
page.evaluate(
"""() => {
const win = window.open();
win.console.log('hello');
win.close();
}"""
),
)
assert message.text == "hello"
assert message.page == popup
async def test_dialog_event_should_work_with_inline_script_tag(
page: Page, server: Server
) -> None:
def handle_route(request: HttpRequestWithPostBody) -> None:
request.setHeader("content-type", "text/html")
request.write(b"""<script>window.result = prompt('hey?')</script>""")
request.finish()
server.set_route("/popup.html", handle_route)
await page.goto(server.EMPTY_PAGE)
await page.set_content("<a href='popup.html' target=_blank>Click me</a>")
promise = asyncio.create_task(page.click("a"))
[dialog, popup] = await asyncio.gather(
page.context.wait_for_event("dialog"),
page.wait_for_event("popup"),
)
assert dialog.message == "hey?"
assert dialog.page == popup
await dialog.accept("hello")
await promise
await popup.evaluate("window.result") == "hello"
async def test_console_event_should_work_with_context_manager(page: Page) -> None:
async with page.context.expect_console_message() as cm_info:
await page.evaluate("() => console.log('hello')")
message = await cm_info.value
assert message.text == "hello"
assert message.page == page
async def test_page_error_event_should_work(page: Page) -> None:
async with page.context.expect_event("weberror") as page_error_info:
await page.set_content('<script>throw new Error("boom")</script>')
page_error = await page_error_info.value
assert page_error.page == page
assert "boom" in page_error.error.stack