streamlit

Форк
0
/
st_components_v1_test.py 
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

15
import re
16

17
from playwright.sync_api import Page, expect
18

19
from e2e_playwright.conftest import ImageCompareFunction
20

21

22
def test_components_iframe_rendering(
23
    themed_app: Page, assert_snapshot: ImageCompareFunction
24
):
25
    """Test that our components v1 API correctly renders elements via screenshot matching."""
26

27
    elements = themed_app.locator("iframe")
28
    expect(elements).to_have_count(3)
29

30
    # Only doing a snapshot of the html component, since the iframe one
31
    # does not use a valid URL.
32
    assert_snapshot(elements.nth(0), name="st_components-html")
33

34

35
def test_html_correctly_sets_attr(app: Page):
36
    """Test that html correctly sets attributes."""
37

38
    html_component = app.locator("iframe").nth(0)
39

40
    expect(html_component).to_have_attribute("srcDoc", "<h1>Hello, Streamlit!</h1>")
41
    expect(html_component).to_have_attribute("width", "200")
42
    expect(html_component).to_have_attribute("height", "500")
43
    expect(html_component).to_have_attribute("scrolling", "no")
44

45

46
def test_iframe_correctly_sets_attr(app: Page):
47
    """Test that iframe correctly sets attributes."""
48

49
    iframe_component = app.locator("iframe").nth(1)
50

51
    expect(iframe_component).to_have_attribute("src", "http://not.a.real.url")
52
    expect(iframe_component).to_have_attribute("width", "200")
53
    expect(iframe_component).to_have_attribute("height", "500")
54
    expect(iframe_component).to_have_attribute("scrolling", "auto")
55

56

57
def test_declare_component_correctly_sets_attr(app: Page):
58
    """Test that components.declare_component correctly sets attributes."""
59

60
    declare_component = app.locator("iframe").nth(2)
61

62
    expect(declare_component).to_have_attribute(
63
        "title", "st_components_v1.test_component"
64
    )
65
    expect(declare_component).to_have_attribute(
66
        "src",
67
        re.compile(
68
            r"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

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.