Improve self.wait_for_console() (#1363)

- Previously, if the message appeared on the console immediately before the call to self.wait_for_console(), the call would hang forever until the timeout. Now, it returns immediately.

- `wait_for_pyscript` now logs the time actually taken for waiting

- `wait_for_pyscript` takes a `timeout` argument so that we can tweak it on a test-by-test basis
This commit is contained in:
Antonio Cuni
2023-04-05 16:00:17 +02:00
committed by GitHub
parent 088a264910
commit af981fc719
3 changed files with 77 additions and 25 deletions

View File

@@ -2,7 +2,6 @@ import re
import textwrap
import pytest
from playwright import sync_api
from .support import JsErrors, JsErrorsDidNotRaise, PyScriptTest
@@ -266,7 +265,7 @@ class TestSupport(PyScriptTest):
# cleared
self.check_js_errors()
def test_wait_for_console(self):
def test_wait_for_console_simple(self):
"""
Test that self.wait_for_console actually waits.
If it's buggy, the test will try to read self.console.log BEFORE the
@@ -285,11 +284,46 @@ class TestSupport(PyScriptTest):
"""
self.writefile("mytest.html", doc)
self.goto("mytest.html")
# we use a timeout of 500ms to give plenty of time to the page to
# we use a timeout of 200ms to give plenty of time to the page to
# actually run the setTimeout callback
self.wait_for_console("Page loaded!", timeout=200)
assert self.console.log.lines[-1] == "Page loaded!"
def test_wait_for_console_timeout(self):
doc = """
<html>
<body>
</body>
</html>
"""
self.writefile("mytest.html", doc)
self.goto("mytest.html")
with pytest.raises(TimeoutError):
self.wait_for_console("This text will never be printed", timeout=200)
def test_wait_for_console_dont_wait_if_already_emitted(self):
"""
If the text is already on the console, wait_for_console() should return
immediately without waiting.
"""
doc = """
<html>
<body>
<script>
console.log('Hello world')
console.log('Page loaded!');
</script>
</body>
</html>
"""
self.writefile("mytest.html", doc)
self.goto("mytest.html")
self.wait_for_console("Page loaded!", timeout=200)
assert self.console.log.lines[-2] == "Hello world"
assert self.console.log.lines[-1] == "Page loaded!"
# the following call should return immediately without waiting
self.wait_for_console("Hello world", timeout=1)
def test_wait_for_console_exception_1(self):
"""
Test that if a JS exception is raised while waiting for the console, we
@@ -316,12 +350,12 @@ class TestSupport(PyScriptTest):
with pytest.raises(JsErrors) as exc:
self.wait_for_console("Page loaded!", timeout=200)
assert "this is an error" in str(exc.value)
assert isinstance(exc.value.__context__, sync_api.TimeoutError)
assert isinstance(exc.value.__context__, TimeoutError)
#
# if we use check_js_errors=False, the error are ignored, but we get the
# Timeout anyway
self.goto("mytest.html")
with pytest.raises(sync_api.TimeoutError):
with pytest.raises(TimeoutError):
self.wait_for_console("Page loaded!", timeout=200, check_js_errors=False)
# we still got a JsErrors, so we need to manually clear it, else the
# test fails at teardown