Simplify tests with pytest-playwright (#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>
This commit is contained in:
Philipp Rudiger
2022-06-21 19:28:05 +02:00
committed by GitHub
parent 1c7ef6622b
commit 829cc9f6f9
5 changed files with 43 additions and 52 deletions

View File

@@ -2,8 +2,8 @@ name: '[CI] Build Release'
on:
push:
tags:
- '[0-9][0-9][0-9][0-9].[0-9][0-9].[0-9]'
tags:
- '[0-9][0-9][0-9][0-9].[0-9][0-9].[0-9]'
env:
MINICONDA_PYTHON_VERSION: py38

View File

@@ -58,5 +58,5 @@ jobs:
- name: Sync to S3
run: | # Overwrite "latest" alpha + versioned subdirectory
aws s3 sync --quiet ./examples/build/ s3://pyscript.net/alpha/${{ github.ref_name }}
aws s3 sync --quiet ./examples/build/ s3://pyscript.net/alpha/${{ github.ref_name }}
# aws s3 sync --quiet ./examples/build/ s3://pyscript.net/alpha/

View File

@@ -14,13 +14,6 @@
- paths:
- ./utils.py
</py-env>
<py-config>
autoclose_loader: false
runtimes:
- src: "https://cdn.jsdelivr.net/pyodide/v0.20.0/full/pyodide.js"
name: pyodide-0.20
lang: python
</py-config>
</head>
<body>
@@ -42,7 +35,7 @@ async def foo():
while True:
await asyncio.sleep(1)
output = now()
pyscript.write("outputDiv2", output)
Element("outputDiv2").write(output)
out3 = Element("outputDiv3")
if output[-1] in ["0", "4", "8"]:

View File

@@ -12,3 +12,6 @@ dependencies:
- codespell
- pre-commit
- playwright
- pip:
- pytest-playwright

View File

@@ -16,7 +16,6 @@ import time
from urllib.parse import urljoin
import pytest
from playwright.sync_api import sync_playwright
MAX_TEST_TIME = 30 # Number of seconds allowed for checking a testing condition
TEST_TIME_INCREMENT = 0.25 # 1/4 second, the length of each iteration
@@ -147,57 +146,53 @@ TEST_PARAMS = {
@pytest.mark.parametrize("example", EXAMPLES)
def test_examples(example, http_server):
def test_examples(example, http_server, page):
base_url = http_server
example_path = urljoin(base_url, TEST_PARAMS[example]["file"])
# Invoke playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto(example_path)
page.goto(example_path, wait_until="commit")
# STEP 1: Check page title proper initial loading of the example page
content = page.text_content("*")
title = page.title()
expected_title = TEST_PARAMS[example]["title"]
if isinstance(expected_title, list):
# One example's title changes so expected_title is a list of possible
# titles in that case
assert page.title() in expected_title # nosec
else:
assert page.title() == expected_title # nosec
# STEP 1: Check page title proper initial loading of the example page
expected_title = TEST_PARAMS[example]["title"]
if isinstance(expected_title, list):
# One example's title changes so expected_title is a list of possible
# titles in that case
assert title in expected_title # nosec
else:
assert title == expected_title # nosec
# STEP 2: Test that pyodide is loading via messages displayed during loading
# STEP 2: Test that pyodide is loading via messages displayed during loading
pyodide_loading = False # Flag to be set to True when condition met
pyodide_loading = False # Flag to be set to True when condition met
for _ in range(TEST_ITERATIONS):
time.sleep(TEST_TIME_INCREMENT)
content = page.text_content("*")
for message in LOADING_MESSAGES:
if message in content:
pyodide_loading = True
if pyodide_loading:
break
for _ in range(TEST_ITERATIONS):
for message in LOADING_MESSAGES:
if message in content:
pyodide_loading = True
if pyodide_loading:
break
content = page.text_content("*")
time.sleep(TEST_TIME_INCREMENT)
assert pyodide_loading # nosec
assert pyodide_loading # nosec
# STEP 3:
# Assert that rendering inserts data into the page as expected: search the
# DOM from within the timing loop for a string that is not present in the
# initial markup but should appear by way of rendering
# STEP 3:
# Assert that rendering inserts data into the page as expected: search the
# DOM from within the timing loop for a string that is not present in the
# initial markup but should appear by way of rendering
re_sub_content = re.compile(TEST_PARAMS[example]["pattern"])
py_rendered = False # Flag to be set to True when condition met
re_sub_content = re.compile(TEST_PARAMS[example]["pattern"])
py_rendered = False # Flag to be set to True when condition met
for _ in range(TEST_ITERATIONS):
time.sleep(TEST_TIME_INCREMENT)
content = page.inner_html("*")
if re_sub_content.search(content):
py_rendered = True
break
for _ in range(TEST_ITERATIONS):
time.sleep(TEST_TIME_INCREMENT)
content = page.inner_html("*")
if re_sub_content.search(content):
py_rendered = True
break
assert py_rendered # nosec
browser.close()
assert py_rendered # nosec