streamlit

Форк
0
/
st_text_area_test.py 
220 строк · 7.8 Кб
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
import pytest
16
from playwright.sync_api import Page, expect
17

18
from e2e_playwright.conftest import ImageCompareFunction
19

20

21
def test_text_area_widget_rendering(
22
    themed_app: Page, assert_snapshot: ImageCompareFunction
23
):
24
    """Test that the st.text_area widgets are correctly rendered via screenshot matching."""
25
    text_area_widgets = themed_app.get_by_test_id("stTextArea")
26
    expect(text_area_widgets).to_have_count(11)
27

28
    assert_snapshot(text_area_widgets.nth(0), name="st_text_area-default")
29
    assert_snapshot(text_area_widgets.nth(1), name="st_text_area-value_some_text")
30
    assert_snapshot(text_area_widgets.nth(2), name="st_text_area-value_1234")
31
    assert_snapshot(text_area_widgets.nth(3), name="st_text_area-value_None")
32
    assert_snapshot(text_area_widgets.nth(4), name="st_text_area-placeholder")
33
    assert_snapshot(text_area_widgets.nth(5), name="st_text_area-disabled")
34
    assert_snapshot(text_area_widgets.nth(6), name="st_text_area-hidden_label")
35
    assert_snapshot(text_area_widgets.nth(7), name="st_text_area-collapsed_label")
36
    assert_snapshot(text_area_widgets.nth(8), name="st_text_area-callback_help")
37
    assert_snapshot(text_area_widgets.nth(9), name="st_text_area-max_chars_5")
38
    assert_snapshot(text_area_widgets.nth(10), name="st_text_area-height_250")
39

40

41
def test_text_area_has_correct_initial_values(app: Page):
42
    """Test that st.text_area has the correct initial values."""
43
    markdown_elements = app.get_by_test_id("stMarkdown")
44
    expect(markdown_elements).to_have_count(12)
45

46
    expected = [
47
        "value 1: ",
48
        "value 2: some text",
49
        "value 3: 1234",
50
        "value 4: None",
51
        "value 5: ",
52
        "value 6: default text",
53
        "value 7: default text",
54
        "value 8: default text",
55
        "value 9: ",
56
        "text area changed: False",
57
        "value 10: 1234",
58
        "value 11: default text",
59
    ]
60

61
    for markdown_element, expected_text in zip(markdown_elements.all(), expected):
62
        expect(markdown_element).to_have_text(expected_text, use_inner_text=True)
63

64

65
def test_text_area_shows_instructions_when_dirty(
66
    app: Page, assert_snapshot: ImageCompareFunction
67
):
68
    """Test that st.text_area shows the instructions correctly when dirty."""
69
    text_area = app.get_by_test_id("stTextArea").nth(9)
70

71
    text_area_field = text_area.locator("textarea").first
72
    text_area_field.fill("123")
73

74
    assert_snapshot(text_area, name="st_text_area-input_instructions")
75

76

77
def test_text_area_limits_input_via_max_chars(app: Page):
78
    """Test that st.text_area correctly limits the number of characters via max_chars."""
79
    text_area_field = app.get_by_test_id("stTextArea").nth(9).locator("textarea").first
80
    # Try typing in char by char:
81
    text_area_field.clear()
82
    text_area_field.type("12345678")
83
    text_area_field.press("Control+Enter")
84

85
    expect(app.get_by_test_id("stMarkdown").nth(10)).to_have_text(
86
        "value 10: 12345", use_inner_text=True
87
    )
88

89
    # Try filling in everything at once:
90
    text_area_field.focus()
91
    text_area_field.fill("12345678")
92
    text_area_field.press("Control+Enter")
93

94
    expect(app.get_by_test_id("stMarkdown").nth(10)).to_have_text(
95
        "value 10: 12345", use_inner_text=True
96
    )
97

98

99
def test_text_area_has_correct_value_on_blur(app: Page):
100
    """Test that st.text_area has the correct value on blur."""
101

102
    first_text_area_field = (
103
        app.get_by_test_id("stTextArea").first.locator("textarea").first
104
    )
105
    first_text_area_field.focus()
106
    first_text_area_field.fill("hello world")
107
    first_text_area_field.blur()
108

109
    expect(app.get_by_test_id("stMarkdown").first).to_have_text(
110
        "value 1: hello world", use_inner_text=True
111
    )
