streamlit

Форк
0
/
st_selectbox_test.py 
193 строки · 7.3 Кб
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_selectbox_widget_rendering(
21
    themed_app: Page, assert_snapshot: ImageCompareFunction
22
):
23
    """Test that the selectbox widgets are correctly rendered via screenshot matching."""
24
    selectbox_widgets = themed_app.get_by_test_id("stSelectbox")
25
    expect(selectbox_widgets).to_have_count(12)
26

27
    assert_snapshot(selectbox_widgets.nth(0), name="st_selectbox-default")
28
    assert_snapshot(selectbox_widgets.nth(1), name="st_selectbox-formatted_options")
29
    assert_snapshot(selectbox_widgets.nth(2), name="st_selectbox-no_options")
30
    assert_snapshot(selectbox_widgets.nth(3), name="st_selectbox-more_options")
31
    assert_snapshot(selectbox_widgets.nth(4), name="st_selectbox-disabled")
32
    assert_snapshot(selectbox_widgets.nth(5), name="st_selectbox-hidden_label")
33
    assert_snapshot(selectbox_widgets.nth(6), name="st_selectbox-collapsed_label")
34
    assert_snapshot(selectbox_widgets.nth(7), name="st_selectbox-callback_help")
35
    assert_snapshot(selectbox_widgets.nth(8), name="st_selectbox-empty_selection")
36
    assert_snapshot(
37
        selectbox_widgets.nth(9), name="st_selectbox-empty_selection_placeholder"
38
    )
39
    assert_snapshot(selectbox_widgets.nth(10), name="st_selectbox-dataframe_options")
40
    assert_snapshot(selectbox_widgets.nth(11), name="st_selectbox-value_from_state")
41

42

43
def test_selectbox_has_correct_initial_values(app: Page):
44
    """Test that st.selectbox returns the correct initial values."""
45
    markdown_elements = app.get_by_test_id("stMarkdown")
46
    expect(markdown_elements).to_have_count(13)
47

48
    expected = [
49
        "value 1: male",
50
        "value 2: female",
51
        "value 3: None",
52
        "value 4: e2e/scripts/components_iframe.py",
53
        "value 5: male",
54
        "value 6: male",
55
        "value 7: male",
56
        "value 8: female",
57
        "selectbox changed: False",
58
        "value 9: None",
59
        "value 10: None",
60
        "value 11: male",
61
        "value 12: female",
62
    ]
63

64
    for markdown_element, expected_text in zip(markdown_elements.all(), expected):
65
        expect(markdown_element).to_have_text(expected_text, use_inner_text=True)
66

67

68
def test_handles_option_selection(app: Page, assert_snapshot: ImageCompareFunction):
69
    """Test that selection of an option via the dropdown works correctly."""
70
    app.get_by_test_id("stSelectbox").nth(3).locator("input").click()
71

72
    # Take a snapshot of the selection dropdown:
73
    selection_dropdown = app.locator('[data-baseweb="popover"]').first
74
    assert_snapshot(selection_dropdown, name="st_selectbox-selection_dropdown")
75
    # Select last option:
76
    selection_dropdown.locator("li").nth(1).click()
77
    # Check that selection worked:
78
    expect(app.get_by_test_id("stMarkdown").nth(3)).to_have_text(
79
        "value 4: e2e/scripts/st_warning.py", use_inner_text=True
80
    )
81

82

83
def test_handles_option_selection_via_typing(app: Page):
84
    """Test that selection of an option via typing works correctly."""
85
    selectbox_input = app.get_by_test_id("stSelectbox").nth(3).locator("input")
86

87
    # Type an option:
88
    selectbox_input.type("e2e/scripts/st_warning.py")
89
    selectbox_input.press("Enter")
90

91
    # Check that selection worked:
92
    expect(app.get_by_test_id("stMarkdown").nth(3)).to_have_text(
93
        "value 4: e2e/scripts/st_warning.py", use_inner_text=True
94
    )
95

96

