import pytest from .support import PyScriptTest, skip_worker pytest.skip(reason="NEXT: entire stdio should be reviewed", allow_module_level=True) class TestOutputHandling(PyScriptTest): # Source of a script to test the TargetedStdio functionality def test_targeted_stdio_solo(self): self.pyscript_run( """ terminal = true
""" ) # Check that page has desired parent/child structure, and that # Output divs are correctly located assert (container := self.page.locator("#container")).count() > 0 assert (first_div := container.locator("#first")).count() > 0 assert (second_div := container.locator("#second")).count() > 0 assert (third_div := container.locator("#third")).count() > 0 # Check that output ends up in proper div assert first_div.text_content() == "first 1.first 2." assert second_div.text_content() == "second." assert third_div.text_content() == "third." # Check that tag with no otuput attribute doesn't end up in container at all assert container.get_by_text("no output.").count() == 0 # Check that all output ends up in py-terminal assert ( self.page.locator("py-terminal").text_content() == "first 1.second.third.first 2.no output." ) # Check that all output ends up in the dev console, in order last_index = -1 for line in ["first 1.", "second.", "third.", "first 2.", "no output."]: assert (line_index := self.console.log.lines.index(line)) > -1 assert line_index > last_index last_index = line_index self.assert_no_banners() def test_stdio_escape(self): # Test that text that looks like HTML tags is properly escaped in stdio self.pyscript_run( """
""" ) text = self.page.locator("#first").text_content() assert "

Hello

" in text assert '' in text self.assert_no_banners() def test_targeted_stdio_linebreaks(self): self.pyscript_run( """
""" ) # check line breaks at end of each input assert self.page.locator("#first").inner_html() == "one.
two.
three.
" # new lines are converted to line breaks assert self.page.locator("#second").inner_html() == "one.
two.
three.
" self.assert_no_banners() def test_targeted_stdio_async(self): # Test the behavior of stdio capture in async contexts self.pyscript_run( """
""" ) self.wait_for_console("DONE DONE") # script tags without output parameter should not send # stdout to element assert self.page.locator("#first").text_content() == "" # script tags with output parameter not expected to send # std to element in coroutine assert self.page.locator("#second").text_content() == "" assert self.page.locator("#third").text_content() == "" self.assert_no_banners() def test_targeted_stdio_interleaved(self): # Test that synchronous writes to stdout are placed correctly, even # While interleaved with scheduling coroutines in the same tag self.pyscript_run( """
""" ) # Three prints should appear from synchronous writes assert self.page.locator("#good").text_content() == "one.two.three." # Check that all output ends up in the dev console, in order last_index = -1 for line in ["one.", "two.", "three.", "badthree.", "badone.", "badtwo."]: assert (line_index := self.console.log.lines.index(line)) > -1 assert line_index > last_index self.assert_no_banners() @skip_worker("FIXME: js.document") def test_targeted_stdio_dynamic_tags(self): # Test that creating py-script tags via Python still leaves # stdio targets working self.pyscript_run( """
""" ) # Ensure second tag was added to page assert (second_div := self.page.locator("#second")).count() > 0 # Ensure output when to correct locations assert self.page.locator("#first").text_content() == "first.first." assert second_div.text_content() == "second." self.assert_no_banners() def test_stdio_stdout_id_errors(self): # Test that using an ID not present on the page as the Output # Attribute creates exactly 1 warning banner per missing id self.pyscript_run( """
""" ) banner = self.page.query_selector_all(".py-warning") assert len(banner) == 1 banner_content = banner[0].inner_text() expected = ( 'output = "not-on-page" does not match the id of any element on the page.' ) assert banner_content == expected def test_stdio_stderr_id_errors(self): # Test that using an ID not present on the page as the stderr # attribute creates exactly 1 warning banner per missing id self.pyscript_run( """
""" ) banner = self.page.query_selector_all(".py-warning") assert len(banner) == 1 banner_content = banner[0].inner_text() expected = ( 'stderr = "not-on-page" does not match the id of any element on the page.' ) assert banner_content == expected def test_stdio_stderr(self): # Test that stderr works, and routes to the same location as stdout # Also, script tags with the stderr attribute route to an additional location self.pyscript_run( """
""" ) assert self.page.locator("#stdout-div").text_content() == "one.two." assert self.page.locator("#stderr-div").text_content() == "one." self.assert_no_banners() @skip_worker("FIXME: js.document") def test_stdio_output_attribute_change(self): # If the user changes the 'output' attribute of a """ ) assert self.page.locator("#first").text_content() == "one." assert self.page.locator("#second").text_content() == "two." expected_alert_banner_msg = ( 'output = "third" does not match the id of any element on the page.' ) alert_banner = self.page.locator(".alert-banner") assert expected_alert_banner_msg in alert_banner.inner_text() @skip_worker("FIXME: js.document") def test_stdio_target_element_id_change(self): # If the user changes the ID of the targeted DOM element mid-execution, # Output should no longer go to the selected element and a warning should appear self.pyscript_run( """
""" ) # Note the ID of the div has changed by the time of this assert assert self.page.locator("#third").text_content() == "one.three." expected_alert_banner_msg = ( 'output = "first" does not match the id of any element on the page.' ) alert_banner = self.page.locator(".alert-banner") assert expected_alert_banner_msg in alert_banner.inner_text()