From d1bd87c5627f5bcae8e6e6a7bdbf5e396ddaa39a Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Mon, 13 Nov 2023 12:40:08 +0100 Subject: [PATCH] fix: page.pause() timeout reset --- playwright/_impl/_browser_context.py | 7 +++++-- tests/async/test_page.py | 30 ++++++++++++++++++++++++++++ tests/conftest.py | 19 ++++++++++-------- tests/sync/test_page.py | 30 ++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 10 deletions(-) diff --git a/playwright/_impl/_browser_context.py b/playwright/_impl/_browser_context.py index 6681e0852..bfcae8c29 100644 --- a/playwright/_impl/_browser_context.py +++ b/playwright/_impl/_browser_context.py @@ -248,7 +248,8 @@ def set_default_navigation_timeout(self, timeout: float) -> None: def _set_default_navigation_timeout_impl(self, timeout: Optional[float]) -> None: self._timeout_settings.set_default_navigation_timeout(timeout) self._channel.send_no_reply( - "setDefaultNavigationTimeoutNoReply", dict(timeout=timeout) + "setDefaultNavigationTimeoutNoReply", + {} if timeout is None else {"timeout": timeout}, ) def set_default_timeout(self, timeout: float) -> None: @@ -256,7 +257,9 @@ def set_default_timeout(self, timeout: float) -> None: def _set_default_timeout_impl(self, timeout: Optional[float]) -> None: self._timeout_settings.set_default_timeout(timeout) - self._channel.send_no_reply("setDefaultTimeoutNoReply", dict(timeout=timeout)) + self._channel.send_no_reply( + "setDefaultTimeoutNoReply", {} if timeout is None else {"timeout": timeout} + ) @property def pages(self) -> List[Page]: diff --git a/tests/async/test_page.py b/tests/async/test_page.py index b92516c3c..b983b631e 100644 --- a/tests/async/test_page.py +++ b/tests/async/test_page.py @@ -1294,3 +1294,33 @@ def binding(source, o): await page.expose_binding("log", lambda source, o: binding(source, o)) await page.evaluate("const a = {}; a.b = a; window.log(a)") assert binding_values[0]["b"] == binding_values[0] + + +async def test_page_pause_should_reset_default_timeouts( + page: Page, headless: bool, server: Server +) -> None: + if not headless: + pytest.skip() + + await page.goto(server.EMPTY_PAGE) + page.pause() + with pytest.raises(Error, match="Timeout 30000ms exceeded."): + await page.get_by_text("foo").click() + + +async def test_page_pause_should_reset_custom_timeouts( + page: Page, headless: bool, server: Server +) -> None: + if not headless: + pytest.skip() + + page.set_default_timeout(123) + page.set_default_navigation_timeout(456) + await page.goto(server.EMPTY_PAGE) + page.pause() + with pytest.raises(Error, match="Timeout 123ms exceeded."): + await page.get_by_text("foo").click() + + server.set_route("/empty.html", lambda route: None) + with pytest.raises(Error, match="Timeout 456ms exceeded."): + await page.goto(server.EMPTY_PAGE) diff --git a/tests/conftest.py b/tests/conftest.py index a3b8e9992..80ec8e0fb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -35,7 +35,7 @@ _dirname = get_file_dirname() -def pytest_generate_tests(metafunc: Any) -> None: +def pytest_generate_tests(metafunc: pytest.Metafunc) -> None: if "browser_name" in metafunc.fixturenames: browsers = metafunc.config.option.browser or ["chromium", "firefox", "webkit"] metafunc.parametrize("browser_name", browsers, scope="session") @@ -54,11 +54,14 @@ def assetdir() -> Path: @pytest.fixture(scope="session") -def launch_arguments(pytestconfig: Any) -> Dict: +def headless(pytestconfig: pytest.Config) -> bool: + return not (pytestconfig.getoption("--headed") or os.getenv("HEADFUL", False)) + + +@pytest.fixture(scope="session") +def launch_arguments(pytestconfig: pytest.Config, headless: bool) -> Dict: return { - "headless": not ( - pytestconfig.getoption("--headed") or os.getenv("HEADFUL", False) - ), + "headless": headless, "channel": pytestconfig.getoption("--browser-channel"), } @@ -92,12 +95,12 @@ def after_each_hook() -> Generator[None, None, None]: @pytest.fixture(scope="session") -def browser_name(pytestconfig: Any) -> None: +def browser_name(pytestconfig: pytest.Config) -> None: return pytestconfig.getoption("browser") @pytest.fixture(scope="session") -def browser_channel(pytestconfig: Any) -> None: +def browser_channel(pytestconfig: pytest.Config) -> None: return pytestconfig.getoption("--browser-channel") @@ -169,7 +172,7 @@ def skip_by_platform(request: pytest.FixtureRequest) -> None: pytest.skip(f"skipped on this platform: {sys.platform}") -def pytest_addoption(parser: Any) -> None: +def pytest_addoption(parser: pytest.Parser) -> None: group = parser.getgroup("playwright", "Playwright") group.addoption( "--browser", diff --git a/tests/sync/test_page.py b/tests/sync/test_page.py index ccb229196..7550a80d1 100644 --- a/tests/sync/test_page.py +++ b/tests/sync/test_page.py @@ -84,3 +84,33 @@ def test_emitted_for_domcontentloaded_and_load(page: Page, server: Server) -> No page.goto(server.EMPTY_PAGE) assert isinstance(dom_info.value, Page) assert isinstance(load_info.value, Page) + + +def test_page_pause_should_reset_default_timeouts( + page: Page, headless: bool, server: Server +) -> None: + if not headless: + pytest.skip() + + page.goto(server.EMPTY_PAGE) + page.pause() + with pytest.raises(Error, match="Timeout 30000ms exceeded."): + page.get_by_text("foo").click() + + +def test_page_pause_should_reset_custom_timeouts( + page: Page, headless: bool, server: Server +) -> None: + if not headless: + pytest.skip() + + page.set_default_timeout(123) + page.set_default_navigation_timeout(456) + page.goto(server.EMPTY_PAGE) + page.pause() + with pytest.raises(Error, match="Timeout 123ms exceeded."): + page.get_by_text("foo").click() + + server.set_route("/empty.html", lambda route: None) + with pytest.raises(Error, match="Timeout 456ms exceeded."): + page.goto(server.EMPTY_PAGE)