97
def test_shows_correct_options_via_fuzzy_search(
98
    app: Page, assert_snapshot: ImageCompareFunction
99
):
100
    """Test that the fuzzy matching of options via typing works correctly."""
101
    selectbox_input = app.get_by_test_id("stSelectbox").nth(3).locator("input")
102

103
    # Start typing:
104
    selectbox_input.type("exp")
105

106
    # Check filtered options
107
    selection_dropdown = app.locator('[data-baseweb="popover"]').first
108
    assert_snapshot(selection_dropdown, name="st_selectbox-fuzzy_matching")
109

110

111
def test_empty_selectbox_behaves_correctly(
112
    app: Page, assert_snapshot: ImageCompareFunction
113
):
114
    """Test that st.selectbox behaves correctly when empty (no initial selection)."""
115
    empty_selectbox_input = app.get_by_test_id("stSelectbox").locator("input").nth(8)
116

117
    # Type an option:
118
    empty_selectbox_input.type("male")
119
    empty_selectbox_input.press("Enter")
120

121
    expect(app.get_by_test_id("stMarkdown").nth(9)).to_have_text(
122
        "value 9: male", use_inner_text=True
123
    )
124

125
    assert_snapshot(
126
        app.get_by_test_id("stSelectbox").nth(8), name="st_selectbox-clearable_input"
127
    )
128

129
    empty_selectbox_input.focus()
130
    empty_selectbox_input.press("Escape")
131

132
    # Should be empty again:
133
    expect(app.get_by_test_id("stMarkdown").nth(9)).to_have_text(
134
        "value 9: None", use_inner_text=True
135
    )
136

137

138
def test_keeps_value_on_selection_close(app: Page):
139
    """Test that the selection is kept when the dropdown is closed."""
140
    app.get_by_test_id("stSelectbox").nth(3).locator("input").click()
141

142
    # Take a snapshot of the selection dropdown:
143
    expect(app.locator('[data-baseweb="popover"]').first).to_be_visible()
144

145
    # Click outside to close the dropdown:
146
    app.get_by_test_id("stMarkdown").first.click()
147

148
    # Check if value is still initial value:
149
    expect(app.get_by_test_id("stMarkdown").nth(3)).to_have_text(
150
        "value 4: e2e/scripts/components_iframe.py", use_inner_text=True
151
    )
152

153

154
def test_handles_callback_on_change_correctly(app: Page):
155
    """Test that it correctly calls the callback on change."""
156
    # Check initial state:
157
    expect(app.get_by_test_id("stMarkdown").nth(7)).to_have_text(
158
        "value 8: female", use_inner_text=True
159
    )
160
    expect(app.get_by_test_id("stMarkdown").nth(8)).to_have_text(
161
        "selectbox changed: False", use_inner_text=True
162
    )
163

164
    app.get_by_test_id("stSelectbox").nth(7).locator("input").click()
165

166
    # Select last option:
167
    selection_dropdown = app.locator('[data-baseweb="popover"]').first
168
    selection_dropdown.locator("li").first.click()
169

170
    # Check that selection worked:
171
    expect(app.get_by_test_id("stMarkdown").nth(7)).to_have_text(
172
        "value 8: male", use_inner_text=True
173
    )
174
    expect(app.get_by_test_id("stMarkdown").nth(8)).to_have_text(
175
        "selectbox changed: True", use_inner_text=True
176
    )
177

178
    # Change different input to trigger delta path change
179
    empty_selectbox_input = app.get_by_test_id("stSelectbox").locator("input").first
180

181
    # Type an option:
182
    empty_selectbox_input.type("female")
183
    empty_selectbox_input.press("Enter")
184

185
    expect(app.get_by_test_id("stMarkdown").first).to_have_text(
186
        "value 1: female", use_inner_text=True
187
    )
188
    expect(app.get_by_test_id("stMarkdown").nth(7)).to_have_text(
189
        "value 8: male", use_inner_text=True
190
    )
191
    expect(app.get_by_test_id("stMarkdown").nth(8)).to_have_text(
192
        "selectbox changed: False", use_inner_text=True
193
    )
194

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

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

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

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