streamlit

Форк
0
/
st_button_test.py 
105 строк · 4.5 Кб
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
from playwright.sync_api import Page, expect
16

17
from e2e_playwright.conftest import ImageCompareFunction
18

19

20
def test_button_widget_rendering(
21
    themed_app: Page, assert_snapshot: ImageCompareFunction
22
):
23
    """Test that the button widgets are correctly rendered via screenshot matching."""
24
    button_elements = themed_app.get_by_test_id("stButton")
25
    expect(button_elements).to_have_count(15)
26

27
    assert_snapshot(button_elements.nth(0), name="st_button-default")
28
    assert_snapshot(button_elements.nth(1), name="st_button-disabled")
29
    assert_snapshot(button_elements.nth(2), name="st_button-primary")
30
    assert_snapshot(button_elements.nth(3), name="st_button-disabled_primary")
31
    assert_snapshot(button_elements.nth(4), name="st_button-use_container_width")
32
    assert_snapshot(button_elements.nth(5), name="st_button-use_container_width_help")
33
    assert_snapshot(button_elements.nth(6), name="st_button-styled_label")
34

35
    # The rest is tested in one screenshot in the following test
36

37

38
def test_buttons_in_columns(themed_app: Page, assert_snapshot: ImageCompareFunction):
39
    """Test that the button widgets are correctly rendered in columns via screenshot matching."""
40
    columns_container = themed_app.get_by_test_id("stHorizontalBlock")
41
    expect(columns_container).to_have_count(1)
42

43
    assert_snapshot(columns_container, name="st_button-in_columns")
44

45

46
def test_value_correct_on_click(app: Page):
47
    button_element = app.get_by_test_id("stButton").locator("button").first
48
    button_element.click()
49
    expect(app.get_by_test_id("stMarkdown").nth(0)).to_have_text("value: True")
50
    expect(app.get_by_test_id("stMarkdown").nth(1)).to_have_text(
51
        "value from state: True"
52
    )
53

54

55
def test_value_not_reset_on_reclick(app: Page):
56
    button_element = app.get_by_test_id("stButton").locator("button").first
57
    button_element.click()
58
    button_element.click()
59
    expect(app.get_by_test_id("stMarkdown").first).to_have_text("value: True")
60

61

62
def test_click_calls_callback(app: Page):
63
    button_element = app.get_by_test_id("stButton").locator("button").first
64
    expect(app.get_by_test_id("stMarkdown").nth(2)).to_contain_text(
65
        "Button was clicked: False"
66
    )
67
    button_element.click()
68
    expect(app.get_by_test_id("stMarkdown").nth(2)).to_have_text(
69
        "Button was clicked: True"
70
    )
71
    expect(app.get_by_test_id("stMarkdown").nth(3)).to_have_text("times clicked: 1")
72
    expect(app.get_by_test_id("stMarkdown").nth(4)).to_have_text("arg value: 1")
73
    expect(app.get_by_test_id("stMarkdown").nth(5)).to_have_text("kwarg value: 2")
74

75

76
def test_click_increment_count(app: Page):
77
    button_element = app.get_by_test_id("stButton").locator("button").first
78
    button_element.click()
79
    expect(app.get_by_test_id("stMarkdown").nth(3)).to_have_text("times clicked: 1")
80
    button_element.click()
81
    expect(app.get_by_test_id("stMarkdown").nth(3)).to_have_text("times clicked: 2")
82
    button_element.click()
83
    expect(app.get_by_test_id("stMarkdown").nth(3)).to_have_text("times clicked: 3")
84

85

86
def test_reset_on_other_widget_change(app: Page):
87
    button_element = app.get_by_test_id("stButton").locator("button").first
88
    checkbox_element = app.get_by_test_id("stCheckbox")
89
    button_element.click()
90
    expect(app.get_by_test_id("stMarkdown").nth(0)).to_have_text("value: True")
91
    expect(app.get_by_test_id("stMarkdown").nth(1)).to_have_text(
92
        "value from state: True"
93
    )
94
    checkbox_element.click()
95
    expect(app.get_by_test_id("stMarkdown").nth(0)).to_have_text("value: False")
96
    expect(app.get_by_test_id("stMarkdown").nth(1)).to_have_text(
97
        "value from state: False"
98
    )
99

100

101
def test_show_tooltip_on_hover(app: Page, assert_snapshot: ImageCompareFunction):
102
    button_element = app.get_by_test_id("stButton").nth(5)
103
    button_element.hover()
104
    assert_snapshot(button_element, name="st_button-on_hover")
105
    expect(app.get_by_test_id("stTooltipContent")).to_have_text("help text")
106

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

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

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

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