112

113

114
@pytest.mark.skip_browser(
115
    "firefox"  # The meta key + enter press doesn't work in the playwright firefox test
116
)
117
def test_text_area_has_correct_value_on_enter(app: Page):
118
    """Test that st.text_area has the correct value on enter."""
119

120
    first_text_area_field = (
121
        app.get_by_test_id("stTextArea").first.locator("textarea").first
122
    )
123
    # Test control + enter:
124
    first_text_area_field.focus()
125
    first_text_area_field.fill("hello world")
126
    first_text_area_field.press("Control+Enter")
127

128
    expect(app.get_by_test_id("stMarkdown").first).to_have_text(
129
        "value 1: hello world", use_inner_text=True
130
    )
131

132
    # Test command (Meta key) + enter:
133
    first_text_area_field.focus()
134
    first_text_area_field.fill("different value")
135
    first_text_area_field.press("Meta+Enter")
136

137
    expect(app.get_by_test_id("stMarkdown").first).to_have_text(
138
        "value 1: different value", use_inner_text=True
139
    )
140

141

142
def test_text_area_has_correct_value_on_click_outside(app: Page):
143
    """Test that st.text_area has the correct value on click outside."""
144

145
    first_text_area_field = (
146
        app.get_by_test_id("stTextArea").first.locator("textarea").first
147
    )
148
    first_text_area_field.focus()
149
    first_text_area_field.fill("hello world")
150
    app.get_by_test_id("stMarkdown").first.click()
151

152
    expect(app.get_by_test_id("stMarkdown").first).to_have_text(
153
        "value 1: hello world", use_inner_text=True
154
    )
155

156

157
def test_empty_text_area_behaves_correctly(app: Page):
158
    """Test that st.text_area behaves correctly when empty."""
159
    # Should return None as value:
160
    expect(app.get_by_test_id("stMarkdown").nth(3)).to_have_text(
161
        "value 4: None", use_inner_text=True
162
    )
163

164
    # Enter value in the empty widget:
165
    empty_text_area = app.get_by_test_id("stTextArea").nth(3)
166
    empty_text_area_field = empty_text_area.locator("textarea").first
167
    empty_text_area_field.fill("hello world")
168
    empty_text_area_field.press("Control+Enter")
169

170
    expect(app.get_by_test_id("stMarkdown").nth(3)).to_have_text(
171
        "value 4: hello world", use_inner_text=True
172
    )
173

174
    # Press escape to clear value:
175
    empty_text_area_field.focus()
176
    empty_text_area_field.clear()
177
    empty_text_area_field.press("Control+Enter")
178

179
    # Should be set to empty string (we don't clear to None for text area):
180
    expect(app.get_by_test_id("stMarkdown").nth(3)).to_have_text(
181
        "value 4: ", use_inner_text=True
182
    )
183

184

185
def test_calls_callback_on_change(app: Page):
186
    """Test that it correctly calls the callback on change."""
187
    text_area_field = app.get_by_test_id("stTextArea").nth(8).locator("textarea").first
188

189
    text_area_field.fill("hello world")
190
    text_area_field.press("Control+Enter")
191

192
    expect(app.get_by_test_id("stMarkdown").nth(8)).to_have_text(
193
        "value 9: hello world",
194
        use_inner_text=True,
195
    )
196
    expect(app.get_by_test_id("stMarkdown").nth(9)).to_have_text(
197
        "text area changed: True",
198
        use_inner_text=True,
199
    )
200

201
    # Change different widget to trigger delta path change
202
    first_text_area_field = (
203
        app.get_by_test_id("stTextArea").first.locator("textarea").first
204
    )
205
    first_text_area_field.fill("hello world")
206
    first_text_area_field.press("Control+Enter")
207

208
    expect(app.get_by_test_id("stMarkdown").first).to_have_text(
209
        "value 1: hello world", use_inner_text=True
210
    )
211

212
    # Test if value is still correct after delta path change
213
    expect(app.get_by_test_id("stMarkdown").nth(8)).to_have_text(
214
        "value 9: hello world",
215
        use_inner_text=True,
216
    )
217
    expect(app.get_by_test_id("stMarkdown").nth(9)).to_have_text(
218
        "text area changed: False",
219
        use_inner_text=True,
220
    )
221

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

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

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

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