Fix py-repl auto-generate=true (#910)

What is not tested is broken man_facepalming
Test&fix <py-repl auto-generate=true> which was broken by PR #884
This commit is contained in:
Antonio Cuni
2022-10-31 12:55:20 +01:00
committed by GitHub
parent 4c635fe84c
commit f67b8e0285
4 changed files with 58 additions and 0 deletions

View File

@@ -173,6 +173,8 @@ export function make_PyRepl(runtime: Runtime) {
if (pyResult !== undefined) {
pyDisplay(runtime, pyResult, { target: outEl.id });
}
this.autogenerateMaybe();
}
getPySrc(): string {

View File

@@ -275,6 +275,15 @@ class PyScriptTest:
if wait_for_pyscript:
self.wait_for_pyscript()
def iter_locator(self, loc):
"""
Helper method to iterate over all the elements which are matched by a
locator, since playwright does not seem to support it natively.
"""
n = loc.count()
elems = [loc.nth(i) for i in range(n)]
return iter(elems)
# ============== Helpers and utility functions ==============

View File

@@ -338,3 +338,20 @@ class TestSupport(PyScriptTest):
self.wait_for_console("Page loaded!", timeout=200, check_js_errors=False)
# clear the errors, else the test fails at teardown
self.clear_js_errors()
def test_iter_locator(self):
doc = """
<html>
<body>
<div>foo</div>
<div>bar</div>
<div>baz</div>
</body>
</html>
"""
self.writefile("mytest.html", doc)
self.goto("mytest.html")
divs = self.page.locator("div")
assert divs.count() == 3
texts = [el.inner_text() for el in self.iter_locator(divs)]
assert texts == ["foo", "bar", "baz"]

View File

@@ -229,3 +229,33 @@ class TestPyRepl(PyScriptTest):
msg = "py-repl ERROR: cannot find the output element #I-dont-exist in the DOM"
assert out_div.inner_text() == msg
assert "I will not be executed" not in self.console.log.text
def test_auto_generate(self):
self.pyscript_run(
"""
<py-repl auto-generate="true">
</py-repl>
"""
)
py_repls = self.page.locator("py-repl")
outputs = py_repls.locator("div.py-repl-output")
assert py_repls.count() == 1
assert outputs.count() == 1
#
# evaluate the py-repl, and wait for the newly generated one
self.page.keyboard.type("'hello'")
self.page.keyboard.press("Shift+Enter")
self.page.locator('py-repl[exec-id="2"]').wait_for()
assert py_repls.count() == 2
assert outputs.count() == 2
#
# now we type something else: the new py-repl should have the focus
self.page.keyboard.type("'world'")
self.page.keyboard.press("Shift+Enter")
self.page.locator('py-repl[exec-id="3"]').wait_for()
assert py_repls.count() == 3
assert outputs.count() == 3
#
# check that the code and the outputs are in order
out_texts = [el.inner_text() for el in self.iter_locator(outputs)]
assert out_texts == ["hello", "world", ""]