streamlit

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

16
from playwright.sync_api import Page, expect
17

18
from e2e_playwright.conftest import ImageCompareFunction, wait_for_app_run
19

20

21
def test_number_input_widget_display(
22
    themed_app: Page, assert_snapshot: ImageCompareFunction
23
):
24
    """Test that st.number_input renders correctly."""
25
    number_input_elements = themed_app.get_by_test_id("stNumberInput")
26
    expect(number_input_elements).to_have_count(12)
27

28
    assert_snapshot(number_input_elements.nth(0), name="st_number_input-default")
29
    assert_snapshot(number_input_elements.nth(1), name="st_number_input-value_1")
30
    assert_snapshot(number_input_elements.nth(2), name="st_number_input-min_max")
31
    assert_snapshot(number_input_elements.nth(3), name="st_number_input-step_2")
32
    assert_snapshot(number_input_elements.nth(4), name="st_number_input-max_10")
33
    assert_snapshot(number_input_elements.nth(5), name="st_number_input-disabled_true")
34
    assert_snapshot(number_input_elements.nth(6), name="st_number_input-label_hidden")
35
    assert_snapshot(
36
        number_input_elements.nth(7), name="st_number_input-label_collapsed"
37
    )
38
    assert_snapshot(number_input_elements.nth(8), name="st_number_input-on_change")
39
    assert_snapshot(number_input_elements.nth(9), name="st_number_input-small_width")
40
    assert_snapshot(number_input_elements.nth(10), name="st_number_input-value_none")
41
    assert_snapshot(
42
        number_input_elements.nth(11), name="st_number_input-value_none_min_1"
43
    )
44

45

46
def test_number_input_has_correct_default_values(app: Page):
47
    """Test that st.number_input has the correct initial values."""
48
    markdown_elements = app.get_by_test_id("stMarkdown")
49
    expect(markdown_elements).to_have_count(13)
50

51
    expected = [
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

67
    for markdown_element, expected_text in zip(markdown_elements.all(), expected):
68
        expect(markdown_element).to_have_text(expected_text, use_inner_text=True)
69

70

71
def test_number_input_shows_instructions_when_dirty(
72
    app: Page, assert_snapshot: ImageCompareFunction
73
):
74
    """Test that st.number_input shows the instructions correctly when dirty."""
75
    first_number_input = app.get_by_test_id("stNumberInput").first
76
    first_number_input.locator("input").fill("10")
77

78
    assert_snapshot(first_number_input, name="st_number_input-input_instructions")
79

80

81
def test_number_input_updates_value_correctly_on_enter(app: Page):
82
    """Test that st.number_input updates the value correctly on enter."""
83
    first_number_input_field = (
84
        app.get_by_test_id("stNumberInput").nth(0).locator("input")
85
    )
86
    first_number_input_field.fill("10")
87
    first_number_input_field.press("Enter")
88

89
    expect(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

94
def test_number_input_has_correct_value_on_increment_click(app: Page):
95
    """Test that st.number_input has the correct value on increment click."""
96
    number_input_up_buttons = app.get_by_test_id("stNumberInput").locator(
97
        "button.step-up"
98
    )
99
    expect(number_input_up_buttons).to_have_count(11)
100
    for i, button in enumerate(number_input_up_buttons.all()):
101
        if i not in [5, 9]:
102
            button.click()
103
            wait_for_app_run(app)
104

105
    markdown_elements = app.get_by_test_id("stMarkdown")
106

107
    expected = [
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

123
    for markdown_element, expected_text in zip(markdown_elements.all(), expected):
124
        expect(markdown_element).to_have_text(expected_text, use_inner_text=True)
125

126

127
def test_number_input_has_correct_value_on_arrow_up(app: Page):
128
    """Test that st.number_input has the correct value on arrow up."""
129
    first_number_input_field = (
130
        app.get_by_test_id("stNumberInput").nth(0).locator("input")
131
    )
132
    first_number_input_field.press("ArrowUp")
133

134
    expect(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

139
def test_number_input_has_correct_value_on_blur(app: Page):
140
    """Test that st.number_input has the correct value on blur."""
141

142
    first_number_input_field = (
143
        app.get_by_test_id("stNumberInput").nth(0).locator("input")
144
    )
145
    first_number_input_field.focus()
146
    first_number_input_field.fill("10")
147
    first_number_input_field.blur()
148

149
    expect(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

154
def test_empty_number_input_behaves_correctly(
155
    app: Page, assert_snapshot: ImageCompareFunction
156
):
157
    """Test that st.number_input behaves correctly when empty."""
158
    # Enter 10 in the first empty input:
159
    empty_number_input = app.get_by_test_id("stNumberInput").nth(10)
160
    empty_number_input_field = empty_number_input.locator("input").first
161
    empty_number_input_field.fill("10")
162
    empty_number_input_field.press("Enter")
163

164
    expect(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

168
    assert_snapshot(empty_number_input, name="st_number_input-clearable_input")
169

170
    # Press escape to clear value:
171
    empty_number_input.focus()
172
    empty_number_input.press("Escape")
173
    empty_number_input.press("Enter")
174

175
    # Should be empty again:
176
    expect(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:
182
    empty_number_input_with_min = (
183
        app.get_by_test_id("stNumberInput").nth(11).locator("input").first
184
    )
185
    empty_number_input_with_min.fill("15")
186
    empty_number_input_with_min.press("Enter")
187

188
    expect(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

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

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

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

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