nested text
Content
First
Second
Third
import pytest from fasthtml.components import Div from fasthtml.fastapp import fast_app from myfasthtml.core.testclient import MyTestClient, TestableElement def test_i_can_open_a_page(): test_app, rt = fast_app(default_hdrs=False) client = MyTestClient(test_app) @rt('/') def get(): return "hello world" client.open("/") assert client.get_content() == "hello world" def test_i_can_open_a_page_when_html(): test_app, rt = fast_app(default_hdrs=False) client = MyTestClient(test_app) @rt('/') def get(): return Div("hello world") client.open("/") assert client.get_content() == ' \n \n
\nThis is a test
" client.open("/").should_see("Welcome").should_see("This is a test") def test_i_can_see_text_ignoring_html_tags(): """Test that should_see() searches in visible text only, not in HTML tags.""" test_app, rt = fast_app(default_hdrs=False) client = MyTestClient(test_app) @rt('/') def get(): return 'Content here
" # Chain multiple assertions client.open("/").should_see("Welcome").should_see("Content").should_not_see("Error") def test_i_can_see_element_context_when_text_should_not_be_seen(): """Test that the HTML element containing the text is displayed with parent context.""" test_app, rt = fast_app(default_hdrs=False) client = MyTestClient(test_app) @rt('/') def get(): return 'forbidden text
forbidden text
' in error_message assert 'error
error
' in error_message assert 'nested text
element, not the outer
nested text
' in error_message def test_i_can_find_fragmented_text_across_tags(): """Test that text fragmented across multiple tags is correctly found.""" test_app, rt = fast_app(default_hdrs=False) client = MyTestClient(test_app) @rt('/') def get(): return '' with pytest.raises(AssertionError) as exc_info: client.open("/").should_not_see("hello world") error_message = str(exc_info.value) # Should find the parentelement that contains the full text assert '
' # "error" is in attributes but not in visible text client.open("/").should_not_see("error") # "Success" is in visible text with pytest.raises(AssertionError): client.should_not_see("Success") @pytest.mark.parametrize("selector,expected_tag", [ ("#unique-id", 'Content
First
Second
Third