streamlit
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
15from playwright.sync_api import Page, expect16
17from e2e_playwright.conftest import ImageCompareFunction, wait_for_app_run18
19EXPANDER_HEADER_IDENTIFIER = "summary"20
21
22def test_expander_displays_correctly(23themed_app: Page, assert_snapshot: ImageCompareFunction24):25"""Test that all expanders are displayed correctly via screenshot testing."""26expander_elements = themed_app.get_by_test_id("stExpander")27expect(expander_elements).to_have_count(6)28
29for expander in expander_elements.all():30expect(expander.locator(EXPANDER_HEADER_IDENTIFIER)).to_be_visible()31
32assert_snapshot(expander_elements.nth(0), name="st_expander-sidebar_collapsed")33assert_snapshot(expander_elements.nth(1), name="st_expander-normal_expanded")34assert_snapshot(expander_elements.nth(2), name="st_expander-normal_collapsed")35assert_snapshot(expander_elements.nth(3), name="st_expander-with_input")36assert_snapshot(expander_elements.nth(4), name="st_expander-long_expanded")37assert_snapshot(expander_elements.nth(5), name="st_expander-long_collapsed")38
39
40def test_expander_collapses_and_expands(app: Page):41"""Test that an expander collapses and expands."""42main_container = app.get_by_test_id("stAppViewBlockContainer")43main_expanders = main_container.get_by_test_id("stExpander")44expect(main_expanders).to_have_count(5)45
46expanders = main_expanders.all()47# Starts expanded48expander_header = expanders[0].locator(EXPANDER_HEADER_IDENTIFIER)49expect(expander_header).to_be_visible()50toggle = expander_header.locator("svg").first51expect(toggle).to_be_visible()52expander_header.click()53toggle = expander_header.locator("svg").first54expect(toggle).to_be_visible()55
56# Starts collapsed57expander_header = expanders[1].locator(EXPANDER_HEADER_IDENTIFIER)58expect(expander_header).to_be_visible()59toggle = expander_header.locator("svg").first60expect(toggle).to_be_visible()61expander_header.click()62toggle = expander_header.locator("svg").first63expect(toggle).to_be_visible()64
65
66def test_empty_expander_not_rendered(app: Page):67"""Test that an empty expander is not rendered."""68expect(app.get_by_text("Empty expander")).not_to_be_attached()69
70
71def test_expander_session_state_set(app: Page):72"""Test that session state updates are propagated to expander content"""73main_container = app.get_by_test_id("stAppViewBlockContainer")74main_expanders = main_container.get_by_test_id("stExpander")75expect(main_expanders).to_have_count(5)76
77# Show the Number Input78num_input = main_expanders.nth(2).get_by_test_id("stNumberInput").locator("input")79num_input.fill("10")80num_input.press("Enter")81wait_for_app_run(app)82
83# Hide the Number Input84main_expanders.nth(2).locator(EXPANDER_HEADER_IDENTIFIER).click()85
86app.get_by_text("Update Num Input").click()87wait_for_app_run(app)88
89app.get_by_text("Print State Value").click()90wait_for_app_run(app)91
92text_elements = app.get_by_test_id("stText")93expect(text_elements).to_have_count(2)94
95expect(text_elements.nth(0)).to_have_text("0.0", use_inner_text=True)96expect(text_elements.nth(1)).to_have_text("0.0", use_inner_text=True)97