Skip to content

Commit 633d881

Browse files
authored
tests: cleanup tests (microsoft#717)
1 parent b5c9f8d commit 633d881

17 files changed

+23
-79
lines changed

playwright/_impl/_connection.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,3 @@ def serialize_call_stack(stack_trace: traceback.StackSummary) -> List[Dict]:
338338
)
339339
stack.reverse()
340340
return stack
341-
342-
343-
def capture_call_stack() -> List[Dict]:
344-
return serialize_call_stack(traceback.extract_stack())

playwright/_impl/_tracing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from playwright._impl._connection import from_channel
2020
from playwright._impl._helper import locals_to_params
2121

22-
if TYPE_CHECKING:
22+
if TYPE_CHECKING: # pragma: no cover
2323
from playwright._impl._browser_context import BrowserContext
2424

2525

tests/async/test_browsercontext.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ async def test_page_event_should_create_new_context(browser):
2626
assert context in browser.contexts
2727
await context.close()
2828
assert len(browser.contexts) == 0
29+
assert context.browser == browser
2930

3031

3132
async def test_window_open_should_use_parent_tab_context(browser, server):

tests/async/test_browsertype_connect_cdp.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import requests
1919

2020
from playwright.async_api import BrowserType
21-
from tests.server import Server, find_free_port, wait_for_port
21+
from tests.server import Server, find_free_port
2222

2323
pytestmark = pytest.mark.only_browser("chromium")
2424

