streamlit
190 строк · 7.7 Кб
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
16from playwright.sync_api import Page, expect
17
18from e2e_playwright.conftest import ImageCompareFunction, wait_for_app_run
19
20
21def test_number_input_widget_display(
22themed_app: Page, assert_snapshot: ImageCompareFunction
23):
24"""Test that st.number_input renders correctly."""
25number_input_elements = themed_app.get_by_test_id("stNumberInput")
26expect(number_input_elements).to_have_count(12)
27
28assert_snapshot(number_input_elements.nth(0), name="st_number_input-default")
29assert_snapshot(number_input_elements.nth(1), name="st_number_input-value_1")
30assert_snapshot(number_input_elements.nth(2), name="st_number_input-min_max")
31assert_snapshot(number_input_elements.nth(3), name="st_number_input-step_2")
32assert_snapshot(number_input_elements.nth(4), name="st_number_input-max_10")
33assert_snapshot(number_input_elements.nth(5), name="st_number_input-disabled_true")
34assert_snapshot(number_input_elements.nth(6), name="st_number_input-label_hidden")
35assert_snapshot(
36number_input_elements.nth(7), name="st_number_input-label_collapsed"
37)
38assert_snapshot(number_input_elements.nth(8), name="st_number_input-on_change")
39assert_snapshot(number_input_elements.nth(9), name="st_number_input-small_width")
40assert_snapshot(number_input_elements.nth(10), name="st_number_input-value_none")
41assert_snapshot(
42number_input_elements.nth(11), name="st_number_input-value_none_min_1"
43)
44
45
46def test_number_input_has_correct_default_values(app: Page):
47"""Test that st.number_input has the correct initial values."""
48markdown_elements = app.get_by_test_id("stMarkdown")
49expect(markdown_elements).to_have_count(13)
50
51expected = [
52"number input 1 (default) - value: 0.0",
53"number input 2 (value=1) - value: 1",
54"number input 3 (min & max) - value: 1",
55"number input 4 (step=2) - value: 0",
56"number input 5 (max=10) - value: 0",
57"number input 6 (disabled=True) - value: 0.0",
58"number input 7 (label=hidden) - value: 0.0",
59"number input 8 (label=collapsed) - value: 0.0",
60"number input 9 (on_change) - value: 0.0",
61"number input 9 (on_change) - changed: False",
62"number input 10 (small width) - value: 0",
63"number input 11 (value=None) - value: None",
64"number input 12 (value from state & min=1) - value: 10",
65]
66
67for markdown_element, expected_text in zip(markdown_elements.all(), expected):
68expect(markdown_element).to_have_text(expected_text, use_inner_text=True)
69
70
71def test_number_input_shows_instructions_when_dirty(
72app: Page, assert_snapshot: ImageCompareFunction
73):
74"""Test that st.number_input shows the instructions correctly when dirty."""
75first_number_input = app.get_by_test_id("stNumberInput").first
76first_number_input.locator("input").fill("10")
77
78assert_snapshot(first_number_input, name="st_number_input-input_instructions")
79
80
81def test_number_input_updates_value_correctly_on_enter(app: Page):
82"""Test that st.number_input updates the value correctly on enter."""
83first_number_input_field = (
84app.get_by_test_id("stNumberInput").nth(0).locator("input")
85)
86first_number_input_field.fill("10")
87first_number_input_field.press("Enter")
88
89expect(app.get_by_test_id("stMarkdown").nth(0)).to_have_text(
90"number input 1 (default) - value: 10.0", use_inner_text=True
91)
92
93
94def test_number_input_has_correct_value_on_increment_click(app: Page):
95"""Test that st.number_input has the correct value on increment click."""
96number_input_up_buttons = app.get_by_test_id("stNumberInput").locator(
97"button.step-up"
98)
99expect(number_input_up_buttons).to_have_count(11)
100for i, button in enumerate(number_input_up_buttons.all()):
101if i not in [5, 9]:
102button.click()
103wait_for_app_run(app)
104
105markdown_elements = app.get_by_test_id("stMarkdown")
106
107expected = [
108"number input 1 (default) - value: 0.01",
109"number input 2 (value=1) - value: 2",
110"number input 3 (min & max) - value: 2",
111"number input 4 (step=2) - value: 2",
112"number input 5 (max=10) - value: 1",
113"number input 6 (disabled=True) - value: 0.0",
114"number input 7 (label=hidden) - value: 0.01",
115"number input 8 (label=collapsed) - value: 0.01",
116"number input 9 (on_change) - value: 0.01",
117"number input 9 (on_change) - changed: True",
118"number input 10 (small width) - value: 0",
119"number input 11 (value=None) - value: None",
120"number input 12 (value from state & min=1) - value: 11",
121]
122
123for markdown_element, expected_text in zip(markdown_elements.all(), expected):
124expect(markdown_element).to_have_text(expected_text, use_inner_text=True)
125
126
127def test_number_input_has_correct_value_on_arrow_up(app: Page):
128"""Test that st.number_input has the correct value on arrow up."""
129first_number_input_field = (
130app.get_by_test_id("stNumberInput").nth(0).locator("input")
131)
132first_number_input_field.press("ArrowUp")
133
134expect(app.get_by_test_id("stMarkdown").nth(0)).to_have_text(
135"number input 1 (default) - value: 0.01", use_inner_text=True
136)
137
138
139def test_number_input_has_correct_value_on_blur(app: Page):
140"""Test that st.number_input has the correct value on blur."""
141
142first_number_input_field = (
143app.get_by_test_id("stNumberInput").nth(0).locator("input")
144)
145first_number_input_field.focus()
146first_number_input_field.fill("10")
147first_number_input_field.blur()
148
149expect(app.get_by_test_id("stMarkdown").nth(0)).to_have_text(
150"number input 1 (default) - value: 10.0", use_inner_text=True
151)
152
153
154def test_empty_number_input_behaves_correctly(
155app: Page, assert_snapshot: ImageCompareFunction
156):
157"""Test that st.number_input behaves correctly when empty."""
158# Enter 10 in the first empty input:
159empty_number_input = app.get_by_test_id("stNumberInput").nth(10)
160empty_number_input_field = empty_number_input.locator("input").first
161empty_number_input_field.fill("10")
162empty_number_input_field.press("Enter")
163
164expect(app.get_by_test_id("stMarkdown").nth(11)).to_have_text(
165"number input 11 (value=None) - value: 10.0", use_inner_text=True
166)
167
168assert_snapshot(empty_number_input, name="st_number_input-clearable_input")
169
170# Press escape to clear value:
171empty_number_input.focus()
172empty_number_input.press("Escape")
173empty_number_input.press("Enter")
174
175# Should be empty again:
176expect(app.get_by_test_id("stMarkdown").nth(11)).to_have_text(
177"number input 11 (value=None) - value: None", use_inner_text=True
178)
179
180# Check with second empty input, this one should be integer since the min_value was
181# set to an integer:
182empty_number_input_with_min = (
183app.get_by_test_id("stNumberInput").nth(11).locator("input").first
184)
185empty_number_input_with_min.fill("15")
186empty_number_input_with_min.press("Enter")
187
188expect(app.get_by_test_id("stMarkdown").nth(12)).to_have_text(
189"number input 12 (value from state & min=1) - value: 15", use_inner_text=True
190)
191