|
| 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 | +import asyncio |
| 15 | +import gc |
| 16 | +from typing import Dict |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from playwright.async_api import async_playwright |
| 21 | + |
| 22 | + |
| 23 | +async def test_should_cancel_underlying_protocol_calls( |
| 24 | + browser_name: str, launch_arguments: Dict |
| 25 | +): |
| 26 | + handler_exception = None |
| 27 | + |
| 28 | + def exception_handlerdler(loop, context) -> None: |
| 29 | + nonlocal handler_exception |
| 30 | + handler_exception = context["exception"] |
| 31 | + |
| 32 | + asyncio.get_running_loop().set_exception_handler(exception_handlerdler) |
| 33 | + |
| 34 | + async with async_playwright() as p: |
| 35 | + browser = await p[browser_name].launch(**launch_arguments) |
| 36 | + page = await browser.new_page() |
| 37 | + task = asyncio.create_task(page.wait_for_selector("will-never-find")) |
| 38 | + # make sure that the wait_for_selector message was sent to the server (driver) |
| 39 | + await asyncio.sleep(0.1) |
| 40 | + task.cancel() |
| 41 | + with pytest.raises(asyncio.CancelledError): |
| 42 | + await task |
| 43 | + await browser.close() |
| 44 | + |
| 45 | + # The actual 'Future exception was never retrieved' is logged inside the Future destructor (__del__). |
| 46 | + gc.collect() |
| 47 | + |
| 48 | + assert handler_exception is None |
| 49 | + |
| 50 | + asyncio.get_running_loop().set_exception_handler(None) |
0 commit comments