8000 Simplify tests with pytest-playwright (#523) · highb/pyscript@829cc9f · GitHub
[go: up one dir, main page]

Skip to content

Commit 829cc9f

Browse files
Simplify tests with pytest-playwright (pyscript#523)
* Simplify tests with pytest-playwright * Add pytest-playwright to pip * Fix simple_clock example * Attempt to make example tests more robust * Make pre-commit happy * Revert accidental change to simple_clock pyscript resources * Another attempt at reducing flakeyness * Do not wait until page load event is fired * Remove pointless py-config Co-authored-by: mariana <marianameireles@protonmail.com>
1 parent 1c7ef66 commit 829cc9f

File tree

5 files changed

+43
-52
lines changed

5 files changed

+43
-52
lines changed

.github/workflows/build-release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ name: '[CI] Build Release'
22

33
on:
44
push:
5-
tags:
6-
- '[0-9][0-9][0-9][0-9].[0-9][0-9].[0-9]'
5+
tags:
6+
- '[0-9][0-9][0-9][0-9].[0-9][0-9].[0-9]'
77

88
env:
99
MINICONDA_PYTHON_VERSION: py38

.github/workflows/publish-release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@ jobs:
5858

5959
- name: Sync to S3
6060
run: | # Overwrite "latest" alpha + versioned subdirectory
61-
aws s3 sync --quiet ./examples/build/ s3://pyscript.net/alpha/${{ github.ref_name }}
61+
aws s3 sync --quiet ./examples/build/ s3://pyscript.net/alpha/${{ github.ref_name }}
6262
# aws s3 sync --quiet ./examples/build/ s3://pyscript.net/alpha/

examples/simple_clock.html

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@
1414< 10000 /td>
- paths:
1515
- ./utils.py
1616
</py-env>
17-
<py-config>
18-
autoclose_loader: false
19-
runtimes:
20-
- src: "https://cdn.jsdelivr.net/pyodide/v0.20.0/full/pyodide.js"
21-
name: pyodide-0.20
22-
lang: python
23-
</py-config>
2417
</head>
2518

2619
<body>
@@ -42,7 +35,7 @@
4235
while True:
4336
await asyncio.sleep(1)
4437
output = now()
45-
pyscript.write("outputDiv2", output)
38+
Element("outputDiv2").write(output)
4639

4740
out3 = Element("outputDiv3")
4841
if output[-1] in ["0", "4", "8"]:

pyscriptjs/environment.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ dependencies:
1212
- codespell
1313
- pre-commit
1414
- playwright
15+
16+
- pip:
17+
- pytest-playwright

pyscriptjs/tests/test_examples.py

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from urllib.parse import urljoin
1717

1818
import pytest
19-
from playwright.sync_api import sync_playwright
2019

2120
MAX_TEST_TIME = 30 # Number of seconds allowed for checking a testing condition
2221
TEST_TIME_INCREMENT = 0.25 # 1/4 second, the length of each iteration
@@ -147,57 +146,53 @@
147146

148147

149148
@pytest.mark.parametrize("example", EXAMPLES)
150-
def test_examples(example, http_server):
149+
def test_examples(example, http_server, page):
151150

152151
base_url = http_server
153152
example_path = urljoin(base_url, TEST_PARAMS[example]["file"])
154153

155-
# Invoke playwright
156-
with sync_playwright() as p:
157-
browser = p.chromium.launch()
158-
page = browser.new_page()
159-
page.goto(example_path)
154+
page.goto(example_path, wait_until="commit")
160155

161-
# STEP 1: Check page title proper initial loading of the example page
156+
content = page.text_content("*")
157+
title = page.title()
162158

163-
expected_title = TEST_PARAMS[example]["title"]
164-
if isinstance(expected_title, list):
165-
# One example's title changes so expected_title is a list of possible
166-
# titles in that case
167-
assert page.title() in expected_title # nosec
168-
else:
169-
assert page.title() == expected_title # nosec
159+
# STEP 1: Check page title proper initial loading of the example page
160+
expected_title = TEST_PARAMS[example]["title"]
161+
if isinstance(expected_title, list):
162+
# One example's title changes so expected_title is a list of possible
163+
# titles in that case
164+
assert title in expected_title # nosec
165+
else:
166+
assert title == expected_title # nosec
170167

171-
# STEP 2: Test that pyodide is loading via messages displayed during loading
168+
# STEP 2: Test that pyodide is loading via messages displayed during loading
172169

173-
pyodide_loading = False # Flag to be set to True when condition met
170+
pyodide_loading = False # Flag to be set to True when condition met
174171

175-
for _ in range(TEST_ITERATIONS):
176-
time.sleep(TEST_TIME_INCREMENT)
177-
content = page.text_content("*")
178-
for message in LOADING_MESSAGES:
179-
if message in content:
180-
pyodide_loading = True
181-
if pyodide_loading:
182-
break
172+
for _ in range(TEST_ITERATIONS):
173+
for message in LOADING_MESSAGES:
174+
if message in content:
175+
pyodide_loading = True
176+
if pyodide_loading:
177+
break
178+
content = page.text_content("*")
179+
time.sleep(TEST_TIME_INCREMENT)
183180

184-
assert pyodide_loading # nosec
181+
assert pyodide_loading # nosec
185182

186-
# STEP 3:
187-
# Assert that rendering inserts data into the page as expected: search the
188-
# DOM from within the timing loop for a string that is not present in the
189-
# initial markup but should appear by way of rendering
183+
# STEP 3:
184+
# Assert that rendering inserts data into the page as expected: search the
185+
# DOM from within the timing loop for a string that is not present in the
186+
# initial markup but should appear by way of rendering
190187

191-
re_sub_content = re.compile(TEST_PARAMS[example]["pattern"])
192-
py_rendered = False # Flag to be set to True when condition met
188+
re_sub_content = re.compile(TEST_PARAMS[example]["pattern"])
189+
py_rendered = False # Flag to be set to True when condition met
193190

194-
for _ in range(TEST_ITERATIONS):
195-
time.sleep(TEST_TIME_INCREMENT)
196-
content = page.inner_html("*")
197-
if re_sub_content.search(content):
198-
py_rendered = True
199-
break
191+
for _ in range(TEST_ITERATIONS):
192+
time.sleep(TEST_TIME_INCREMENT)
193+
content = page.inner_html("*")
194+
if re_sub_content.search(content):
195+
py_rendered = True
196+
break
200197

201-
assert py_rendered # nosec
202-
203-
browser.close()
198+
assert py_rendered # nosec

0 commit comments

Comments
 (0)
0