streamlit
105 строк · 4.5 Кб
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, expect
16
17from e2e_playwright.conftest import ImageCompareFunction
18
19
20def test_button_widget_rendering(
21themed_app: Page, assert_snapshot: ImageCompareFunction
22):
23"""Test that the button widgets are correctly rendered via screenshot matching."""
24button_elements = themed_app.get_by_test_id("stButton")
25expect(button_elements).to_have_count(15)
26
27assert_snapshot(button_elements.nth(0), name="st_button-default")
28assert_snapshot(button_elements.nth(1), name="st_button-disabled")
29assert_snapshot(button_elements.nth(2), name="st_button-primary")
30assert_snapshot(button_elements.nth(3), name="st_button-disabled_primary")
31assert_snapshot(button_elements.nth(4), name="st_button-use_container_width")
32assert_snapshot(button_elements.nth(5), name="st_button-use_container_width_help")
33assert_snapshot(button_elements.nth(6), name="st_button-styled_label")
34
35# The rest is tested in one screenshot in the following test
36
37
38def test_buttons_in_columns(themed_app: Page, assert_snapshot: ImageCompareFunction):
39"""Test that the button widgets are correctly rendered in columns via screenshot matching."""
40columns_container = themed_app.get_by_test_id("stHorizontalBlock")
41expect(columns_container).to_have_count(1)
42
43assert_snapshot(columns_container, name="st_button-in_columns")
44
45
46def test_value_correct_on_click(app: Page):
47button_element = app.get_by_test_id("stButton").locator("button").first
48button_element.click()
49expect(app.get_by_test_id("stMarkdown").nth(0)).to_have_text("value: True")
50expect(app.get_by_test_id("stMarkdown").nth(1)).to_have_text(
51"value from state: True"
52)
53
54
55def test_value_not_reset_on_reclick(app: Page):
56button_element = app.get_by_test_id("stButton").locator("button").first
57button_element.click()
58button_element.click()
59expect(app.get_by_test_id("stMarkdown").first).to_have_text("value: True")
60
61
62def test_click_calls_callback(app: Page):
63button_element = app.get_by_test_id("stButton").locator("button").first
64expect(app.get_by_test_id("stMarkdown").nth(2)).to_contain_text(
65"Button was clicked: False"
66)
67button_element.click()
68expect(app.get_by_test_id("stMarkdown").nth(2)).to_have_text(
69"Button was clicked: True"
70)
71expect(app.get_by_test_id("stMarkdown").nth(3)).to_have_text("times clicked: 1")
72expect(app.get_by_test_id("stMarkdown").nth(4)).to_have_text("arg value: 1")
73expect(app.get_by_test_id("stMarkdown").nth(5)).to_have_text("kwarg value: 2")
74
75
76def test_click_increment_count(app: Page):
77button_element = app.get_by_test_id("stButton").locator("button").first
78button_element.click()
79expect(app.get_by_test_id("stMarkdown").nth(3)).to_have_text("times clicked: 1")
80button_element.click()
81expect(app.get_by_test_id("stMarkdown").nth(3)).to_have_text("times clicked: 2")
82button_element.click()
83expect(app.get_by_test_id("stMarkdown").nth(3)).to_have_text("times clicked: 3")
84
85
86def test_reset_on_other_widget_change(app: Page):
87button_element = app.get_by_test_id("stButton").locator("button").first
88checkbox_element = app.get_by_test_id("stCheckbox")
89button_element.click()
90expect(app.get_by_test_id("stMarkdown").nth(0)).to_have_text("value: True")
91expect(app.get_by_test_id("stMarkdown").nth(1)).to_have_text(
92"value from state: True"
93)
94checkbox_element.click()
95expect(app.get_by_test_id("stMarkdown").nth(0)).to_have_text("value: False")
96expect(app.get_by_test_id("stMarkdown").nth(1)).to_have_text(
97"value from state: False"
98)
99
100
101def test_show_tooltip_on_hover(app: Page, assert_snapshot: ImageCompareFunction):
102button_element = app.get_by_test_id("stButton").nth(5)
103button_element.hover()
104assert_snapshot(button_element, name="st_button-on_hover")
105expect(app.get_by_test_id("stTooltipContent")).to_have_text("help text")
106