Add tests verifying correct example behavior (#625)

This commit is contained in:
Philipp Rudiger
2022-07-29 20:42:49 +02:00
committed by GitHub
parent 677d708588
commit eb31978488
2 changed files with 56 additions and 29 deletions

View File

@@ -25,13 +25,14 @@ repos:
hooks: hooks:
- id: bandit - id: bandit
args: args:
- --skip=B201 - --skip=B101,B201
- repo: https://github.com/psf/black - repo: https://github.com/psf/black
rev: 22.6.0 rev: 22.6.0
hooks: hooks:
- id: black - id: black
- repo: https://github.com/codespell-project/codespell - repo: https://github.com/codespell-project/codespell
rev: v2.1.0 rev: v2.1.0
hooks: hooks:

View File

@@ -145,15 +145,50 @@ TEST_PARAMS = {
} }
def wait_for_load(page):
"""
Assert that pyscript loading messages appear.
"""
pyodide_loading = False # Flag to be set to True when condition met
for _ in range(TEST_ITERATIONS):
content = page.text_content("*")
for message in LOADING_MESSAGES:
if message in content:
pyodide_loading = True
if pyodide_loading:
break
time.sleep(TEST_TIME_INCREMENT)
assert pyodide_loading # nosec
def wait_for_render(page, selector, pattern):
"""
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(pattern)
py_rendered = False # Flag to be set to True when condition met
for _ in range(TEST_ITERATIONS):
content = page.inner_html(selector)
if re_sub_content.search(content):
py_rendered = True
break
time.sleep(TEST_TIME_INCREMENT)
assert py_rendered # nosec
@pytest.mark.parametrize("example", EXAMPLES) @pytest.mark.parametrize("example", EXAMPLES)
def test_examples(example, http_server, page): def test_examples(example, http_server, page):
base_url = http_server base_url = http_server
example_path = urljoin(base_url, TEST_PARAMS[example]["file"]) example_path = urljoin(base_url, TEST_PARAMS[example]["file"])
page.goto(example_path, wait_until="commit") page.goto(example_path, wait_until="commit")
content = page.text_content("*")
title = page.title() title = page.title()
# STEP 1: Check page title proper initial loading of the example page # STEP 1: Check page title proper initial loading of the example page
@@ -166,33 +201,24 @@ def test_examples(example, http_server, page):
assert title == expected_title # nosec 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
wait_for_load(page)
pyodide_loading = False # Flag to be set to True when condition met # Step 3: Wait for expected pattern to appear on page
wait_for_render(page, "*", TEST_PARAMS[example]["pattern"])
def test_simple_clock(http_server, page):
example_path = urljoin(http_server, TEST_PARAMS["simple_clock"]["file"])
page.goto(example_path, wait_until="commit")
wait_for_load(page)
pattern = r"\d{2}/\d{2}/\d{4}, \d{2}:\d{2}:\d{2}"
for _ in range(TEST_ITERATIONS): for _ in range(TEST_ITERATIONS):
for message in LOADING_MESSAGES: content = page.inner_html("#outputDiv2")
if message in content: if re.match(pattern, content) and int(content[-1]) in (0, 4, 8):
pyodide_loading = True assert page.inner_html("#outputDiv3") == "It's espresso time!"
if pyodide_loading:
break break
content = page.text_content("*") else:
time.sleep(TEST_TIME_INCREMENT) time.sleep(TEST_TIME_INCREMENT)
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
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
assert py_rendered # nosec