forked from streamlit/streamlit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathst_download_button_test.py
148 lines (115 loc) · 5.5 KB
/
st_download_button_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# 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
from e2e_playwright.shared.app_utils import (
check_top_level_class,
click_checkbox,
get_element_by_key,
)
def test_download_button_widget_rendering(
themed_app: Page, assert_snapshot: ImageCompareFunction
):
"""Test that download buttons are correctly rendered via screenshot matching."""
download_buttons = themed_app.get_by_test_id("stDownloadButton")
expect(download_buttons).to_have_count(11)
assert_snapshot(download_buttons.nth(0), name="st_download_button-default")
assert_snapshot(download_buttons.nth(1), name="st_download_button-disabled")
assert_snapshot(
download_buttons.nth(4), name="st_download_button-use_container_width"
)
assert_snapshot(
download_buttons.nth(5), name="st_download_button-use_container_width_help"
)
assert_snapshot(download_buttons.nth(6), name="st_download_button-primary")
assert_snapshot(download_buttons.nth(7), name="st_download_button-emoji_icon")
assert_snapshot(download_buttons.nth(8), name="st_download_button-material_icon")
def test_show_tooltip_on_hover(app: Page, assert_snapshot: ImageCompareFunction):
download_button = app.get_by_test_id("stDownloadButton").nth(5)
download_button.hover()
expect(app.get_by_test_id("stTooltipContent")).to_have_text("Example help text")
def test_value_correct_on_click(app: Page):
download_button = app.get_by_test_id("stDownloadButton").nth(9).locator("button")
download_button.click()
expect(app.get_by_test_id("stMarkdown").first).to_have_text("value: True")
def test_value_not_reset_on_reclick(app: Page):
download_button = app.get_by_test_id("stDownloadButton").nth(9).locator("button")
download_button.click()
download_button.click()
expect(app.get_by_test_id("stMarkdown").first).to_have_text("value: True")
def test_click_calls_callback(app: Page):
download_button = app.get_by_test_id("stDownloadButton").nth(10).locator("button")
expect(app.get_by_test_id("stMarkdown").nth(3)).to_contain_text(
"Download Button was clicked: False"
)
download_button.click()
expect(app.get_by_test_id("stMarkdown").nth(3)).to_have_text(
"Download Button was clicked: True"
)
expect(app.get_by_test_id("stMarkdown").nth(4)).to_have_text("times clicked: 1")
expect(app.get_by_test_id("stMarkdown").nth(5)).to_have_text("arg value: 1")
expect(app.get_by_test_id("stMarkdown").nth(6)).to_have_text("kwarg value: 2")
def test_reset_on_other_widget_change(app: Page):
download_button = app.get_by_test_id("stDownloadButton").nth(10).locator("button")
download_button.click()
expect(app.get_by_test_id("stMarkdown").nth(1)).to_have_text("value: True")
expect(app.get_by_test_id("stMarkdown").nth(2)).to_have_text(
"value from state: True"
)
click_checkbox(app, "reset button return value")
expect(app.get_by_test_id("stMarkdown").nth(1)).to_have_text("value: False")
expect(app.get_by_test_id("stMarkdown").nth(2)).to_have_text(
"value from state: False"
)
def test_downloads_RAR_file_on_click(app: Page):
# Start waiting for the download
with app.expect_download() as download_info:
# Perform the action that initiates download
download_button_element = (
app.get_by_test_id("stDownloadButton").locator("button").nth(2)
)
download_button_element.click()
download = download_info.value
file_name = download.suggested_filename
assert file_name == "archive.rar"
def test_downloads_image_file_on_click(app: Page):
# Start waiting for the download
with app.expect_download() as download_info:
# Perform the action that initiates download
download_button_element = (
app.get_by_test_id("stDownloadButton").locator("button").nth(3)
)
download_button_element.click()
download = download_info.value
file_name = download.suggested_filename
assert file_name == "cat.jpg"
def test_downloads_txt_file_on_click(app: Page):
# Start waiting for the download
with app.expect_download() as download_info:
# Perform the action that initiates download
download_button_element = (
app.get_by_test_id("stDownloadButton").locator("button").first
)
download_button_element.click()
download = download_info.value
file_name = download.suggested_filename
file_text = download.path().read_text()
assert file_name == "hello.txt"
assert file_text == "Hello world!"
def test_check_top_level_class(app: Page):
"""Check that the top level class is correctly set."""
check_top_level_class(app, "stDownloadButton")
def test_custom_css_class_via_key(app: Page):
"""Test that the element can have a custom css class via the key argument."""
expect(get_element_by_key(app, "download_button")).to_be_visible()