streamlit
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
15import pytest
16from playwright.sync_api import Page, expect
17
18from e2e_playwright.conftest import ImageCompareFunction
19
20
21def test_text_area_widget_rendering(
22themed_app: Page, assert_snapshot: ImageCompareFunction
23):
24"""Test that the st.text_area widgets are correctly rendered via screenshot matching."""
25text_area_widgets = themed_app.get_by_test_id("stTextArea")
26expect(text_area_widgets).to_have_count(11)
27
28assert_snapshot(text_area_widgets.nth(0), name="st_text_area-default")
29assert_snapshot(text_area_widgets.nth(1), name="st_text_area-value_some_text")
30assert_snapshot(text_area_widgets.nth(2), name="st_text_area-value_1234")
31assert_snapshot(text_area_widgets.nth(3), name="st_text_area-value_None")
32assert_snapshot(text_area_widgets.nth(4), name="st_text_area-placeholder")
33assert_snapshot(text_area_widgets.nth(5), name="st_text_area-disabled")
34assert_snapshot(text_area_widgets.nth(6), name="st_text_area-hidden_label")
35assert_snapshot(text_area_widgets.nth(7), name="st_text_area-collapsed_label")
36assert_snapshot(text_area_widgets.nth(8), name="st_text_area-callback_help")
37assert_snapshot(text_area_widgets.nth(9), name="st_text_area-max_chars_5")
38assert_snapshot(text_area_widgets.nth(10), name="st_text_area-height_250")
39
40
41def test_text_area_has_correct_initial_values(app: Page):
42"""Test that st.text_area has the correct initial values."""
43markdown_elements = app.get_by_test_id("stMarkdown")
44expect(markdown_elements).to_have_count(12)
45
46expected = [
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
61for markdown_element, expected_text in zip(markdown_elements.all(), expected):
62expect(markdown_element).to_have_text(expected_text, use_inner_text=True)
63
64
65def test_text_area_shows_instructions_when_dirty(
66app: Page, assert_snapshot: ImageCompareFunction
67):
68"""Test that st.text_area shows the instructions correctly when dirty."""
69text_area = app.get_by_test_id("stTextArea").nth(9)
70
71text_area_field = text_area.locator("textarea").first
72text_area_field.fill("123")
73
74assert_snapshot(text_area, name="st_text_area-input_instructions")
75
76
77def 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."""
79text_area_field = app.get_by_test_id("stTextArea").nth(9).locator("textarea").first
80# Try typing in char by char:
81text_area_field.clear()
82text_area_field.type("12345678")
83text_area_field.press("Control+Enter")
84
85expect(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:
90text_area_field.focus()
91text_area_field.fill("12345678")
92text_area_field.press("Control+Enter")
93
94expect(app.get_by_test_id("stMarkdown").nth(10)).to_have_text(
95"value 10: 12345", use_inner_text=True
96)
97
98
99def test_text_area_has_correct_value_on_blur(app: Page):
100"""Test that st.text_area has the correct value on blur."""
101
102first_text_area_field = (
103app.get_by_test_id("stTextArea").first.locator("textarea").first
104)
105first_text_area_field.focus()
106first_text_area_field.fill("hello world")
107first_text_area_field.blur()
108
109expect(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)
117def test_text_area_has_correct_value_on_enter(app: Page):
118"""Test that st.text_area has the correct value on enter."""
119
120first_text_area_field = (
121app.get_by_test_id("stTextArea").first.locator("textarea").first
122)
123# Test control + enter:
124first_text_area_field.focus()
125first_text_area_field.fill("hello world")
126first_text_area_field.press("Control+Enter")
127
128expect(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:
133first_text_area_field.focus()
134first_text_area_field.fill("different value")
135first_text_area_field.press("Meta+Enter")
136
137expect(app.get_by_test_id("stMarkdown").first).to_have_text(
138"value 1: different value", use_inner_text=True
139)
140
141
142def 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
145first_text_area_field = (
146app.get_by_test_id("stTextArea").first.locator("textarea").first
147)
148first_text_area_field.focus()
149first_text_area_field.fill("hello world")
150app.get_by_test_id("stMarkdown").first.click()
151
152expect(app.get_by_test_id("stMarkdown").first).to_have_text(
153"value 1: hello world", use_inner_text=True
154)
155
156
157def test_empty_text_area_behaves_correctly(app: Page):
158"""Test that st.text_area behaves correctly when empty."""
159# Should return None as value:
160expect(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:
165empty_text_area = app.get_by_test_id("stTextArea").nth(3)
166empty_text_area_field = empty_text_area.locator("textarea").first
167empty_text_area_field.fill("hello world")
168empty_text_area_field.press("Control+Enter")
169
170expect(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:
175empty_text_area_field.focus()
176empty_text_area_field.clear()
177empty_text_area_field.press("Control+Enter")
178
179# Should be set to empty string (we don't clear to None for text area):
180expect(app.get_by_test_id("stMarkdown").nth(3)).to_have_text(
181"value 4: ", use_inner_text=True
182)
183
184
185def test_calls_callback_on_change(app: Page):
186"""Test that it correctly calls the callback on change."""
187text_area_field = app.get_by_test_id("stTextArea").nth(8).locator("textarea").first
188
189text_area_field.fill("hello world")
190text_area_field.press("Control+Enter")
191
192expect(app.get_by_test_id("stMarkdown").nth(8)).to_have_text(
193"value 9: hello world",
194use_inner_text=True,
195)
196expect(app.get_by_test_id("stMarkdown").nth(9)).to_have_text(
197"text area changed: True",
198use_inner_text=True,
199)
200
201# Change different widget to trigger delta path change
202first_text_area_field = (
203app.get_by_test_id("stTextArea").first.locator("textarea").first
204)
205first_text_area_field.fill("hello world")
206first_text_area_field.press("Control+Enter")
207
208expect(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
213expect(app.get_by_test_id("stMarkdown").nth(8)).to_have_text(
214"value 9: hello world",
215use_inner_text=True,
216)
217expect(app.get_by_test_id("stMarkdown").nth(9)).to_have_text(
218"text area changed: False",
219use_inner_text=True,
220)
221