-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy patherror.py
304 lines (259 loc) · 8.63 KB
/
error.py
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import asyncio
import pytest
from webdriver.bidi.error import UnknownErrorException
from . import navigate_and_assert
pytestmark = pytest.mark.asyncio
NAVIGATION_STARTED_EVENT = "browsingContext.navigationStarted"
USER_PROMPT_OPENED_EVENT = "browsingContext.userPromptOpened"
@pytest.mark.parametrize(
"url",
[
"thisprotocoldoesnotexist://",
"https://doesnotexist.localhost/",
"https://localhost:0",
],
ids=[
"protocol",
"host",
"port",
],
)
async def test_invalid_address(bidi_session, new_tab, url):
await navigate_and_assert(bidi_session, new_tab, url, expected_error=True)
async def test_with_csp_meta_tag(
bidi_session,
inline,
new_tab,
):
same_origin_url = inline("<div>foo</div>")
cross_origin_url = inline("<div>bar</div>", domain="alt")
page_url = inline(
f"""
<!DOCTYPE html>
<html>
<head>
<meta
http-equiv="Content-Security-Policy"
content="default-src 'self'" />
</head>
<body><iframe src="{same_origin_url}"></iframe></body>
</html>
"""
)
await bidi_session.browsing_context.navigate(
context=new_tab["context"], url=page_url, wait="complete"
)
contexts = await bidi_session.browsing_context.get_tree(root=new_tab["context"])
iframe_context = contexts[0]["children"][0]["context"]
# Make sure that cross-origin navigation in iframe failed.
with pytest.raises(UnknownErrorException):
await bidi_session.browsing_context.navigate(
context=iframe_context, url=cross_origin_url, wait="complete"
)
@pytest.mark.parametrize(
"header",
[
"Content-Security-Policy, default-src 'self'",
"Cross-Origin-Embedder-Policy, require-corp",
],
)
async def test_with_content_blocking_header_in_top_context(
bidi_session,
subscribe_events,
inline,
new_tab,
wait_for_event,
wait_for_future_safe,
header,
):
same_origin_url = inline("<div>foo</div>")
cross_origin_url = inline("<div>bar</div>", domain="alt")
page_url = inline(
f"""<iframe src={same_origin_url}></iframe>""",
parameters={"pipe": f"header({header})"},
)
await bidi_session.browsing_context.navigate(
context=new_tab["context"], url=page_url, wait="complete"
)
contexts = await bidi_session.browsing_context.get_tree(root=new_tab["context"])
iframe_context = contexts[0]["children"][0]["context"]
# Make sure that cross-origin navigation in iframe failed.
with pytest.raises(UnknownErrorException):
await bidi_session.browsing_context.navigate(
context=iframe_context, url=cross_origin_url, wait="complete"
)
@pytest.mark.parametrize(
"header_value",
[
"SAMEORIGIN",
"DENY",
],
)
async def test_with_x_frame_options_header(
bidi_session,
subscribe_events,
inline,
new_tab,
wait_for_event,
wait_for_future_safe,
header_value,
):
iframe_url_without_header = inline("<div>bar</div>")
iframe_url_with_header = inline(
"<div>foo</div>",
parameters={"pipe": f"header(X-Frame-Options, {header_value})"},
)
page_url = inline(
f"""<iframe src={iframe_url_without_header}></iframe>""", domain="alt"
)
await bidi_session.browsing_context.navigate(
context=new_tab["context"], url=page_url, wait="complete"
)
contexts = await bidi_session.browsing_context.get_tree(root=new_tab["context"])
iframe_context = contexts[0]["children"][0]["context"]
# Make sure that cross-origin navigation in iframe failed.
with pytest.raises(UnknownErrorException):
await bidi_session.browsing_context.navigate(
context=iframe_context, url=iframe_url_with_header, wait="complete"
)
async def test_with_new_navigation(
bidi_session,
subscribe_events,
inline,
url,
new_tab,
wait_for_event,
wait_for_future_safe,
):
slow_page_url = url(
"/webdriver/tests/bidi/browsing_context/support/empty.html?pipe=trickle(d10)"
)
await subscribe_events(events=[NAVIGATION_STARTED_EVENT])
on_navigation_started = wait_for_event(NAVIGATION_STARTED_EVENT)
task = asyncio.ensure_future(
bidi_session.browsing_context.navigate(
context=new_tab["context"], url=slow_page_url, wait="complete"
)
)
await wait_for_future_safe(on_navigation_started)
second_url = inline("<div>foo</div>")
# Trigger the second navigation which should fail the first one.
await bidi_session.browsing_context.navigate(
context=new_tab["context"], url=second_url, wait="none"
)
# Make sure that the first navigation failed.
with pytest.raises(UnknownErrorException):
await task
async def test_with_new_navigation_inside_page(
bidi_session,
subscribe_events,
inline,
new_tab,
wait_for_event,
wait_for_future_safe,
):
second_url = inline("<div>foo</div>")
slow_page_url = inline(
f"""
<!DOCTYPE html>
<html>
<body>
<img src="/webdriver/tests/bidi/browsing_context/support/empty.svg?pipe=trickle(d10)" />
<script>
location.href = "{second_url}"
</script>
<img src="/webdriver/tests/bidi/browsing_context/support/empty.svg?pipe=trickle(d10)" />
</body>
</html>
"""
)
# Make sure that the navigation failed.
with pytest.raises(UnknownErrorException):
await bidi_session.browsing_context.navigate(
context=new_tab["context"], url=slow_page_url, wait="complete"
)
@pytest.mark.parametrize("type_hint", ["tab", "window"])
async def test_close_context(
bidi_session,
url,
subscribe_events,
wait_for_event,
wait_for_future_safe,
type_hint,
):
await subscribe_events(events=[NAVIGATION_STARTED_EVENT])
new_context = await bidi_session.browsing_context.create(type_hint=type_hint)
slow_page_url = url(
"/webdriver/tests/bidi/browsing_context/support/empty.html?pipe=trickle(d10)"
)
on_navigation_started = wait_for_event(NAVIGATION_STARTED_EVENT)
task = asyncio.ensure_future(
bidi_session.browsing_context.navigate(
context=new_context["context"], url=slow_page_url, wait="complete"
)
)
await wait_for_future_safe(on_navigation_started)
await bidi_session.browsing_context.close(context=new_context["context"])
# Make sure that the navigation failed.
with pytest.raises(UnknownErrorException):
await task
async def test_close_iframe(
bidi_session,
subscribe_events,
inline,
url,
new_tab,
wait_for_event,
wait_for_future_safe,
):
iframe_url = inline("<div>foo</div>")
page_url = inline(f"<iframe src={iframe_url}></iframe")
await bidi_session.browsing_context.navigate(
context=new_tab["context"], url=page_url, wait="complete"
)
contexts = await bidi_session.browsing_context.get_tree(root=new_tab["context"])
iframe_context = contexts[0]["children"][0]["context"]
slow_page_url = url(
"/webdriver/tests/bidi/browsing_context/support/empty.html?pipe=trickle(d10)"
)
await subscribe_events(events=[NAVIGATION_STARTED_EVENT])
on_navigation_started = wait_for_event(NAVIGATION_STARTED_EVENT)
# Navigate in the iframe.
task = asyncio.ensure_future(
bidi_session.browsing_context.navigate(
context=iframe_context, url=slow_page_url, wait="complete"
)
)
await wait_for_future_safe(on_navigation_started)
# Reload the top context to destroy the iframe.
await bidi_session.browsing_context.reload(context=new_tab["context"], wait="none")
# Make sure that the iframe navigation failed.
with pytest.raises(UnknownErrorException):
await task
@pytest.mark.capabilities({"unhandledPromptBehavior": {"beforeUnload": "ignore"}})
async def test_beforeunload_rejected(
bidi_session,
new_tab,
inline,
setup_beforeunload_page,
subscribe_events,
wait_for_event,
wait_for_future_safe,
):
await subscribe_events(events=[USER_PROMPT_OPENED_EVENT])
await setup_beforeunload_page(new_tab)
url_after = inline("<div>foo</div>")
on_prompt_opened = wait_for_event(USER_PROMPT_OPENED_EVENT)
task = asyncio.ensure_future(
bidi_session.browsing_context.navigate(
context=new_tab["context"], url=url_after, wait="complete"
)
)
# Wait for the prompt to open.
await wait_for_future_safe(on_prompt_opened)
# Stay on the page to fail the started navigation.
await bidi_session.browsing_context.handle_user_prompt(
context=new_tab["context"], accept=False
)
with pytest.raises(UnknownErrorException):
await task