mirror of
https://github.com/pyscript/pyscript.git
synced 2026-02-13 16:00:37 -05:00
Fix Flaky test_execute_on_shift_enter (#1097)
* Wait for text to render in DOM before 'assert', to fix timing issue.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import dataclasses
|
||||
import math
|
||||
import os
|
||||
import pdb
|
||||
import re
|
||||
@@ -372,6 +373,31 @@ class PyScriptTest:
|
||||
|
||||
# ============== Helpers and utility functions ==============
|
||||
|
||||
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
|
||||
TEST_ITERATIONS = math.ceil(
|
||||
MAX_TEST_TIME / TEST_TIME_INCREMENT
|
||||
) # 120 iters of 1/4 second
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
class JsErrors(Exception):
|
||||
"""
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from .support import PyScriptTest
|
||||
from .support import PyScriptTest, wait_for_render
|
||||
|
||||
|
||||
class TestPyRepl(PyScriptTest):
|
||||
@@ -65,6 +65,7 @@ class TestPyRepl(PyScriptTest):
|
||||
)
|
||||
self.page.wait_for_selector("#runButton")
|
||||
self.page.keyboard.press("Shift+Enter")
|
||||
wait_for_render(self.page, "*", "hello world")
|
||||
|
||||
assert self.console.log.lines[0] == self.PY_COMPLETE
|
||||
assert self.console.log.lines[-1] == "hello world"
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import base64
|
||||
import io
|
||||
import math
|
||||
import os
|
||||
import re
|
||||
import time
|
||||
@@ -9,32 +8,7 @@ import numpy as np
|
||||
import pytest
|
||||
from PIL import Image
|
||||
|
||||
from .support import ROOT, PyScriptTest
|
||||
|
||||
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
|
||||
TEST_ITERATIONS = math.ceil(
|
||||
MAX_TEST_TIME / TEST_TIME_INCREMENT
|
||||
) # 120 iters of 1/4 second
|
||||
|
||||
|
||||
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
|
||||
from .support import ROOT, PyScriptTest, wait_for_render
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("chdir")
|
||||
|
||||
Reference in New Issue
Block a user