File: test_unroute_behavior.py

package info (click to toggle)
python-playwright 1.55.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,728 kB
  • sloc: python: 53,655; javascript: 383; sh: 216; makefile: 6
file content (509 lines) | stat: -rw-r--r-- 14,451 bytes parent folder | download
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# 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 re

from playwright.async_api import BrowserContext, Error, Page, Route
from tests.server import Server
from tests.utils import must


async def test_context_unroute_should_not_wait_for_pending_handlers_to_complete(
    page: Page, context: BrowserContext, server: Server
) -> None:
    second_handler_called = False

    async def _handler1(route: Route) -> None:
        nonlocal second_handler_called
        second_handler_called = True
        await route.continue_()

    await context.route(
        re.compile(".*"),
        _handler1,
    )
    route_future: "asyncio.Future[Route]" = asyncio.Future()
    route_barrier_future: "asyncio.Future[None]" = asyncio.Future()

    async def _handler2(route: Route) -> None:
        route_future.set_result(route)
        await route_barrier_future
        await route.fallback()

    await context.route(
        re.compile(".*"),
        _handler2,
    )
    navigation_task = asyncio.create_task(page.goto(server.EMPTY_PAGE))
    await route_future
    await context.unroute(
        re.compile(".*"),
        _handler2,
    )
    route_barrier_future.set_result(None)
    await navigation_task
    assert second_handler_called


async def test_context_unroute_all_removes_all_handlers(
    page: Page, context: BrowserContext, server: Server
) -> None:
    await context.route(
        "**/*",
        lambda route: route.abort(),
    )
    await context.route(
        "**/empty.html",
        lambda route: route.abort(),
    )
    await context.unroute_all()
    await page.goto(server.EMPTY_PAGE)


async def test_context_unroute_all_should_not_wait_for_pending_handlers_to_complete(
    page: Page, context: BrowserContext, server: Server
) -> None:
    second_handler_called = False

    async def _handler1(route: Route) -> None:
        nonlocal second_handler_called
        second_handler_called = True
        await route.abort()

    await context.route(
        re.compile(".*"),
        _handler1,
    )
    route_future: "asyncio.Future[Route]" = asyncio.Future()
    route_barrier_future: "asyncio.Future[None]" = asyncio.Future()

    async def _handler2(route: Route) -> None:
        route_future.set_result(route)
        await route_barrier_future
        await route.fallback()

    await context.route(
        re.compile(".*"),
        _handler2,
    )
    navigation_task = asyncio.create_task(page.goto(server.EMPTY_PAGE))
    await route_future
    did_unroute = False

    async def _unroute_promise() -> None:
        nonlocal did_unroute
        await context.unroute_all(behavior="wait")
        did_unroute = True

    unroute_task = asyncio.create_task(_unroute_promise())
    await asyncio.sleep(0.5)
    assert did_unroute is False
    route_barrier_future.set_result(None)
    await unroute_task
    assert did_unroute
    await navigation_task
    assert second_handler_called is False


async def test_context_unroute_all_should_not_wait_for_pending_handlers_to_complete_if_behavior_is_ignore_errors(
    page: Page, context: BrowserContext, server: Server
) -> None:
    second_handler_called = False

    async def _handler1(route: Route) -> None:
        nonlocal second_handler_called
        second_handler_called = True
        await route.abort()

    await context.route(
        re.compile(".*"),
        _handler1,
    )
    route_future: "asyncio.Future[Route]" = asyncio.Future()
    route_barrier_future: "asyncio.Future[None]" = asyncio.Future()

    async def _handler2(route: Route) -> None:
        route_future.set_result(route)
        await route_barrier_future
        raise Exception("Handler error")

    await context.route(
        re.compile(".*"),
        _handler2,
    )
    navigation_task = asyncio.create_task(page.goto(server.EMPTY_PAGE))
    await route_future
    did_unroute = False

    async def _unroute_promise() -> None:
        await context.unroute_all(behavior="ignoreErrors")
        nonlocal did_unroute
        did_unroute = True

    unroute_task = asyncio.create_task(_unroute_promise())
    await asyncio.sleep(0.5)
    await unroute_task
    assert did_unroute
    route_barrier_future.set_result(None)
    try:
        await navigation_task
    except Error:
        pass
    # The error in the unrouted handler should be silently caught and remaining handler called.
    assert not second_handler_called


