|
| 1 | +# Copyright 2025 Streamlit PDF Component |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""E2E regression test for streamlit/streamlit#14917. |
| 17 | +
|
| 18 | +When an ``st.dialog`` opened from a custom component is closed, the parent page |
| 19 | +scroll (``section[data-testid="stMain"]``) must be preserved. |
| 20 | +
|
| 21 | +Measured behaviour of this exact scenario (click a page-2 annotation, which scrolls |
| 22 | +the page down, then close the dialog) across Streamlit versions: |
| 23 | + - <= 1.40.2 : preserved (pre-regression) |
| 24 | + - 1.41.0 - 1.58.0 : parent scroll resets to 0 on dialog close (the #14917 bug) |
| 25 | + - >= 1.59.0 : preserved (fixed upstream) |
| 26 | +
|
| 27 | +The test asserts the desired invariant (scroll preserved), so it PASSES on fixed |
| 28 | +Streamlit (>= 1.59) and FAILS on the affected range, acting as a compatibility guard. |
| 29 | +Skipped where ``st.dialog`` is unavailable (Streamlit < 1.37). |
| 30 | +""" |
| 31 | + |
| 32 | +import os |
| 33 | +from pathlib import Path |
| 34 | + |
| 35 | +import pytest |
| 36 | +import streamlit as st |
| 37 | +from playwright.sync_api import Page, expect |
| 38 | + |
| 39 | +from tests import ROOT_DIRECTORY |
| 40 | +from tests.e2e_utils import StreamlitRunner |
| 41 | + |
| 42 | +APP_FILE = os.path.join(ROOT_DIRECTORY, "tests", "streamlit_apps", "example_dialog_scroll.py") |
| 43 | + |
| 44 | +pytestmark = pytest.mark.skipif( |
| 45 | + not hasattr(st, "dialog"), |
| 46 | + reason="st.dialog (required to reproduce #14917) is unavailable on this Streamlit", |
| 47 | +) |
| 48 | + |
| 49 | +STMAIN = 'section[data-testid="stMain"]' |
| 50 | +IFRAME = 'iframe[title="streamlit_pdf_viewer.streamlit_pdf_viewer"]' |
| 51 | +DIALOG = '[data-testid="stDialog"]' |
| 52 | + |
| 53 | +# "reset to 0" vs "preserved" is a wide gap; allow small layout jitter. |
| 54 | +TOLERANCE_PX = 50 |
| 55 | + |
| 56 | + |
| 57 | +@pytest.fixture(scope="session") |
| 58 | +def browser_type_launch_args(browser_type_launch_args): |
| 59 | + return { |
| 60 | + **browser_type_launch_args, |
| 61 | + "firefox_user_prefs": {"pdfjs.disabled": False}, |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | +@pytest.fixture(autouse=True, scope="module") |
| 66 | +def streamlit_app(): |
| 67 | + with StreamlitRunner(Path(APP_FILE)) as runner: |
| 68 | + yield runner |
| 69 | + |
| 70 | + |
| 71 | +@pytest.fixture(autouse=True, scope="function") |
| 72 | +def go_to_app(page: Page, streamlit_app: StreamlitRunner): |
| 73 | + page.goto(streamlit_app.server_url) |
| 74 | + expect(page.get_by_role("img", name="Running...")).not_to_be_visible() |
| 75 | + |
| 76 | + |
| 77 | +def _scroll_top(page: Page) -> float: |
| 78 | + return page.evaluate(f"document.querySelector('{STMAIN}').scrollTop") |
| 79 | + |
| 80 | + |
| 81 | +def test_parent_scroll_preserved_after_dialog_close(page: Page): |
| 82 | + # Wait for the component to render. The component iframe starts hidden until |
| 83 | + # ready, so wait for it to be attached and for the PDF canvas + a page-2 |
| 84 | + # annotation to render rather than asserting outer-iframe visibility. |
| 85 | + page.locator(IFRAME).nth(0).wait_for(state="attached", timeout=30000) |
| 86 | + frame = page.frame_locator(IFRAME).nth(0) |
| 87 | + expect(frame.locator('div[id="pdfViewer"] canvas').nth(0)).to_be_visible(timeout=30000) |
| 88 | + # data-index "2" is the first page-2 annotation; clicking it scrolls the page |
| 89 | + # down (giving a non-zero reference) and opens the dialog. |
| 90 | + annotation = frame.locator('div[data-index="2"]') |
| 91 | + expect(annotation).to_be_visible(timeout=15000) |
| 92 | + |
| 93 | + annotation.click() |
| 94 | + dialog = page.locator(DIALOG) |
| 95 | + expect(dialog).to_be_visible(timeout=15000) |
| 96 | + page.wait_for_timeout(400) |
| 97 | + |
| 98 | + # Scroll position at the moment the dialog is open is the reference the page |
| 99 | + # must return to after the dialog closes. |
| 100 | + scroll_when_open = _scroll_top(page) |
| 101 | + assert scroll_when_open > TOLERANCE_PX, ( |
| 102 | + f"Precondition failed: page did not scroll on annotation click " |
| 103 | + f"({scroll_when_open}px); cannot detect a reset." |
| 104 | + ) |
| 105 | + |
| 106 | + # Close the dialog -> triggers the rerun that resets the scroll on affected builds. |
| 107 | + page.keyboard.press("Escape") |
| 108 | + expect(dialog).not_to_be_visible(timeout=15000) |
| 109 | + page.wait_for_timeout(700) |
| 110 | + |
| 111 | + after = _scroll_top(page) |
| 112 | + delta = abs(after - scroll_when_open) |
| 113 | + assert delta <= TOLERANCE_PX, ( |
| 114 | + f"Parent scroll not preserved after dialog close (#14917): " |
| 115 | + f"open={scroll_when_open}px, after={after}px, delta={delta}px" |
| 116 | + ) |
0 commit comments