|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import pytest |
| 4 | +from e2e_utils import StreamlitRunner |
| 5 | +from playwright.sync_api import Page, expect |
| 6 | + |
| 7 | +ROOT_DIRECTORY = Path(__file__).parent.parent.absolute() |
| 8 | +BASIC_EXAMPLE_FILE = ROOT_DIRECTORY / "my_component" / "example.py" |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture(autouse=True, scope="module") |
| 12 | +def streamlit_app(): |
| 13 | + with StreamlitRunner(BASIC_EXAMPLE_FILE) as runner: |
| 14 | + yield runner |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture(autouse=True, scope="function") |
| 18 | +def go_to_app(page: Page, streamlit_app: StreamlitRunner): |
| 19 | + page.goto(streamlit_app.server_url) |
| 20 | + # Wait for app to load |
| 21 | + page.get_by_role("img", name="Running...").is_hidden() |
| 22 | + |
| 23 | + |
| 24 | +def test_should_render_template(page: Page): |
| 25 | + frame_0 = page.frame_locator('iframe[title="my_component\\.my_component"]').nth(0) |
| 26 | + frame_1 = page.frame_locator('iframe[title="my_component\\.my_component"]').nth(1) |
| 27 | + |
| 28 | + st_markdown_0 = page.get_by_role("paragraph").nth(0) |
| 29 | + st_markdown_1 = page.get_by_role("paragraph").nth(1) |
| 30 | + |
| 31 | + expect(st_markdown_0).to_contain_text("You've clicked 0 times!") |
| 32 | + |
| 33 | + frame_0.get_by_role("button", name="Click me!").click() |
| 34 | + |
| 35 | + expect(st_markdown_0).to_contain_text("You've clicked 1 times!") |
| 36 | + expect(st_markdown_1).to_contain_text("You've clicked 0 times!") |
| 37 | + |
| 38 | + frame_1.get_by_role("button", name="Click me!").click() |
| 39 | + frame_1.get_by_role("button", name="Click me!").click() |
| 40 | + |
| 41 | + expect(st_markdown_0).to_contain_text("You've clicked 1 times!") |
| 42 | + expect(st_markdown_1).to_contain_text("You've clicked 2 times!") |
| 43 | + |
| 44 | + page.get_by_label("Enter a name").click() |
| 45 | + page.get_by_label("Enter a name").fill("World") |
| 46 | + page.get_by_label("Enter a name").press("Enter") |
| 47 | + |
| 48 | + expect(frame_1.get_by_text("Hello, World!")).to_be_visible() |
| 49 | + |
| 50 | + frame_1.get_by_role("button", name="Click me!").click() |
| 51 | + |
| 52 | + expect(st_markdown_0).to_contain_text("You've clicked 1 times!") |
| 53 | + expect(st_markdown_1).to_contain_text("You've clicked 3 times!") |
| 54 | + |
| 55 | + |
| 56 | +def test_should_change_iframe_height(page: Page): |
| 57 | + frame = page.frame_locator('iframe[title="my_component\\.my_component"]').nth(1) |
| 58 | + |
| 59 | + expect(frame.get_by_text("Hello, Streamlit!")).to_be_visible() |
| 60 | + |
| 61 | + locator = page.locator('iframe[title="my_component\\.my_component"]').nth(1) |
| 62 | + |
| 63 | + page.wait_for_timeout(1000) |
| 64 | + init_frame_height = locator.bounding_box()["height"] |
| 65 | + assert init_frame_height != 0 |
| 66 | + |
| 67 | + page.get_by_label("Enter a name").click() |
| 68 | + |
| 69 | + page.get_by_label("Enter a name").fill(35 * "Streamlit ") |
| 70 | + page.get_by_label("Enter a name").press("Enter") |
| 71 | + |
| 72 | + expect(frame.get_by_text("Streamlit Streamlit Streamlit")).to_be_visible() |
| 73 | + |
| 74 | + page.wait_for_timeout(1000) |
| 75 | + frame_height = locator.bounding_box()["height"] |
| 76 | + assert frame_height > init_frame_height |
| 77 | + |
| 78 | + page.set_viewport_size({"width": 150, "height": 150}) |
| 79 | + |
| 80 | + expect(frame.get_by_text("Streamlit Streamlit Streamlit")).not_to_be_in_viewport() |
| 81 | + |
| 82 | + page.wait_for_timeout(1000) |
| 83 | + frame_height_after_viewport_change = locator.bounding_box()["height"] |
| 84 | + assert frame_height_after_viewport_change > frame_height |
0 commit comments