async def test_page_close_should_not_wait_for_active_route_handlers_on_the_owning_context(
    page: Page, context: BrowserContext, server: Server
) -> None:
    route_future: "asyncio.Future[Route]" = asyncio.Future()
    await context.route(
        re.compile(".*"),
        lambda route: route_future.set_result(route),
    )
    await page.route(
        re.compile(".*"),
        lambda route: route.fallback(),
    )

    async def _goto_ignore_exceptions() -> None:
        try:
            await page.goto(server.EMPTY_PAGE)
        except Error:
            pass

    asyncio.create_task(_goto_ignore_exceptions())
    await route_future
    await page.close()


async def test_context_close_should_not_wait_for_active_route_handlers_on_the_owned_pages(
    page: Page, context: BrowserContext, server: Server
) -> None:
    route_future: "asyncio.Future[Route]" = asyncio.Future()
    await page.route(
        re.compile(".*"),
        lambda route: route_future.set_result(route),
    )
    await page.route(re.compile(".*"), lambda route: route.fallback())

    async def _goto_ignore_exceptions() -> None:
        try:
            await page.goto(server.EMPTY_PAGE)
        except Error:
            pass

    asyncio.create_task(_goto_ignore_exceptions())
    await route_future
    await context.close()


async def test_page_unroute_should_not_wait_for_pending_handlers_to_complete(
    page: Page, server: Server
) -> None:
    second_handler_called = False

    async def _handler1(route: Route) -> None:
        nonlocal second_handler_called
        second_handler_called = True
        await route.continue_()

    await page.route(
        re.compile(".*"),
        _handler1,
    )
    route_future: "asyncio.Future[Route]" = asyncio.Future()
    route_barrier_future: "asyncio.Future[None]" = asyncio.Future()

    async def _handler2(route: Route) -> None:
        route_future.set_result(route)
        await route_barrier_future
        await route.fallback()

    await page.route(
        re.compile(".*"),
        _handler2,
    )
    navigation_task = asyncio.create_task(page.goto(server.EMPTY_PAGE))
    await route_future
    await page.unroute(
        re.compile(".*"),
        _handler2,
    )
    route_barrier_future.set_result(None)
    await navigation_task
    assert second_handler_called


async def test_page_unroute_all_removes_all_routes(page: Page, server: Server) -> None:
    await page.route(
        "**/*",
        lambda route: route.abort(),
    )
    await page.route(
        "**/empty.html",
        lambda route: route.abort(),
    )
    await page.unroute_all()
    response = must(await page.goto(server.EMPTY_PAGE))
    assert response.ok


async def test_page_unroute_should_wait_for_pending_handlers_to_complete(
    page: Page, server: Server
) -> None:
    second_handler_called = False

    async def _handler1(route: Route) -> None:
        nonlocal second_handler_called
        second_handler_called = True
        await route.abort()

    await page.route(
        "**/*",
        _handler1,
    )
    route_future: "asyncio.Future[Route]" = asyncio.Future()
    route_barrier_future: "asyncio.Future[None]" = asyncio.Future()

    async def _handler2(route: Route) -> None:
        route_future.set_result(route)
        await route_barrier_future
        await route.fallback()

    await page.route(
        "**/*",
        _handler2,
    )
    navigation_task = asyncio.create_task(page.goto(server.EMPTY_PAGE))
    await route_future
    did_unroute = False

    async def _unroute_promise() -> None:
        await page.unroute_all(behavior="wait")
        nonlocal did_unroute
        did_unroute = True

    unroute_task = asyncio.create_task(_unroute_promise())
    await asyncio.sleep(0.5)
    assert did_unroute is False
    route_barrier_future.set_result(None)
    await unroute_task
    assert did_unroute
    await navigation_task
    assert second_handler_called is False


async def test_page_unroute_all_should_not_wait_for_pending_handlers_to_complete_if_behavior_is_ignore_errors(
    page: Page, server: Server
) -> None:
    second_handler_called = False

    async def _handler1(route: Route) -> None:
        nonlocal second_handler_called
        second_handler_called = True
        await route.abort()

    await page.route(re.compile(".*"), _handler1)
    route_future: "asyncio.Future[Route]" = asyncio.Future()
    route_barrier_future: "asyncio.Future[None]" = asyncio.Future()

    async def _handler2(route: Route) -> None:
        route_future.set_result(route)
        await route_barrier_future
        raise Exception("Handler error")

    await page.route(re.compile(".*"), _handler2)
    navigation_task = asyncio.create_task(page.goto(server.EMPTY_PAGE))
    await route_future
    did_unroute = False

    async def _unroute_promise() -> None:
        await page.unroute_all(behavior="ignoreErrors")
        nonlocal did_unroute
        did_unroute = True

    unroute_task = asyncio.create_task(_unroute_promise())
    await asyncio.sleep(0.5)
    await unroute_task
    assert did_unroute
    route_barrier_future.set_result(None)
    try:
        await navigation_task
    except Error:
        pass
    # The error in the unrouted handler should be silently caught.
    assert not second_handler_called


