Skip to content
Closed
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
8 changes: 8 additions & 0 deletions tests/async/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,21 @@ async def launch(**kwargs):

yield launch
for browser in browsers:
if browser.contexts:
raise Exception(
"Test failed to close a context it created via browser_factory fixture. Explicitly close context, or use context_factory."
)
await browser.close()


@pytest.fixture(scope="session")
async def browser(browser_factory):
browser = await browser_factory()
yield browser
if browser.contexts:
raise Exception(
"Test failed to close a context it created via browser fixture. Explicitly close context, or use context_factory."
)
await browser.close()


Expand Down
1 change: 1 addition & 0 deletions tests/async/test_browsercontext.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,3 +751,4 @@ async def test_should_support_forced_colors(browser: Browser):
page = await context.new_page()
assert await page.evaluate("matchMedia('(forced-colors: active)').matches")
assert not await page.evaluate("matchMedia('(forced-colors: none)').matches")
await context.close()
2 changes: 2 additions & 0 deletions tests/async/test_browsercontext_add_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,14 @@ async def test_should_isolate_cookies_between_launches(browser_factory, server):
}
]
)
await context_1.close()
await browser_1.close()

browser_2 = await browser_factory()
context_2 = await browser_2.new_context()
cookies = await context_2.cookies()
assert cookies == []
await context_2.close()
await browser_2.close()


