diff --git a/pytest-playwright-asyncio/pytest_playwright_asyncio/_base_url_fallback.py b/pytest-playwright-asyncio/pytest_playwright_asyncio/_base_url_fallback.py new file mode 100644 index 0000000..e14d201 --- /dev/null +++ b/pytest-playwright-asyncio/pytest_playwright_asyncio/_base_url_fallback.py @@ -0,0 +1,29 @@ +# 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. + +# Provides the base_url fixture when pytest-base-url is not in use, see +# pytest_load_initial_conftests in pytest_playwright.py. It is a module rather +# than a class instance, as pytest 9.2 deprecates session-scoped fixtures defined +# as instance methods: https://github.com/microsoft/playwright-pytest/issues/341 + +from typing import Any, Optional + +import pytest + + +@pytest.fixture(scope="session") +def base_url(pytestconfig: Any) -> Optional[str]: + return ( + pytestconfig.getoption("--base-url") or pytestconfig.getini("base_url") or None + ) diff --git a/pytest-playwright-asyncio/pytest_playwright_asyncio/pytest_playwright.py b/pytest-playwright-asyncio/pytest_playwright_asyncio/pytest_playwright.py index f1ebce0..ba9ccd8 100644 --- a/pytest-playwright-asyncio/pytest_playwright_asyncio/pytest_playwright.py +++ b/pytest-playwright-asyncio/pytest_playwright_asyncio/pytest_playwright.py @@ -74,6 +74,8 @@ import traceback import urllib.parse +from pytest_playwright_asyncio import _base_url_fallback + @pytest.fixture(scope="session") def _pw_artifacts_folder( @@ -818,7 +820,7 @@ def pytest_load_initial_conftests( default=os.getenv("PYTEST_BASE_URL", None), help="base url for the application under test.", ) - early_config.pluginmanager.register(_BaseUrlFallback(), "playwright-base-url") + early_config.pluginmanager.register(_base_url_fallback, "playwright-base-url") def _is_pytest_base_url_loaded(pluginmanager: pytest.PytestPluginManager) -> bool: @@ -828,17 +830,6 @@ def _is_pytest_base_url_loaded(pluginmanager: pytest.PytestPluginManager) -> boo return module is not None and pluginmanager.is_registered(module) -class _BaseUrlFallback: - # Provides the base_url fixture when pytest-base-url is not in use. - @pytest.fixture(scope="session") - def base_url(self, pytestconfig: Any) -> Optional[str]: - return ( - pytestconfig.getoption("--base-url") - or pytestconfig.getini("base_url") - or None - ) - - class ArtifactsRecorder: def __init__( self, diff --git a/pytest-playwright/pytest_playwright/_base_url_fallback.py b/pytest-playwright/pytest_playwright/_base_url_fallback.py new file mode 100644 index 0000000..e14d201 --- /dev/null +++ b/pytest-playwright/pytest_playwright/_base_url_fallback.py @@ -0,0 +1,29 @@ +# 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. + +# Provides the base_url fixture when pytest-base-url is not in use, see +# pytest_load_initial_conftests in pytest_playwright.py. It is a module rather +# than a class instance, as pytest 9.2 deprecates session-scoped fixtures defined +# as instance methods: https://github.com/microsoft/playwright-pytest/issues/341 + +from typing import Any, Optional + +import pytest + + +@pytest.fixture(scope="session") +def base_url(pytestconfig: Any) -> Optional[str]: + return ( + pytestconfig.getoption("--base-url") or pytestconfig.getini("base_url") or None + ) diff --git a/pytest-playwright/pytest_playwright/pytest_playwright.py b/pytest-playwright/pytest_playwright/pytest_playwright.py index fce034a..aeb22fa 100644 --- a/pytest-playwright/pytest_playwright/pytest_playwright.py +++ b/pytest-playwright/pytest_playwright/pytest_playwright.py @@ -71,6 +71,8 @@ import traceback import urllib.parse +from pytest_playwright import _base_url_fallback + @pytest.fixture(scope="session") def _pw_artifacts_folder( @@ -815,7 +817,7 @@ def pytest_load_initial_conftests( default=os.getenv("PYTEST_BASE_URL", None), help="base url for the application under test.", ) - early_config.pluginmanager.register(_BaseUrlFallback(), "playwright-base-url") + early_config.pluginmanager.register(_base_url_fallback, "playwright-base-url") def _is_pytest_base_url_loaded(pluginmanager: pytest.PytestPluginManager) -> bool: @@ -825,17 +827,6 @@ def _is_pytest_base_url_loaded(pluginmanager: pytest.PytestPluginManager) -> boo return module is not None and pluginmanager.is_registered(module) -class _BaseUrlFallback: - # Provides the base_url fixture when pytest-base-url is not in use. - @pytest.fixture(scope="session") - def base_url(self, pytestconfig: Any) -> Optional[str]: - return ( - pytestconfig.getoption("--base-url") - or pytestconfig.getini("base_url") - or None - ) - - class ArtifactsRecorder: def __init__( self, diff --git a/tests/test_asyncio.py b/tests/test_asyncio.py index d34e3ea..4175355 100644 --- a/tests/test_asyncio.py +++ b/tests/test_asyncio.py @@ -479,7 +479,9 @@ async def test_base_url(page, base_url): """ ) result = testdir.runpytest(*args) - result.assert_outcomes(passed=1) + # The fallback fixture must not trip pytest's deprecations, see + # https://github.com/microsoft/playwright-pytest/issues/341 + result.assert_outcomes(passed=1, warnings=0) def test_base_url_without_plugin_autoload( diff --git a/tests/test_sync.py b/tests/test_sync.py index 9528fc5..10f73e4 100644 --- a/tests/test_sync.py +++ b/tests/test_sync.py @@ -492,7 +492,9 @@ def test_base_url(page, base_url): """ ) result = testdir.runpytest(*args) - result.assert_outcomes(passed=1) + # The fallback fixture must not trip pytest's deprecations, see + # https://github.com/microsoft/playwright-pytest/issues/341 + result.assert_outcomes(passed=1, warnings=0) def test_base_url_without_plugin_autoload(