streamlit
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
15from playwright.sync_api import Page, expect
16
17from e2e_playwright.conftest import ImageCompareFunction
18
19
20def test_selectbox_widget_rendering(
21themed_app: Page, assert_snapshot: ImageCompareFunction
22):
23"""Test that the selectbox widgets are correctly rendered via screenshot matching."""
24selectbox_widgets = themed_app.get_by_test_id("stSelectbox")
25expect(selectbox_widgets).to_have_count(12)
26
27assert_snapshot(selectbox_widgets.nth(0), name="st_selectbox-default")
28assert_snapshot(selectbox_widgets.nth(1), name="st_selectbox-formatted_options")
29assert_snapshot(selectbox_widgets.nth(2), name="st_selectbox-no_options")
30assert_snapshot(selectbox_widgets.nth(3), name="st_selectbox-more_options")
31assert_snapshot(selectbox_widgets.nth(4), name="st_selectbox-disabled")
32assert_snapshot(selectbox_widgets.nth(5), name="st_selectbox-hidden_label")
33assert_snapshot(selectbox_widgets.nth(6), name="st_selectbox-collapsed_label")
34assert_snapshot(selectbox_widgets.nth(7), name="st_selectbox-callback_help")
35assert_snapshot(selectbox_widgets.nth(8), name="st_selectbox-empty_selection")
36assert_snapshot(
37selectbox_widgets.nth(9), name="st_selectbox-empty_selection_placeholder"
38)
39assert_snapshot(selectbox_widgets.nth(10), name="st_selectbox-dataframe_options")
40assert_snapshot(selectbox_widgets.nth(11), name="st_selectbox-value_from_state")
41
42
43def test_selectbox_has_correct_initial_values(app: Page):
44"""Test that st.selectbox returns the correct initial values."""
45markdown_elements = app.get_by_test_id("stMarkdown")
46expect(markdown_elements).to_have_count(13)
47
48expected = [
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
64for markdown_element, expected_text in zip(markdown_elements.all(), expected):
65expect(markdown_element).to_have_text(expected_text, use_inner_text=True)
66
67
68def test_handles_option_selection(app: Page, assert_snapshot: ImageCompareFunction):
69"""Test that selection of an option via the dropdown works correctly."""
70app.get_by_test_id("stSelectbox").nth(3).locator("input").click()
71
72# Take a snapshot of the selection dropdown:
73selection_dropdown = app.locator('[data-baseweb="popover"]').first
74assert_snapshot(selection_dropdown, name="st_selectbox-selection_dropdown")
75# Select last option:
76selection_dropdown.locator("li").nth(1).click()
77# Check that selection worked:
78expect(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
83def test_handles_option_selection_via_typing(app: Page):
84"""Test that selection of an option via typing works correctly."""
85selectbox_input = app.get_by_test_id("stSelectbox").nth(3).locator("input")
86
87# Type an option:
88selectbox_input.type("e2e/scripts/st_warning.py")
89selectbox_input.press("Enter")
90
91# Check that selection worked:
92expect(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
97def test_shows_correct_options_via_fuzzy_search(
98app: Page, assert_snapshot: ImageCompareFunction
99):
100"""Test that the fuzzy matching of options via typing works correctly."""
101selectbox_input = app.get_by_test_id("stSelectbox").nth(3).locator("input")
102
103# Start typing:
104selectbox_input.type("exp")
105
106# Check filtered options
107selection_dropdown = app.locator('[data-baseweb="popover"]').first
108assert_snapshot(selection_dropdown, name="st_selectbox-fuzzy_matching")
109
110
111def test_empty_selectbox_behaves_correctly(
112app: Page, assert_snapshot: ImageCompareFunction
113):
114"""Test that st.selectbox behaves correctly when empty (no initial selection)."""
115empty_selectbox_input = app.get_by_test_id("stSelectbox").locator("input").nth(8)
116
117# Type an option:
118empty_selectbox_input.type("male")
119empty_selectbox_input.press("Enter")
120
121expect(app.get_by_test_id("stMarkdown").nth(9)).to_have_text(
122"value 9: male", use_inner_text=True
123)
124
125assert_snapshot(
126app.get_by_test_id("stSelectbox").nth(8), name="st_selectbox-clearable_input"
127)
128
129empty_selectbox_input.focus()
130empty_selectbox_input.press("Escape")
131
132# Should be empty again:
133expect(app.get_by_test_id("stMarkdown").nth(9)).to_have_text(
134"value 9: None", use_inner_text=True
135)
136
137
138def test_keeps_value_on_selection_close(app: Page):
139"""Test that the selection is kept when the dropdown is closed."""
140app.get_by_test_id("stSelectbox").nth(3).locator("input").click()
141
142# Take a snapshot of the selection dropdown:
143expect(app.locator('[data-baseweb="popover"]').first).to_be_visible()
144
145# Click outside to close the dropdown:
146app.get_by_test_id("stMarkdown").first.click()
147
148# Check if value is still initial value:
149expect(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
154def test_handles_callback_on_change_correctly(app: Page):
155"""Test that it correctly calls the callback on change."""
156# Check initial state:
157expect(app.get_by_test_id("stMarkdown").nth(7)).to_have_text(
158"value 8: female", use_inner_text=True
159)
160expect(app.get_by_test_id("stMarkdown").nth(8)).to_have_text(
161"selectbox changed: False", use_inner_text=True
162)
163
164app.get_by_test_id("stSelectbox").nth(7).locator("input").click()
165
166# Select last option:
167selection_dropdown = app.locator('[data-baseweb="popover"]').first
168selection_dropdown.locator("li").first.click()
169
170# Check that selection worked:
171expect(app.get_by_test_id("stMarkdown").nth(7)).to_have_text(
172"value 8: male", use_inner_text=True
173)
174expect(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
179empty_selectbox_input = app.get_by_test_id("stSelectbox").locator("input").first
180
181# Type an option:
182empty_selectbox_input.type("female")
183empty_selectbox_input.press("Enter")
184
185expect(app.get_by_test_id("stMarkdown").first).to_have_text(
186"value 1: female", use_inner_text=True
187)
188expect(app.get_by_test_id("stMarkdown").nth(7)).to_have_text(
189"value 8: male", use_inner_text=True
190)
191expect(app.get_by_test_id("stMarkdown").nth(8)).to_have_text(
192"selectbox changed: False", use_inner_text=True
193)
194