streamlit

Форк
0
/
st_expander_test.py 
96 строк · 3.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
from playwright.sync_api import Page, expect
16

17
from e2e_playwright.conftest import ImageCompareFunction, wait_for_app_run
18

19
EXPANDER_HEADER_IDENTIFIER = "summary"
20

21

22
def test_expander_displays_correctly(
23
    themed_app: Page, assert_snapshot: ImageCompareFunction
24
):
25
    """Test that all expanders are displayed correctly via screenshot testing."""
26
    expander_elements = themed_app.get_by_test_id("stExpander")
27
    expect(expander_elements).to_have_count(6)
28

29
    for expander in expander_elements.all():
30
        expect(expander.locator(EXPANDER_HEADER_IDENTIFIER)).to_be_visible()
31

32
    assert_snapshot(expander_elements.nth(0), name="st_expander-sidebar_collapsed")
33
    assert_snapshot(expander_elements.nth(1), name="st_expander-normal_expanded")
34
    assert_snapshot(expander_elements.nth(2), name="st_expander-normal_collapsed")
35
    assert_snapshot(expander_elements.nth(3), name="st_expander-with_input")
36
    assert_snapshot(expander_elements.nth(4), name="st_expander-long_expanded")
37
    assert_snapshot(expander_elements.nth(5), name="st_expander-long_collapsed")
38

39

40
def test_expander_collapses_and_expands(app: Page):
41
    """Test that an expander collapses and expands."""
42
    main_container = app.get_by_test_id("stAppViewBlockContainer")
43
    main_expanders = main_container.get_by_test_id("stExpander")
44
    expect(main_expanders).to_have_count(5)
45

46
    expanders = main_expanders.all()
47
    # Starts expanded
48
    expander_header = expanders[0].locator(EXPANDER_HEADER_IDENTIFIER)
49
    expect(expander_header).to_be_visible()
50
    toggle = expander_header.locator("svg").first
51
    expect(toggle).to_be_visible()
52
    expander_header.click()
53
    toggle = expander_header.locator("svg").first
54
    expect(toggle).to_be_visible()
55

56
    # Starts collapsed
57
    expander_header = expanders[1].locator(EXPANDER_HEADER_IDENTIFIER)
58
    expect(expander_header).to_be_visible()
59
    toggle = expander_header.locator("svg").first
60
    expect(toggle).to_be_visible()
61
    expander_header.click()
62
    toggle = expander_header.locator("svg").first
63
    expect(toggle).to_be_visible()
64

65

66
def test_empty_expander_not_rendered(app: Page):
67
    """Test that an empty expander is not rendered."""
68
    expect(app.get_by_text("Empty expander")).not_to_be_attached()
69

70

71
def test_expander_session_state_set(app: Page):
72
    """Test that session state updates are propagated to expander content"""
73
    main_container = app.get_by_test_id("stAppViewBlockContainer")
74
    main_expanders = main_container.get_by_test_id("stExpander")
75
    expect(main_expanders).to_have_count(5)
76

77
    # Show the Number Input
78
    num_input = main_expanders.nth(2).get_by_test_id("stNumberInput").locator("input")
79
    num_input.fill("10")
80
    num_input.press("Enter")
81
    wait_for_app_run(app)
82

83
    # Hide the Number Input
84
    main_expanders.nth(2).locator(EXPANDER_HEADER_IDENTIFIER).click()
85

86
    app.get_by_text("Update Num Input").click()
87
    wait_for_app_run(app)
88

89
    app.get_by_text("Print State Value").click()
90
    wait_for_app_run(app)
91

92
    text_elements = app.get_by_test_id("stText")
93
    expect(text_elements).to_have_count(2)
94

95
    expect(text_elements.nth(0)).to_have_text("0.0", use_inner_text=True)
96
    expect(text_elements.nth(1)).to_have_text("0.0", use_inner_text=True)
97

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

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

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

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