forked from streamlit/streamlit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathst_expander_test.py
119 lines (93 loc) · 4.62 KB
/
st_expander_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2024)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from playwright.sync_api import Page, expect
from e2e_playwright.conftest import ImageCompareFunction, wait_for_app_run
from e2e_playwright.shared.app_utils import check_top_level_class, get_expander
EXPANDER_HEADER_IDENTIFIER = "summary"
def test_expander_displays_correctly(
themed_app: Page, assert_snapshot: ImageCompareFunction
):
"""Test that all expanders are displayed correctly via screenshot testing."""
expander_elements = themed_app.get_by_test_id("stExpander")
expect(expander_elements).to_have_count(8)
for expander in expander_elements.all():
expect(expander.locator(EXPANDER_HEADER_IDENTIFIER)).to_be_visible()
assert_snapshot(expander_elements.nth(0), name="st_expander-sidebar_collapsed")
assert_snapshot(expander_elements.nth(1), name="st_expander-normal_expanded")
assert_snapshot(expander_elements.nth(2), name="st_expander-normal_collapsed")
assert_snapshot(expander_elements.nth(3), name="st_expander-with_input")
assert_snapshot(expander_elements.nth(4), name="st_expander-long_expanded")
assert_snapshot(expander_elements.nth(5), name="st_expander-long_collapsed")
assert_snapshot(expander_elements.nth(6), name="st_expander-with_material_icon")
assert_snapshot(expander_elements.nth(7), name="st_expander-with_emoji_icon")
def test_expander_collapses_and_expands(app: Page):
"""Test that an expander collapses and expands."""
main_container = app.get_by_test_id("stMain")
main_expanders = main_container.get_by_test_id("stExpander")
expect(main_expanders).to_have_count(7)
expanders = main_expanders.all()
# Starts expanded
expander_header = expanders[0].locator(EXPANDER_HEADER_IDENTIFIER)
expect(expander_header).to_be_visible()
toggle = expander_header.locator("svg").first
expect(toggle).to_be_visible()
expander_header.click()
toggle = expander_header.locator("svg").first
expect(toggle).to_be_visible()
# Starts collapsed
expander_header = expanders[1].locator(EXPANDER_HEADER_IDENTIFIER)
expect(expander_header).to_be_visible()
toggle = expander_header.locator("svg").first
expect(toggle).to_be_visible()
expander_header.click()
toggle = expander_header.locator("svg").first
expect(toggle).to_be_visible()
def test_empty_expander_not_rendered(app: Page):
"""Test that an empty expander is not rendered."""
expect(app.get_by_text("Empty expander")).not_to_be_attached()
def test_expander_session_state_set(app: Page):
"""Test that session state updates are propagated to expander content"""
main_container = app.get_by_test_id("stMain")
main_expanders = main_container.get_by_test_id("stExpander")
expect(main_expanders).to_have_count(7)
# Show the Number Input
num_input = main_expanders.nth(2).get_by_test_id("stNumberInput").locator("input")
num_input.fill("10")
num_input.press("Enter")
wait_for_app_run(app)
# Hide the Number Input
main_expanders.nth(2).locator(EXPANDER_HEADER_IDENTIFIER).click()
app.get_by_text("Update Num Input").click()
wait_for_app_run(app)
app.get_by_text("Print State Value").click()
wait_for_app_run(app)
text_elements = app.get_by_test_id("stText")
expect(text_elements).to_have_count(2)
expect(text_elements.nth(0)).to_have_text("0.0", use_inner_text=True)
expect(text_elements.nth(1)).to_have_text("0.0", use_inner_text=True)
def test_expander_renders_icon(app: Page):
"""Test that an expander renders a material icon and an emoji icon."""
material_icon = get_expander(app, "Expander with material icon!").get_by_test_id(
"stExpanderIcon"
)
expect(material_icon).to_be_visible()
expect(material_icon).to_have_text("bolt")
emoji_icon = get_expander(app, "Expander with emoji icon!").get_by_test_id(
"stExpanderIcon"
)
expect(emoji_icon).to_be_visible()
expect(emoji_icon).to_have_text("🎈")
def test_check_top_level_class(app: Page):
"""Check that the top level class is correctly set."""
check_top_level_class(app, "stExpander")