Expand Down
10 changes: 8 additions & 2 deletions tests/async/test_browsertype_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ async def test_browser_type_connect_should_be_able_to_reconnect_to_a_browser(
assert len(browser_context.pages) == 1
assert await page.evaluate("11 * 11") == 121
await page.goto(server.EMPTY_PAGE)
await browser_context.close()
await browser.close()

browser = await browser_type.connect(remote_server.ws_endpoint)
browser_context = await browser.new_context()
page = await browser_context.new_page()
await page.goto(server.EMPTY_PAGE)
await browser_context.close()
await browser.close()


Expand All @@ -49,20 +51,22 @@ async def test_browser_type_connect_should_be_able_to_connect_two_browsers_at_th
remote_server = launch_server()
browser1 = await browser_type.connect(remote_server.ws_endpoint)
assert len(browser1.contexts) == 0
await browser1.new_context()
browser1_context = await browser1.new_context()
assert len(browser1.contexts) == 1

browser2 = await browser_type.connect(remote_server.ws_endpoint)
assert len(browser2.contexts) == 0
await browser2.new_context()
browser2_context = await browser2.new_context()
assert len(browser2.contexts) == 1
assert len(browser1.contexts) == 1

await browser1_context.close()
await browser1.close()
page2 = await browser2.new_page()
# original browser should still work
assert await page2.evaluate("7 * 6") == 42

await browser2_context.close()
await browser2.close()


Expand Down Expand Up @@ -297,3 +301,5 @@ async def test_should_upload_large_file(
)
assert match.group("name") == b"file1"
assert match.group("filename") == b"200MB.zip"
await context.close()
await browser.close()
7 changes: 7 additions & 0 deletions tests/async/test_har.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ async def test_should_round_trip_har_zip(
await expect(page_2.locator("body")).to_have_css(
"background-color", "rgb(255, 192, 203)"
)
await context_2.close()


async def test_should_round_trip_har_with_post_data(
Expand Down Expand Up @@ -536,6 +537,7 @@ async def test_should_round_trip_har_with_post_data(
assert await page_2.evaluate(fetch_function, "3") == "3"
with pytest.raises(Exception):
await page_2.evaluate(fetch_function, "4")
await context_2.close()


async def test_should_disambiguate_by_header(
Expand Down Expand Up @@ -578,6 +580,7 @@ async def test_should_disambiguate_by_header(
assert await page_2.evaluate(fetch_function, "baz2") == "baz2"
assert await page_2.evaluate(fetch_function, "baz3") == "baz3"
assert await page_2.evaluate(fetch_function, "baz4") == "baz1"
await context_2.close()


async def test_should_produce_extracted_zip(
Expand Down Expand Up @@ -605,6 +608,7 @@ async def test_should_produce_extracted_zip(
await expect(page_2.locator("body")).to_have_css(
"background-color", "rgb(255, 192, 203)"
)
await context_2.close()


async def test_should_update_har_zip_for_context(
Expand All @@ -627,6 +631,7 @@ async def test_should_update_har_zip_for_context(
await expect(page_2.locator("body")).to_have_css(
"background-color", "rgb(255, 192, 203)"
)
await context_2.close()


async def test_should_update_har_zip_for_page(
Expand All @@ -649,6 +654,7 @@ async def test_should_update_har_zip_for_page(
await expect(page_2.locator("body")).to_have_css(
"background-color", "rgb(255, 192, 203)"
)
await context_2.close()


async def test_should_update_extracted_har_zip_for_page(
Expand All @@ -675,3 +681,4 @@ async def test_should_update_extracted_har_zip_for_page(
await expect(page_2.locator("body")).to_have_css(
"background-color", "rgb(255, 192, 203)"
)
await context_2.close()
2 changes: 2 additions & 0 deletions tests/async/test_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ async def test_browser_context_output_trace(
await page.goto(server.PREFIX + "/grid.html")
await context.tracing.stop(path=tmp_path / "trace.zip")
assert Path(tmp_path / "trace.zip").exists()
await context.close()


async def test_browser_context_should_not_throw_when_stopping_without_start_but_not_exporting(
Expand All @@ -56,6 +57,7 @@ async def test_browser_context_output_trace_chunk(
await button.click()
await context.tracing.stop_chunk(path=tmp_path / "trace2.zip")
assert Path(tmp_path / "trace2.zip").exists()
await context.close()


async def test_should_collect_sources(
Expand Down
4 changes: 4 additions & 0 deletions tests/sync/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ def browser(
) -> Generator[Browser, None, None]:
browser = browser_type.launch(**launch_arguments)
yield browser
if browser.contexts:
raise Exception(
"Test failed to close a context it created via browser fixture. Explicitly close context, or use context fixture"
)
browser.close()


Expand Down
11 changes: 8 additions & 3 deletions tests/sync/test_browsertype_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ def test_browser_type_connect_should_be_able_to_reconnect_to_a_browser(
assert len(browser_context.pages) == 1
assert page.evaluate("11 * 11") == 121
page.goto(server.EMPTY_PAGE)
browser_context.close()
browser.close()

browser = browser_type.connect(remote_server.ws_endpoint)
browser_context = browser.new_context()
page = browser_context.new_page()
page.goto(server.EMPTY_PAGE)
browser_context.close()
browser.close()


Expand All @@ -62,20 +64,22 @@ def test_browser_type_connect_should_be_able_to_connect_two_browsers_at_the_same
remote_server = launch_server()
browser1 = browser_type.connect(remote_server.ws_endpoint)
assert len(browser1.contexts) == 0
browser1.new_context()
browser1_context = browser1.new_context()
assert len(browser1.contexts) == 1

browser2 = browser_type.connect(remote_server.ws_endpoint)
assert len(browser2.contexts) == 0
browser2.new_context()
browser2_context = browser2.new_context()
assert len(browser2.contexts) == 1
assert len(browser1.contexts) == 1

browser1_context.close()
browser1.close()
page2 = browser2.new_page()
# original browser should still work
assert page2.evaluate("7 * 6") == 42

browser2_context.close()
browser2.close()


Expand Down Expand Up @@ -219,5 +223,6 @@ def handle_request(route: Route) -> None:
assert response
assert response.status == 200
assert response.json() == {"foo": "bar"}

context.close()
browser.close()
remote.kill()
7 changes: 7 additions & 0 deletions tests/sync/test_har.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ def test_should_round_trip_har_zip(
page_2.goto(server.PREFIX + "/one-style.html")
assert "hello, world!" in page_2.content()
expect(page_2.locator("body")).to_have_css("background-color", "rgb(255, 192, 203)")
context_2.close()


def test_should_round_trip_har_with_post_data(
Expand Down Expand Up @@ -471,6 +472,7 @@ def test_should_round_trip_har_with_post_data(
assert page_2.evaluate(fetch_function, "3") == "3"
with pytest.raises(Exception):
page_2.evaluate(fetch_function, "4")
context_2.close()


def test_should_disambiguate_by_header(
Expand Down Expand Up @@ -512,6 +514,7 @@ def test_should_disambiguate_by_header(
assert page_2.evaluate(fetch_function, "baz2") == "baz2"
assert page_2.evaluate(fetch_function, "baz3") == "baz3"
assert page_2.evaluate(fetch_function, "baz4") == "baz1"
context_2.close()


def test_should_produce_extracted_zip(
Expand All @@ -537,6 +540,7 @@ def test_should_produce_extracted_zip(
page_2.goto(server.PREFIX + "/one-style.html")
assert "hello, world!" in page_2.content()
expect(page_2.locator("body")).to_have_css("background-color", "rgb(255, 192, 203)")
context_2.close()


def test_should_update_har_zip_for_context(
Expand All @@ -557,6 +561,7 @@ def test_should_update_har_zip_for_context(
page_2.goto(server.PREFIX + "/one-style.html")
assert "hello, world!" in page_2.content()
expect(page_2.locator("body")).to_have_css("background-color", "rgb(255, 192, 203)")
context_2.close()


def test_should_update_har_zip_for_page(
Expand All @@ -577,6 +582,7 @@ def test_should_update_har_zip_for_page(
page_2.goto(server.PREFIX + "/one-style.html")
assert "hello, world!" in page_2.content()
expect(page_2.locator("body")).to_have_css("background-color", "rgb(255, 192, 203)")
context_2.close()


def test_should_update_extracted_har_zip_for_page(
Expand All @@ -601,3 +607,4 @@ def test_should_update_extracted_har_zip_for_page(
page_2.goto(server.PREFIX + "/one-style.html")
assert "hello, world!" in page_2.content()
expect(page_2.locator("body")).to_have_css("background-color", "rgb(255, 192, 203)")
context_2.close()
2 changes: 2 additions & 0 deletions tests/sync/test_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def test_browser_context_output_trace(
page.goto(server.PREFIX + "/grid.html")
context.tracing.stop(path=tmp_path / "trace.zip")
assert Path(tmp_path / "trace.zip").exists()
context.close()


def test_browser_context_should_not_throw_when_stopping_without_start_but_not_exporting(
Expand All @@ -56,6 +57,7 @@ def test_browser_context_output_trace_chunk(
button.click()
context.tracing.stop_chunk(path=tmp_path / "trace2.zip")
assert Path(tmp_path / "trace2.zip").exists()
context.close()


def test_should_collect_sources(
Expand Down