Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions pytest-playwright/pytest_playwright/pytest_playwright.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions tests/test_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,<div>hello</div>")
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:
Expand Down
21 changes: 21 additions & 0 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,<div>hello</div>")
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:
Expand Down
Loading