MCPcopy Index your code
hub / github.com/microsoft/playwright-python / test_should_ignore_aborted_requests

Function test_should_ignore_aborted_requests

tests/async/test_har.py:757–806  ·  view source on GitHub ↗
(
    context_factory: Callable[[], Awaitable[BrowserContext]],
    server: Server,
    tmp_path: Path,
)

Source from the content-addressed store, hash-verified

755
756
757async def test_should_ignore_aborted_requests(
758 context_factory: Callable[[], Awaitable[BrowserContext]],
759 server: Server,
760 tmp_path: Path,
761) -> None:
762 path = tmp_path / "test.har"
763 server.set_route("/x", lambda request: request.loseConnection())
764 context1 = await context_factory()
765 await context1.route_from_har(har=path, update=True)
766 page1 = await context1.new_page()
767 await page1.goto(server.EMPTY_PAGE)
768 req_promise = asyncio.create_task(server.wait_for_request("/x"))
769 eval_task = asyncio.create_task(
770 page1.evaluate(
771 "url => fetch(url).catch(e => 'cancelled')", server.PREFIX + "/x"
772 )
773 )
774 await req_promise
775 req = await eval_task
776 assert req == "cancelled"
777 await context1.close()
778
779 server.reset()
780
781 def _handle_route(req: TestServerRequest) -> None:
782 req.setHeader("Content-Type", "text/plain")
783 req.write(b"test")
784 req.finish()
785
786 server.set_route("/x", _handle_route)
787 context2 = await context_factory()
788 await context2.route_from_har(path)
789 page2 = await context2.new_page()
790 await page2.goto(server.EMPTY_PAGE)
791 eval_task = asyncio.create_task(
792 page2.evaluate(
793 "url => fetch(url).catch(e => 'cancelled')", server.PREFIX + "/x"
794 )
795 )
796
797 async def _timeout() -> str:
798 await asyncio.sleep(1)
799 return "timeout"
800
801 done, _ = await asyncio.wait(
802 [eval_task, asyncio.create_task(_timeout())],
803 return_when=asyncio.FIRST_COMPLETED,
804 )
805 assert next(iter(done)).result() == "timeout"
806 eval_task.cancel()

Callers

nothing calls this directly

Calls 12

context_factoryFunction · 0.85
_timeoutFunction · 0.85
set_routeMethod · 0.80
wait_for_requestMethod · 0.80
resultMethod · 0.80
route_from_harMethod · 0.45
new_pageMethod · 0.45
gotoMethod · 0.45
evaluateMethod · 0.45
closeMethod · 0.45
resetMethod · 0.45
cancelMethod · 0.45

Tested by

no test coverage detected