streamlit
76 строк · 2.9 Кб
1# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2024)
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import re16
17from playwright.sync_api import Page, expect18
19from e2e_playwright.conftest import ImageCompareFunction20
21
22def test_components_iframe_rendering(23themed_app: Page, assert_snapshot: ImageCompareFunction24):25"""Test that our components v1 API correctly renders elements via screenshot matching."""26
27elements = themed_app.locator("iframe")28expect(elements).to_have_count(3)29
30# Only doing a snapshot of the html component, since the iframe one31# does not use a valid URL.32assert_snapshot(elements.nth(0), name="st_components-html")33
34
35def test_html_correctly_sets_attr(app: Page):36"""Test that html correctly sets attributes."""37
38html_component = app.locator("iframe").nth(0)39
40expect(html_component).to_have_attribute("srcDoc", "<h1>Hello, Streamlit!</h1>")41expect(html_component).to_have_attribute("width", "200")42expect(html_component).to_have_attribute("height", "500")43expect(html_component).to_have_attribute("scrolling", "no")44
45
46def test_iframe_correctly_sets_attr(app: Page):47"""Test that iframe correctly sets attributes."""48
49iframe_component = app.locator("iframe").nth(1)50
51expect(iframe_component).to_have_attribute("src", "http://not.a.real.url")52expect(iframe_component).to_have_attribute("width", "200")53expect(iframe_component).to_have_attribute("height", "500")54expect(iframe_component).to_have_attribute("scrolling", "auto")55
56
57def test_declare_component_correctly_sets_attr(app: Page):58"""Test that components.declare_component correctly sets attributes."""59
60declare_component = app.locator("iframe").nth(2)61
62expect(declare_component).to_have_attribute(63"title", "st_components_v1.test_component"64)65expect(declare_component).to_have_attribute(66"src",67re.compile(68r"http://not.a.real.url\?streamlitUrl=http%3A%2F%2Flocalhost%3A\d*%2F$"69),70)71
72
73# TODO (willhuang1997): Add tests for handling bytes, JSON, DFs, theme
74# TODO (willhuang1997):add tests to ensure the messages actually go to the iframe
75# Relevant code is here from the past: https://github.com/streamlit/streamlit/blob/3d0b0603627037255790fe55a483f55fce5eff67/frontend/lib/src/components/widgets/CustomComponent/ComponentInstance.test.tsx#L257
76# Relevant PR is here: https://github.com/streamlit/streamlit/pull/7971
77