async def test_page_close_does_not_wait_for_active_route_handlers(
    page: Page, server: Server
) -> None:
    stalling_future: "asyncio.Future[None]" = asyncio.Future()
    second_handler_called = False

    def _handler1(route: Route) -> None:
        nonlocal second_handler_called
        second_handler_called = True

    await page.route(
        "**/*",
        _handler1,
    )
    route_future: "asyncio.Future[Route]" = asyncio.Future()

    async def _handler2(route: Route) -> None:
        route_future.set_result(route)
        await stalling_future

    await page.route(
        "**/*",
        _handler2,
    )

    async def _goto_ignore_exceptions() -> None:
        try:
            await page.goto(server.EMPTY_PAGE)
        except Error:
            pass

    asyncio.create_task(_goto_ignore_exceptions())
    await route_future
    await page.close()
    await asyncio.sleep(0.5)
    assert not second_handler_called
    stalling_future.cancel()


async def test_route_continue_should_not_throw_if_page_has_been_closed(
    page: Page, server: Server
) -> None:
    route_future: "asyncio.Future[Route]" = asyncio.Future()
    await page.route(
        re.compile(".*"),
        lambda route: route_future.set_result(route),
    )

    async def _goto_ignore_exceptions() -> None:
        try:
            await page.goto(server.EMPTY_PAGE)
        except Error:
            pass

    asyncio.create_task(_goto_ignore_exceptions())
    route = await route_future
    await page.close()
    # Should not throw.
    await route.continue_()


async def test_route_fallback_should_not_throw_if_page_has_been_closed(
    page: Page, server: Server
) -> None:
    route_future: "asyncio.Future[Route]" = asyncio.Future()
    await page.route(
        re.compile(".*"),
        lambda route: route_future.set_result(route),
    )

    async def _goto_ignore_exceptions() -> None:
        try:
            await page.goto(server.EMPTY_PAGE)
        except Error:
            pass

    asyncio.create_task(_goto_ignore_exceptions())
    route = await route_future
    await page.close()
    # Should not throw.
    await route.fallback()


async def test_route_fulfill_should_not_throw_if_page_has_been_closed(
    page: Page, server: Server
) -> None:
    route_future: "asyncio.Future[Route]" = asyncio.Future()
    await page.route(
        "**/*",
        lambda route: route_future.set_result(route),
    )

    async def _goto_ignore_exceptions() -> None:
        try:
            await page.goto(server.EMPTY_PAGE)
        except Error:
            pass

    asyncio.create_task(_goto_ignore_exceptions())
    route = await route_future
    await page.close()
    # Should not throw.
    await route.fulfill()


async def test_should_not_continue_requests_in_flight_page(
    page: Page, server: Server
) -> None:
    await page.goto(server.EMPTY_PAGE)

    route_future: "asyncio.Future[Route]" = asyncio.Future()

    async def handle(route: Route) -> None:
        route_future.set_result(route)
        await asyncio.sleep(3)
        await route.fulfill(status=200)

    async def _evaluate_ignore_exceptions() -> None:
        try:
            await page.evaluate("() => fetch('/')")
        except Error:
            pass

    await page.route(
        "**/*",
        handle,
    )

    asyncio.create_task(_evaluate_ignore_exceptions())
    await route_future
    await page.unroute_all(behavior="wait")


async def test_should_not_continue_requests_in_flight_context(
    page: Page, context: BrowserContext, server: Server
) -> None:
    await page.goto(server.EMPTY_PAGE)

    route_future: "asyncio.Future[Route]" = asyncio.Future()

    async def handle(route: Route) -> None:
        route_future.set_result(route)
        await asyncio.sleep(3)
        await route.fulfill(status=200)

    async def _evaluate_ignore_exceptions() -> None:
        try:
            await page.evaluate("() => fetch('/')")
        except Error:
            pass

    await context.route(
        "**/*",
        handle,
    )

    asyncio.create_task(_evaluate_ignore_exceptions())
    await route_future
    await context.unroute_all(behavior="wait")