Files
pyscript/pyscript.core/tests/integration/test_splashscreen.py
Antonio Cuni abfc68765f Enable worker tests (#1757)
This PR re-enables tests on `worker`s. Highlights:
 
* by default, each test is run twice: the main thread version uses `<script type="py">`, the worker version automatically turn the tags into `<script type="py" worker>`

* you can tweak the settings per-class by using the `@with_execution_thread` decorator. In particular, `@with_execution_thread(None)` is for those tests which don't care about it (e.g., `test_py_config.py`)

* inside each class, there might be some test which should be run only in the main thread (because it doesn't make sense to test it in a worker). For those, I introduced the `@only_main` decorator

* we might introduce `@only_worker` in the future, if needed

* `@skip_worker` is for those tests which currently pass on main but not on workers. These are meant to be temporary, and eventually they should all be fixed
 
During the process, I tweaked/improved/fixed/deleted some of the existing tests. Some of them were at risk of being flaky and I made them more robust, others depended on some very precise implementation detail, and I made them more generic (for example, `test_image_renders_correctly` relied on pillow to render an image with a very specific string of bytes, and it broke due to the recent upgrade to pyodide 0.24.1)
 
I also renamed all the skip messages to start with `NEXT`, so that they are easier to grep.
2023-09-27 10:05:40 +02:00

123 lines
4.0 KiB
Python

import pytest
from playwright.sync_api import expect
from .support import PyScriptTest, skip_worker
pytest.skip(reason="NEXT: Should we remove the splashscreen?", allow_module_level=True)
class TestSplashscreen(PyScriptTest):
def test_autoshow_and_autoclose(self):
"""
By default, we show the splashscreen and we close it when the loading is
complete.
XXX: this test is a bit fragile: now it works reliably because the
startup is so slow that when we do expect(div).to_be_visible(), the
splashscreen is still there. But in theory, if the startup become very
fast, it could happen that by the time we arrive in python lang, it
has already been removed.
"""
self.pyscript_run(
"""
<script type="py">
print('hello pyscript')
</script>
""",
wait_for_pyscript=False,
)
div = self.page.locator("py-splashscreen > div")
expect(div).to_be_visible()
expect(div).to_contain_text("Python startup...")
assert "Python startup..." in self.console.info.text
#
# now we wait for the startup to complete
self.wait_for_pyscript()
#
# and now the splashscreen should have been removed
expect(div).to_be_hidden()
assert self.page.locator("py-locator").count() == 0
assert "hello pyscript" in self.console.log.lines
def test_autoclose_false(self):
self.pyscript_run(
"""
<py-config>
[splashscreen]
autoclose = false
</py-config>
<script type="py">
print('hello pyscript')
</script>
""",
)
div = self.page.locator("py-splashscreen > div")
expect(div).to_be_visible()
expect(div).to_contain_text("Python startup...")
expect(div).to_contain_text("Startup complete")
assert "hello pyscript" in self.console.log.lines
def test_autoclose_loader_deprecated(self):
self.pyscript_run(
"""
<py-config>
autoclose_loader = false
</py-config>
<script type="py">
print('hello pyscript')
</script>
""",
)
warning = self.page.locator(".py-warning")
inner_text = warning.inner_html()
assert "The setting autoclose_loader is deprecated" in inner_text
div = self.page.locator("py-splashscreen > div")
expect(div).to_be_visible()
expect(div).to_contain_text("Python startup...")
expect(div).to_contain_text("Startup complete")
assert "hello pyscript" in self.console.log.lines
def test_splashscreen_disabled_option(self):
self.pyscript_run(
"""
<py-config>
[splashscreen]
enabled = false
</py-config>
<script type="py">
def test():
print("Hello pyscript!")
test()
</script>
""",
)
assert self.page.locator("py-splashscreen").count() == 0
assert self.console.log.lines[-1] == "Hello pyscript!"
py_terminal = self.page.wait_for_selector("py-terminal")
assert py_terminal.inner_text() == "Hello pyscript!\n"
@skip_worker("FIXME: js.document")
def test_splashscreen_custom_message(self):
self.pyscript_run(
"""
<py-config>
[splashscreen]
autoclose = false
</py-config>
<script type="py">
from js import document
splashscreen = document.querySelector("py-splashscreen")
splashscreen.log("Hello, world!")
</script>
""",
)
splashscreen = self.page.locator("py-splashscreen")
assert splashscreen.count() == 1
assert "Hello, world!" in splashscreen.inner_text()