streamlit

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

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_toggle_widget_display(themed_app: Page, assert_snapshot: ImageCompareFunction):
22
    """Test that st.toggle renders correctly."""
23
    toggle_elements = themed_app.get_by_test_id("stCheckbox")
24
    expect(toggle_elements).to_have_count(8)
25

26
    for i, element in enumerate(toggle_elements.all()):
27
        assert_snapshot(element, name=f"st_toggle-{i}")
28

29

30
def test_toggle_initial_values(app: Page):
31
    """Test that st.toggle has the correct initial values."""
32
    markdown_elements = app.get_by_test_id("stMarkdown")
33
    expect(markdown_elements).to_have_count(9)
34

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

47
    for markdown_element, expected_text in zip(markdown_elements.all(), expected):
48
        expect(markdown_element).to_have_text(expected_text, use_inner_text=True)
49

50

51
def test_toggle_values_on_click(app: Page):
52
    """Test that st.toggle updates values correctly when user clicks."""
53
    toggle_elements = app.get_by_test_id("stCheckbox")
54
    expect(toggle_elements).to_have_count(8)
55

56
    for toggle_element in toggle_elements.all():
57
        # Not sure if this is needed, but somehow it is slightly
58
        # 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.
63
        toggle_element.scroll_into_view_if_needed()
64
        toggle_element.click(delay=50)
65
        wait_for_app_run(app)
66

67
    markdown_elements = app.get_by_test_id("stMarkdown")
68
    expected = [
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

80
    for markdown_element, expected_text in zip(markdown_elements.all(), expected):
81
        expect(markdown_element).to_have_text(expected_text, use_inner_text=True)
82

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

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

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

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