streamlit
81 строка · 3.0 Кб
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, expect17
18from e2e_playwright.conftest import ImageCompareFunction, wait_for_app_run19
20
21def test_toggle_widget_display(themed_app: Page, assert_snapshot: ImageCompareFunction):22"""Test that st.toggle renders correctly."""23toggle_elements = themed_app.get_by_test_id("stCheckbox")24expect(toggle_elements).to_have_count(8)25
26for i, element in enumerate(toggle_elements.all()):27assert_snapshot(element, name=f"st_toggle-{i}")28
29
30def test_toggle_initial_values(app: Page):31"""Test that st.toggle has the correct initial values."""32markdown_elements = app.get_by_test_id("stMarkdown")33expect(markdown_elements).to_have_count(9)34
35expected = [36"toggle 1 - value: True",37"toggle 2 - value: False",38"toggle 3 - value: False",39"toggle 4 - value: False",40"toggle 4 - clicked: False",41"toggle 5 - value: False",42"toggle 6 - value: True",43"toggle 7 - value: False",44"toggle 8 - value: False",45]46
47for markdown_element, expected_text in zip(markdown_elements.all(), expected):48expect(markdown_element).to_have_text(expected_text, use_inner_text=True)49
50
51def test_toggle_values_on_click(app: Page):52"""Test that st.toggle updates values correctly when user clicks."""53toggle_elements = app.get_by_test_id("stCheckbox")54expect(toggle_elements).to_have_count(8)55
56for toggle_element in toggle_elements.all():57# Not sure if this is needed, but somehow it is slightly58# flaky with the last toggle without it.59# It seems that it sometimes fails to click,60# and in these cases the toggle was not scrolled into view.61# So, maybe thats the reason why it fails to click it.62# But this is just a guess.63toggle_element.scroll_into_view_if_needed()64toggle_element.click(delay=50)65wait_for_app_run(app)66
67markdown_elements = app.get_by_test_id("stMarkdown")68expected = [69"toggle 1 - value: False",70"toggle 2 - value: True",71"toggle 3 - value: True",72"toggle 4 - value: True",73"toggle 4 - clicked: True",74"toggle 5 - value: False",75"toggle 6 - value: True",76"toggle 7 - value: True",77"toggle 8 - value: True",78]79
80for markdown_element, expected_text in zip(markdown_elements.all(), expected):81expect(markdown_element).to_have_text(expected_text, use_inner_text=True)82