|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from contextlib import contextmanager |
| 4 | +from time import sleep |
| 5 | + |
| 6 | +import pytest |
| 7 | +from playwright.sync_api import Page, expect |
| 8 | + |
| 9 | +LOCAL_TEST = False |
| 10 | + |
| 11 | +PORT = "8503" if LOCAL_TEST else "8699" |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture(scope="module", autouse=True) |
| 15 | +def _before_module(): |
| 16 | + # Run the streamlit app before each module |
| 17 | + with run_streamlit(): |
| 18 | + yield |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture(autouse=True) |
| 22 | +def _before_test(page: Page): |
| 23 | + page.goto(f"localhost:{PORT}") |
| 24 | + page.set_viewport_size({"width": 2000, "height": 2000}) |
| 25 | + expect.set_options(timeout=5_000) |
| 26 | + |
| 27 | + |
| 28 | +# Take screenshot of each page if there are failures for this session |
| 29 | +@pytest.fixture(autouse=True) |
| 30 | +def _after_test(page: Page, request): |
| 31 | + yield |
| 32 | + if request.node.rep_call.failed: |
| 33 | + page.screenshot(path=f"screenshot-{request.node.name}.png", full_page=True) |
| 34 | + |
| 35 | + |
| 36 | +@contextmanager |
| 37 | +def run_streamlit(): |
| 38 | + """Run the streamlit app at examples/streamlit_app.py on port 8599""" |
| 39 | + import subprocess |
| 40 | + |
| 41 | + if LOCAL_TEST: |
| 42 | + try: |
| 43 | + yield 1 |
| 44 | + finally: |
| 45 | + pass |
| 46 | + else: |
| 47 | + p = subprocess.Popen( |
| 48 | + [ |
| 49 | + "streamlit", |
| 50 | + "run", |
| 51 | + "examples/streamlit/main.py", |
| 52 | + "--server.port", |
| 53 | + PORT, |
| 54 | + "--server.headless", |
| 55 | + "true", |
| 56 | + ] |
| 57 | + ) |
| 58 | + |
| 59 | + sleep(5) |
| 60 | + |
| 61 | + try: |
| 62 | + yield 1 |
| 63 | + finally: |
| 64 | + p.kill() |
| 65 | + |
| 66 | + |
| 67 | +def click_button_or_marker(page: Page, nth: int = 0, locator: str | None = None): |
| 68 | + """For some reason, there's a discrepancy between how the map markers are |
| 69 | + selectable locally and on github actions, perhaps related some error in loading |
| 70 | + the actual marker images. This tries both ways to select a marker""" |
| 71 | + |
| 72 | + frame = page.frame_locator('iframe[title="streamlit_folium\\.st_folium"]') |
| 73 | + if locator is not None: |
| 74 | + frame = frame.locator(locator) |
| 75 | + try: |
| 76 | + frame.get_by_role("button", name="Marker").nth(nth).click(timeout=5_000) |
| 77 | + except Exception: |
| 78 | + frame.get_by_role("img").nth(nth).click(timeout=5_000) |
| 79 | + |
| 80 | + |
| 81 | +def test_draw(page: Page): |
| 82 | + # Test draw support |
| 83 | + page.get_by_role("link", name="draw feature group").click() |
| 84 | + |
| 85 | + # This is the marker that was drawn beforehand |
| 86 | + expect( |
| 87 | + page.locator('[data-testid="stCustomComponentV1"]').content_frame.get_by_role( |
| 88 | + "button", name="Marker" |
| 89 | + ) |
| 90 | + ).to_be_visible() |
| 91 | + |
| 92 | + # Start drawing a rectangle |
| 93 | + page.locator('[data-testid="stCustomComponentV1"]').content_frame.get_by_role( |
| 94 | + "link", name="Draw a rectangle" |
| 95 | + ).click() |
| 96 | + |
| 97 | + # I could not record this |
| 98 | + # so some trickery to get the mouse movements correct |
| 99 | + bbox = page.locator('[data-testid="stCustomComponentV1"]').bounding_box() |
| 100 | + |
| 101 | + # One of the few times I miss javascript |
| 102 | + x = bbox["x"] |
| 103 | + y = bbox["y"] |
| 104 | + width = bbox["width"] |
| 105 | + height = bbox["height"] |
| 106 | + |
| 107 | + # careful, my first click attempt triggered the zoom button |
| 108 | + page.mouse.click(x + 100, y + 100) |
| 109 | + page.mouse.click(x + width - 100, y + height - 100) |
| 110 | + |
| 111 | + # Now check if streamlit shows a Polygon result |
| 112 | + expect(page.get_by_text('"Polygon"').first).to_be_visible() |
0 commit comments