diff --git a/pytest-playwright-asyncio/pytest_playwright_asyncio/pytest_playwright.py b/pytest-playwright-asyncio/pytest_playwright_asyncio/pytest_playwright.py index ccc1432..694bda9 100644 --- a/pytest-playwright-asyncio/pytest_playwright_asyncio/pytest_playwright.py +++ b/pytest-playwright-asyncio/pytest_playwright_asyncio/pytest_playwright.py @@ -617,8 +617,10 @@ async def _new_context(**kwargs: Any) -> BrowserContext: original_close = context.close async def _close_wrapper(*args: Any, **kwargs: Any) -> None: - contexts.remove(context) - await _artifacts_recorder.on_will_close_browser_context(context) + # BrowserContext.close() may be called more than once. + if context in contexts: + contexts.remove(context) + await _artifacts_recorder.on_will_close_browser_context(context) await original_close(*args, **kwargs) context.close = _close_wrapper diff --git a/pytest-playwright/pytest_playwright/pytest_playwright.py b/pytest-playwright/pytest_playwright/pytest_playwright.py index 4a6a5a2..93fad36 100644 --- a/pytest-playwright/pytest_playwright/pytest_playwright.py +++ b/pytest-playwright/pytest_playwright/pytest_playwright.py @@ -612,8 +612,10 @@ def _new_context(**kwargs: Any) -> BrowserContext: original_close = context.close def _close_wrapper(*args: Any, **kwargs: Any) -> None: - contexts.remove(context) - _artifacts_recorder.on_will_close_browser_context(context) + # BrowserContext.close() may be called more than once. + if context in contexts: + contexts.remove(context) + _artifacts_recorder.on_will_close_browser_context(context) original_close(*args, **kwargs) context.close = _close_wrapper diff --git a/tests/test_asyncio.py b/tests/test_asyncio.py index 26e83a0..f9452de 100644 --- a/tests/test_asyncio.py +++ b/tests/test_asyncio.py @@ -1532,6 +1532,31 @@ async def test_artifact_collection(browser, page, new_context): ) +def test_context_can_be_closed_twice(testdir: pytest.Testdir) -> None: + testdir.makepyfile( + """ + import pytest + + @pytest.mark.asyncio + async def test_close_twice(context): + page = await context.new_page() + await page.goto("data:text/html,
hello
") + await context.close() + await context.close() + """ + ) + result = testdir.runpytest("--tracing", "on") + result.assert_outcomes(passed=1) + test_results_dir = os.path.join(testdir.tmpdir, "test-results") + _assert_folder_structure( + test_results_dir, + """ +- test-context-can-be-closed-twice-py-test-close-twice-chromium: + - trace.zip +""", + ) + + def test_artifact_collection_should_work_for_manually_created_contexts_retain_on_failure_failed( testdir: pytest.Testdir, ) -> None: diff --git a/tests/test_sync.py b/tests/test_sync.py index 866967d..ced5eff 100644 --- a/tests/test_sync.py +++ b/tests/test_sync.py @@ -1484,6 +1484,27 @@ def test_artifact_collection(browser, page, new_context): ) +def test_context_can_be_closed_twice(testdir: pytest.Testdir) -> None: + testdir.makepyfile( + """ + def test_close_twice(context): + context.new_page().goto("data:text/html,
hello
") + context.close() + context.close() + """ + ) + result = testdir.runpytest("--tracing", "on") + result.assert_outcomes(passed=1) + test_results_dir = os.path.join(testdir.tmpdir, "test-results") + _assert_folder_structure( + test_results_dir, + """ +- test-context-can-be-closed-twice-py-test-close-twice-chromium: + - trace.zip +""", + ) + + def test_artifact_collection_should_work_for_manually_created_contexts_retain_on_failure_failed( testdir: pytest.Testdir, ) -> None: