Skip to content

Commit 463c6df

Browse files
chore: simplified tests (microsoft#558)
1 parent 6e062b1 commit 463c6df

File tree

10 files changed

+170
-147
lines changed

10 files changed

+170
-147
lines changed

.github/workflows/ci.yml

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ jobs:
4040
git diff
4141
exit 1
4242
fi
43-
- name: Run reference checks
44-
run: bash buildbots/test-reference-count.sh
4543
build:
4644
name: Build
4745
timeout-minutes: 30
@@ -87,19 +85,25 @@ jobs:
8785
- name: Install browsers
8886
run: python -m playwright install
8987
- name: Common Tests
90-
run: pytest -vv tests/common --browser=${{ matrix.browser }} --timeout 90
88+
run: pytest tests/common --browser=${{ matrix.browser }} --timeout 90
89+
- name: Test Reference count
90+
run: pytest tests/test_reference_count_async.py --browser=${{ matrix.browser }}
91+
- name: Test Wheel Installation
92+
run: pytest tests/test_installation.py --browser=${{ matrix.browser }}
93+
- name: Test Generation Scripts
94+
run: pytest tests/test_generation_scripts.py --browser=${{ matrix.browser }}
9195
- name: Test Sync API
9296
if: matrix.os != 'ubuntu-latest'
93-
run: pytest -vv tests/sync --browser=${{ matrix.browser }} --timeout 90
97+
run: pytest tests/sync --browser=${{ matrix.browser }} --timeout 90
9498
- name: Test Sync API
9599
if: matrix.os == 'ubuntu-latest'
96-
run: xvfb-run pytest -vv tests/sync --browser=${{ matrix.browser }} --timeout 90
100+
run: xvfb-run pytest tests/sync --browser=${{ matrix.browser }} --timeout 90
97101
- name: Test Async API
98102
if: matrix.os != 'ubuntu-latest'
99-
run: pytest -vv tests/async --browser=${{ matrix.browser }} --timeout 90
103+
run: pytest tests/async --browser=${{ matrix.browser }} --timeout 90
100104
- name: Test Async API
101105
if: matrix.os == 'ubuntu-latest'
102-
run: xvfb-run pytest -vv tests/async --browser=${{ matrix.browser }} --timeout 90
106+
run: xvfb-run pytest tests/async --browser=${{ matrix.browser }} --timeout 90
103107

104108
stable:
105109
name: Stable
@@ -148,23 +152,3 @@ jobs:
148152
- name: Test Async API
149153
if: matrix.os == 'ubuntu-latest'
150154
run: xvfb-run pytest -vv tests/async --browser=chromium --browser-channel=${{ matrix.browser-channel }} --timeout 90
151-
152-
test-package-installations:
153-
name: Test package installations
154-
runs-on: ubuntu-latest
155-
timeout-minutes: 30
156-
steps:
157-
- uses: actions/checkout@v2
158-
- name: Set up Python
159-
uses: actions/setup-python@v2
160-
with:
161-
python-version: 3.9
162-
- name: Install dependencies
163-
run: |
164-
python -m pip install --upgrade pip
165-
pip install -r local-requirements.txt
166-
pip install -e .
167-
python setup.py bdist_wheel
168-
python -m playwright install-deps
169-
- name: Test package installation
170-
run: bash buildbots/test-package-installations.sh

buildbots/assets/stub.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

buildbots/assets/test-reference-count-async.py

Lines changed: 0 additions & 45 deletions
This file was deleted.

buildbots/test-package-installations.sh

Lines changed: 0 additions & 30 deletions
This file was deleted.

buildbots/test-reference-count.sh

Lines changed: 0 additions & 25 deletions
This file was deleted.

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[tool:pytest]
2+
addopts = -rsx -vv
23
markers =
34
skip_browser
45
only_browser

tests/assets/client.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright (c) Microsoft Corporation.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from pathlib import Path
16+
17+
from playwright.sync_api import Playwright, sync_playwright
18+
19+
20+
def main(playwright: Playwright) -> None:
21+
for browser_type in [playwright.chromium, playwright.firefox, playwright.webkit]:
22+
browser = browser_type.launch()
23+
page = browser.new_page()
24+
page.goto("https://example.com")
25+
here = Path(__file__).parent.resolve()
26+
page.screenshot(path=here / f"{browser_type.name}.png")
27+
page.close()
28+
browser.close()
29+
30+
31+
if __name__ == "__main__":
32+
with sync_playwright() as p:
33+
main(p)

tests/test_generation_scripts.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,39 @@
1+
# Copyright (c) Microsoft Corporation.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License")
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
import sys
216
from io import StringIO
317
from unittest.mock import patch
418

519
import pytest
620

7-
CAN_RUN_GENERATION_SCRIPT = sys.version_info >= (3, 8)
8-
9-
if CAN_RUN_GENERATION_SCRIPT:
10-
from scripts.generate_async_api import main as generate_async_api
11-
from scripts.generate_sync_api import main as generate_sync_api
21+
pytestmark = pytest.mark.skipif(
22+
sys.version_info < (3, 9), reason="requires python3.9 or higher"
23+
)
1224

1325

14-
@pytest.mark.skipif(
15-
not CAN_RUN_GENERATION_SCRIPT, reason="requires python3.8 or higher"
16-
)
1726
@patch("sys.stderr", new_callable=StringIO)
1827
@patch("sys.stdout", new_callable=StringIO)
1928
def test_generate_sync_api(stdout, stderr):
29+
from scripts.generate_sync_api import main as generate_sync_api
30+
2031
generate_sync_api()
2132

2233

23-
@pytest.mark.skipif(
24-
not CAN_RUN_GENERATION_SCRIPT, reason="requires python3.8 or higher"
25-
)
2634
@patch("sys.stderr", new_callable=StringIO)
2735
@patch("sys.stdout", new_callable=StringIO)
2836
def test_generate_async_api(stdout, stderr):
37+
from scripts.generate_async_api import main as generate_async_api
38+
2939
generate_async_api()

tests/test_installation.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright (c) Microsoft Corporation.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License")
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import shutil
17+
import subprocess
18+
import sys
19+
from pathlib import Path
20+
from venv import EnvBuilder
21+
22+
23+
def test_install(tmp_path: Path):
24+
env = EnvBuilder(with_pip=True)
25+
env.create(env_dir=tmp_path)
26+
context = env.ensure_directories(tmp_path)
27+
root = Path(__file__).parent.parent.resolve()
28+
if sys.platform == "win32":
29+
wheelpath = list((root / "dist").glob("playwright*win_amd64*.whl"))[0]
30+
elif sys.platform == "linux":
31+
wheelpath = list((root / "dist").glob("playwright*manylinux1*.whl"))[0]
32+
elif sys.platform == "darwin":
33+
wheelpath = list((root / "dist").glob("playwright*macosx_10_*.whl"))[0]
34+
subprocess.check_output(
35+
[
36+
context.env_exe,
37+
"-m",
38+
"pip",
39+
"install",
40+
str(wheelpath),
41+
]
42+
)
43+
environ = os.environ.copy()
44+
environ["PLAYWRIGHT_BROWSERS_PATH"] = str(tmp_path)
45+
subprocess.check_output(
46+
[context.env_exe, "-m", "playwright", "install"], env=environ
47+
)
48+
shutil.copyfile(root / "tests" / "assets" / "client.py", tmp_path / "main.py")
49+
subprocess.check_output([context.env_exe, str(tmp_path / "main.py")], env=environ)
50+
assert (tmp_path / "chromium.png").exists()
51+
assert (tmp_path / "firefox.png").exists()
52+
assert (tmp_path / "webkit.png").exists()

tests/test_reference_count_async.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright (c) Microsoft Corporation.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License")
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import gc
16+
17+
import objgraph
18+
import pandas as pd
19+
import pytest
20+
21+
from playwright.async_api import async_playwright
22+
23+
24+
@pytest.mark.asyncio
25+
async def test_memory_objects() -> None:
26+
async with async_playwright() as p:
27+
browser = await p.chromium.launch()
28+
page = await browser.new_page()
29+
await page.goto("https://example.com")
30+
31+
page.on("dialog", lambda dialog: dialog.dismiss())
32+
for _ in range(100):
33+
await page.evaluate("""async () => alert()""")
34+
35+
await page.route("**/", lambda route, _: route.fulfill(body="OK"))
36+
37+
for _ in range(100):
38+
response = await page.evaluate("""async () => (await fetch("/")).text()""")
39+
assert response == "OK"
40+
41+
await browser.close()
42+
43+
gc.collect()
44+
45+
df_dicts = pd.DataFrame()
46+
df_dicts["dicts"] = objgraph.by_type("dict")
47+
df_dicts["pw_types"] = df_dicts["dicts"].apply(lambda x: x.get("_type"))
48+
49+
head = df_dicts["pw_types"].value_counts().head(20)
50+
assert "Dialog" not in head.items()
51+
assert "Request" not in head.items()
52+
assert "Route" not in head.items()

0 commit comments

Comments
 (0)