@@ -30,7 +30,6 @@ async def test_connect_to_an_existing_cdp_session(
3030
browser_server = await browser_type.launch(
3131
**launch_arguments, args=[f"--remote-debugging-port={port}"]
3232
)
33-
wait_for_port(port)
3433
cdp_browser = await browser_type.connect_over_cdp(f"http://localhost:{port}")
3534
assert len(cdp_browser.contexts) == 1
3635
await cdp_browser.close()
@@ -44,7 +43,6 @@ async def test_connect_to_an_existing_cdp_session_twice(
4443
browser_server = await browser_type.launch(
4544
**launch_arguments, args=[f"--remote-debugging-port={port}"]
4645
)
47-
wait_for_port(port)
4846
endpoint_url = f"http://localhost:{port}"
4947
cdp_browser1 = await browser_type.connect_over_cdp(endpoint_url)
5048
cdp_browser2 = await browser_type.connect_over_cdp(endpoint_url)
@@ -78,7 +76,6 @@ async def test_conect_over_a_ws_endpoint(
7876
browser_server = await browser_type.launch(
7977
**launch_arguments, args=[f"--remote-debugging-port={port}"]
8078
)
81-
wait_for_port(port)
8279
ws_endpoint = _ws_endpoint_from_url(f"http://localhost:{port}/json/version/")
8380

8481
cdp_browser1 = await browser_type.connect_over_cdp(ws_endpoint)

tests/async/test_defaultbrowsercontext.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import os
1717

1818
import pytest
19-
from flaky import flaky
2019

2120
from playwright._impl._api_types import Error
2221

@@ -280,7 +279,6 @@ async def test_should_accept_user_data_dir(server, tmpdir, launch_persistent):
280279
assert len(os.listdir(tmpdir)) > 0
281280

282281

283-
@flaky
284282
async def test_should_restore_state_from_userDataDir(
285283
browser_type, launch_arguments, server, tmp_path_factory
286284
):

tests/async/test_download.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,6 @@ def handle_download(request):
288288

289289

290290
async def test_should_report_new_window_downloads(browser, server):
291-
# TODO: - the test fails in headful Chromium as the popup page gets closed along
292-
# with the session before download completed event arrives.
293-
# - WebKit doesn't close the popup page
294291
page = await browser.new_page(accept_downloads=True)
295292
await page.set_content(
296293
f'<a target=_blank href="{server.PREFIX}/download">download</a>'

tests/async/test_frames.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
async def test_evaluate_handle(page, server):
2121
await page.goto(server.EMPTY_PAGE)
2222
main_frame = page.main_frame
23+
assert main_frame.page == page
2324
window_handle = await main_frame.evaluate_handle("window")
2425
assert window_handle
2526

tests/async/test_headful.py

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515

1616
import pytest
17-
from flaky import flaky
1817

1918

2019
async def test_should_have_default_url_when_launching_browser(
@@ -28,34 +27,6 @@ async def test_should_have_default_url_when_launching_browser(
2827
await browser_context.close()
2928

3029

31-
async def test_headless_should_be_able_to_read_cookies_written_by_headful(
32-
browser_type, launch_arguments, server, tmpdir, is_chromium, is_win
33-
):
34-
if is_chromium and is_win:
35-
pytest.skip("see https://github.com/microsoft/playwright/issues/717")
36-
return
37-
# Write a cookie in headful chrome
38-
headful_context = await browser_type.launch_persistent_context(
39-
tmpdir, **{**launch_arguments, "headless": False}
40-
)
41-
headful_page = await headful_context.new_page()
42-
await headful_page.goto(server.EMPTY_PAGE)
43-
await headful_page.evaluate(
44-
"""() => document.cookie = 'foo=true; expires=Fri, 31 Dec 9999 23:59:59 GMT'"""
45-
)
46-
await headful_context.close()
47-
# Read the cookie from headless chrome
48-
headless_context = await browser_type.launch_persistent_context(
49-
tmpdir, **{**launch_arguments, "headless": True}
50-
)
51-
headless_page = await headless_context.new_page()
52-
await headless_page.goto(server.EMPTY_PAGE)
53-
cookie = await headless_page.evaluate("() => document.cookie")
54-
await headless_context.close()
55-
# This might throw. See https://github.com/GoogleChrome/puppeteer/issues/2778
56-
assert cookie == "foo=true"
57-
58-
5930
async def test_should_close_browser_with_beforeunload_page(
6031
browser_type, launch_arguments, server, tmpdir
6132
):
@@ -177,7 +148,6 @@ async def test_should_not_override_viewport_size_when_passed_null(
177148
await browser.close()
178149

179150

180-
@flaky
181151
async def test_page_bring_to_front_should_work(browser_type, launch_arguments):
182152
browser = await browser_type.launch(**{**launch_arguments, "headless": False})
183153
page1 = await browser.new_page()

tests/async/test_network.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ async def test_request_headers_should_work(
171171
assert "WebKit" in response.request.headers["user-agent"]
172172

173173

174+
# TODO: update once fixed https://github.com/microsoft/playwright/issues/6690
174175
@pytest.mark.xfail
175176
async def test_request_headers_should_get_the_same_headers_as_the_server(
176177
page: Page, server, is_webkit, is_win

tests/async/test_page.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,6 @@ async def test_async_stacks_should_work(page, server):
128128
assert __file__ in exc_info.value.stack
129129

130130

131-
# TODO: bring in page.crash events
132-
133-
134131
async def test_opener_should_provide_access_to_the_opener_page(page):
135132
async with page.expect_popup() as popup_info:
136133
await page.evaluate("window.open('about:blank')"),
@@ -766,7 +763,6 @@ async def test_select_option_should_select_only_first_option(page, server):
766763
assert await page.evaluate("result.onChange") == ["blue"]
767764

768765

769-
@pytest.mark.skip_browser("webkit") # TODO: investigate
770766
async def test_select_option_should_not_throw_when_select_causes_navigation(
771767
page, server
772768
):

tests/async/test_tracing.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ async def test_browser_context_output_trace(
2828
page = await context.new_page()
2929
await page.goto(server.PREFIX + "/grid.html")
3030
await context.tracing.stop()
31+
await page.wait_for_timeout(1000)
3132
await context.tracing.export(Path(tmp_path / "traces" / "trace.zip").resolve())
3233
assert Path(tmp_path / "traces" / "trace.zip").exists()
3334
assert Path(tmp_path / "traces" / "resources").exists()

tests/async/test_video.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ async def test_short_video_should_throw(browser, tmpdir, server):
2828
await page.goto(server.PREFIX + "/grid.html")
2929
path = await page.video.path()
3030
assert str(tmpdir) in str(path)
31+
await page.wait_for_timeout(1000)
3132
await page.context.close()
3233
assert os.path.exists(path)
3334

@@ -43,6 +44,7 @@ async def test_short_video_should_throw_persistent_context(
4344
)
4445
page = context.pages[0]
4546
await page.goto(server.PREFIX + "/grid.html")
47+
await page.wait_for_timeout(1000)
4648
await context.close()
4749

4850
path = await page.video.path()

tests/async/test_worker.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from asyncio.futures import Future
1717

1818
import pytest
19+
from flaky import flaky
1920

2021
from playwright.async_api import Error, Page, Worker
2122

@@ -99,6 +100,7 @@ async def test_workers_should_report_errors(page):
99100
assert "this is my error" in error_log.message
100101

101102

103+
@flaky # Upstream flaky
102104
async def test_workers_should_clear_upon_navigation(server, page):
103105
await page.goto(server.EMPTY_PAGE)
104106
async with page.expect_event("worker") as event_info:
@@ -114,6 +116,7 @@ async def test_workers_should_clear_upon_navigation(server, page):
114116
assert len(page.workers) == 0
115117

116118

119+
@flaky # Upstream flaky
117120
async def test_workers_should_clear_upon_cross_process_navigation(server, page):
118121
await page.goto(server.EMPTY_PAGE)
119122
async with page.expect_event("worker") as event_info:

tests/conftest.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,11 @@ def kill(self):
239239
if self.process.poll() is not None:
240240
return
241241
if sys.platform == "win32":
242-
subprocess.check_call(["taskkill", "/F", "/PID", str(self.process.pid)])
242+
subprocess.check_call(
243+
["taskkill", "/F", "/PID", str(self.process.pid)],
244+
stderr=subprocess.DEVNULL,
245+
stdout=subprocess.DEVNULL,
246+
)
243247
else:
244248
self.process.kill()
245249
self.process.wait()

tests/server.py

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import mimetypes
1919
import socket
2020
import threading
21-
import time
2221
from contextlib import closing
2322
from http import HTTPStatus
2423
from urllib.parse import urlparse
@@ -40,30 +39,6 @@ def find_free_port():
4039
return s.getsockname()[1]
4140

4241

43-
def wait_for_port(port, host="localhost", timeout=5.0):
44-
"""Wait until a port starts accepting TCP connections.
45-
Args:
46-
port (int): Port number.
47-
host (str): Host address on which the port should exist.
48-
timeout (float): In seconds. How long to wait before raising errors.
49-
Raises:
50-
TimeoutError: The port isn't accepting connection after time specified in `timeout`.
51-
Reference: https://gist.github.com/butla/2d9a4c0f35ea47b7452156c96a4e7b12
52-
"""
53-
start_time = time.perf_counter()
54-
while True:
55-
try:
56-
with socket.create_connection((host, port), timeout=timeout):
57-
break
58-
except OSError as ex:
59-
time.sleep(0.01)
60-
if time.perf_counter() - start_time >= timeout:
61-
raise TimeoutError(
62-
"Waited too long for the port {} on host {} to start accepting "
63-
"connections.".format(port, host)
64-
) from ex
65-
66-
6742
class Server:
6843
protocol = "http"
6944

@@ -135,9 +110,7 @@ def process(self):
135110
return
136111
file_content = None
137112
try:
138-
file_content = (
139-
static_path / request.path.decode()[1:]
140-
).read_bytes()
113+
file_content = (static_path / path[1:]).read_bytes()
141114
request.setHeader(b"Content-Type", mimetypes.guess_type(path)[0])
142115
request.setHeader(b"Cache-Control", "no-cache, no-store")
143116
if path in gzip_routes:

tests/sync/test_browsertype_connect_cdp.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import pytest
1818

1919
from playwright.sync_api import BrowserType
20-
from tests.server import find_free_port, wait_for_port
20+
from tests.server import find_free_port
2121

2222
pytestmark = pytest.mark.only_browser("chromium")
2323

@@ -29,7 +29,6 @@ def test_connect_to_an_existing_cdp_session(
2929
browser_server = browser_type.launch(
3030
**launch_arguments, args=[f"--remote-debugging-port={port}"]
3131
)
32-
wait_for_port(port)
3332
cdp_browser = browser_type.connect_over_cdp(f"http://localhost:{port}")
3433
assert len(cdp_browser.contexts) == 1
3534
cdp_browser.close()

tests/sync/test_video.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def test_should_expose_video_path(browser, tmpdir, server):
2323
path = page.video.path()
2424
assert repr(page.video) == f"<Video page={page}>"
2525
assert str(tmpdir) in str(path)
26+
page.wait_for_timeout(1000)
2627
page.context.close()
2728

2829

@@ -31,6 +32,7 @@ def test_video_should_exist(browser, tmpdir, server):
3132
page.goto(server.PREFIX + "/grid.html")
3233
path = page.video.path()
3334
assert str(tmpdir) in str(path)
35+
page.wait_for_timeout(1000)
3436
page.context.close()
3537
assert os.path.exists(path)
3638

@@ -40,6 +42,7 @@ def test_record_video_to_path(browser, tmpdir, server):
4042
page.goto(server.PREFIX + "/grid.html")
4143
path = page.video.path()
4244
assert str(tmpdir) in str(path)
45+
page.wait_for_timeout(1000)
4346
page.context.close()
4447
assert os.path.exists(path)
4548

@@ -54,6 +57,7 @@ def test_record_video_to_path_persistent(
5457
page.goto(server.PREFIX + "/grid.html")
5558
path = page.video.path()
5659
assert str(tmpdir) in str(path)
60+
page.wait_for_timeout(1000)
5761
context.close()
5862
assert os.path.exists(path)
5963

@@ -67,5 +71,6 @@ def test_record_video_can_get_video_path_immediately(
6771
page = context.pages[0]
6872
path = page.video.path()
6973
assert str(tmpdir) in str(path)
74+
page.wait_for_timeout(1000)
7075
context.close()
7176
assert os.path.exists(path)

0 commit comments

Comments
 (0)