forked from streamlit/streamlit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathst_heading_test.py
289 lines (221 loc) · 11.1 KB
/
st_heading_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# 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.
import re
import pytest
from playwright.sync_api import Locator, Page, expect
from e2e_playwright.conftest import ImageCompareFunction, wait_for_app_loaded
from e2e_playwright.shared.app_utils import check_top_level_class, expect_help_tooltip
def _get_title_elements(app: Page) -> Locator:
"""Title elements are rendered as h1 elements"""
return app.get_by_test_id("stHeading").locator("h1")
def _get_header_elements(app: Page) -> Locator:
"""Header elements are rendered as h2 elements"""
return app.get_by_test_id("stHeading").locator("h2")
def _get_subheader_elements(app: Page) -> Locator:
"""Subheader elements are rendered as h3 elements"""
return app.get_by_test_id("stHeading").locator("h3")
_header_divider_filter_text = re.compile(r"[a-zA-Z]+ Header Divider:")
_subheader_divider_filter_text = re.compile(r"[a-zA-Z]+ Subheader Divider:")
def test_correct_number_and_content_of_title_elements(app: Page):
"""Test that correct number of st.title (=> h1) exist with the right content"""
titles = _get_title_elements(app)
expect(titles).to_have_count(6)
expect(titles.nth(0)).to_have_text("info This title is awesome!")
expect(titles.nth(1)).to_have_text("This title is awesome too!")
expect(titles.nth(2)).to_have_text("Code - Title with hidden Anchor")
expect(titles.nth(3)).to_have_text("a link")
expect(titles.nth(4)).to_have_text("日本語タイトル")
expect(titles.nth(5)).to_have_text("その他の邦題")
def test_correct_number_and_content_of_header_elements(app: Page):
"""Test that correct number of st.header (=> h2) exist with the right content"""
headers = _get_header_elements(app).filter(has_not_text=_header_divider_filter_text)
expect(headers).to_have_count(5)
expect(headers.nth(0)).to_have_text("info This header is awesome!")
expect(headers.nth(1)).to_have_text("This header is awesome too!")
expect(headers.nth(2)).to_have_text(
"This header with hidden anchor is awesome tooooo!"
)
def test_correct_number_and_content_of_subheader_elements(app: Page):
"""Test that correct number of st.subheader (=> h3) exist with the right content"""
subheaders = _get_subheader_elements(app).filter(
has_not_text=_subheader_divider_filter_text
)
expect(subheaders).to_have_count(8)
expect(subheaders.nth(0)).to_have_text("info This subheader is awesome!")
expect(subheaders.nth(1)).to_have_text("This subheader is awesome too!")
expect(subheaders.nth(2)).to_have_text("Code - Subheader without Anchor")
expect(subheaders.nth(3)).to_have_text("Code - Subheader with Anchor test_link")
expect(subheaders.nth(4)).to_have_text("Subheader with hidden Anchor")
def test_display_titles_with_anchors(app: Page):
titles = _get_title_elements(app)
expect(titles.nth(0)).to_have_id("info-this-title-is-awesome")
expect(titles.nth(1)).to_have_id("awesome-title")
expect(titles.nth(2)).to_have_id("code-title-with-hidden-anchor")
expect(titles.nth(3)).to_have_id("a-link")
# the id is generated based on the title
expect(titles.nth(4)).to_have_id("d3b04b7a")
expect(titles.nth(5)).to_have_id("アンカー")
def test_display_headers_with_anchors_and_style_icons(app: Page):
headers = _get_header_elements(app)
first_header = headers.nth(0)
expect(first_header).to_have_id("info-this-header-is-awesome")
expect(first_header.locator("svg")).to_be_attached()
expect(first_header.locator("a")).to_have_attribute(
"href", "#info-this-header-is-awesome"
)
second_header = headers.nth(1)
expect(second_header).to_have_id("awesome-header")
expect(second_header.locator("svg")).to_be_attached()
expect(second_header.locator("a")).to_have_attribute("href", "#awesome-header")
third_header = headers.nth(2)
expect(third_header).to_have_id("this-header-with-hidden-anchor-is-awesome-tooooo")
expect(third_header.locator("svg")).not_to_be_attached()
def test_display_subheaders_with_anchors_and_style_icons(app: Page):
headers = _get_subheader_elements(app)
first_header = headers.nth(0)
expect(first_header).to_have_id("info-this-subheader-is-awesome")
expect(first_header.locator("svg")).to_be_attached()
expect(first_header.locator("a")).to_have_attribute(
"href", "#info-this-subheader-is-awesome"
)
second_header = headers.nth(1)
expect(second_header).to_have_id("awesome-subheader")
expect(second_header.locator("svg")).to_be_attached()
expect(second_header.locator("a")).to_have_attribute("href", "#awesome-subheader")
third_header = headers.nth(4)
expect(third_header).to_have_id("subheader-with-hidden-anchor")
expect(third_header.locator("svg")).not_to_be_attached()
def test_clicking_on_anchor_changes_url(app: Page):
import re
headers = _get_header_elements(app)
first_header = headers.nth(0)
first_header.hover()
link = first_header.locator("a")
expect(link).to_have_attribute("href", "#info-this-header-is-awesome")
link.click()
expect(app).to_have_url(re.compile(".*#info-this-header-is-awesome"))
def test_headers_snapshot_match(
themed_app: Page, assert_snapshot: ImageCompareFunction
):
headers = _get_header_elements(themed_app)
assert_snapshot(headers.nth(0), name="st_header-simple")
assert_snapshot(headers.nth(3), name="st_header-with_help")
def test_headers_hovered_snapshot_match(
themed_app: Page, assert_snapshot: ImageCompareFunction
):
headers = _get_header_elements(themed_app)
header = headers.nth(0)
link_container = header.get_by_test_id("stHeaderActionElements").locator("a")
expect(link_container).to_have_css("visibility", "hidden")
header.hover()
expect(link_container).to_have_css("visibility", "visible")
assert_snapshot(header, name="st_header-hover_with_visible_anchor")
header = headers.nth(3)
link_container = header.get_by_test_id("stHeaderActionElements").locator("a")
expect(link_container).to_have_css("visibility", "hidden")
header.hover()
expect(link_container).to_have_css("visibility", "visible")
assert_snapshot(header, name="st_header-hover_with_help_and_anchor")
header = headers.nth(4)
link_container = header.get_by_test_id("stHeaderActionElements").locator("a")
expect(link_container).not_to_be_attached()
assert_snapshot(header, name="st_header-hover_with_help_and_hidden_anchor")
def test_subheaders_snapshot_match(
themed_app: Page, assert_snapshot: ImageCompareFunction
):
headers = _get_subheader_elements(themed_app)
assert_snapshot(headers.nth(0), name="st_subheader-simple")
assert_snapshot(headers.nth(5), name="st_subheader-with_code_and_help")
def test_subheaders_hovered_snapshot_match(
themed_app: Page, assert_snapshot: ImageCompareFunction
):
headers = _get_subheader_elements(themed_app)
header = headers.nth(0)
link_container = header.get_by_test_id("stHeaderActionElements").locator("a")
expect(link_container).to_have_css("visibility", "hidden")
header.hover()
expect(link_container).to_have_css("visibility", "visible")
assert_snapshot(header, name="st_subheader-hover_with_visible_anchor")
header = headers.nth(5)
link_container = header.get_by_test_id("stHeaderActionElements").locator("a")
expect(link_container).to_have_css("visibility", "hidden")
header.hover()
expect(link_container).to_have_css("visibility", "visible")
assert_snapshot(header, name="st_subheader-hover_with_help_and_anchor")
header = headers.nth(6)
link_container = header.get_by_test_id("stHeaderActionElements").locator("a")
expect(link_container).not_to_be_attached()
assert_snapshot(header, name="st_subheader-hover_with_help_and_hidden_anchor")
def test_links_are_rendered_correctly_snapshot(
themed_app: Page, assert_snapshot: ImageCompareFunction
):
wait_for_app_loaded(themed_app)
link = themed_app.get_by_text("a link")
link.scroll_into_view_if_needed()
expect(link).to_have_count(1)
expect(link).to_be_visible()
assert_snapshot(link, name="st_header-title_with_link")
_number_of_colors = 8
@pytest.mark.parametrize("color_index", range(_number_of_colors))
def test_header_divider_snapshot(
app: Page, assert_snapshot: ImageCompareFunction, color_index: int
):
"""Test that st.header renders correctly with dividers."""
header_divider_elements = _get_header_elements(app).filter(
has_text=_header_divider_filter_text
)
expect(header_divider_elements).to_have_count(_number_of_colors)
header_divider_element = header_divider_elements.nth(color_index)
header_divider_element.scroll_into_view_if_needed()
assert_snapshot(
header_divider_element,
name=f"st_header-divider_{color_index}",
)
@pytest.mark.parametrize("color_index", range(_number_of_colors))
def test_subheader_divider_snapshot(
app: Page, assert_snapshot: ImageCompareFunction, color_index: int
):
"""Test that st.subheader renders correctly with dividers."""
subheader_divider_elements = _get_subheader_elements(app).filter(
has_text=_subheader_divider_filter_text
)
expect(subheader_divider_elements).to_have_count(_number_of_colors)
subheader_divider_element = subheader_divider_elements.nth(color_index)
subheader_divider_element.scroll_into_view_if_needed()
assert_snapshot(
subheader_divider_element,
name=f"st_subheader-divider_{color_index}",
)
def test_help_tooltip_works(app: Page):
"""Test that the help tooltip is displayed on hover."""
header_with_help = _get_header_elements(app).nth(3)
tooltip_text = "Some help tooltip"
expect_help_tooltip(app, header_with_help, tooltip_text)
subheader_with_help = _get_subheader_elements(app).nth(5)
expect_help_tooltip(app, subheader_with_help, tooltip_text)
title_with_help = _get_title_elements(app).nth(1)
expect_help_tooltip(app, title_with_help, tooltip_text)
def test_not_scrolled_on_empty_anchor_tag(app: Page):
"""Test that the page is not scrolled when the page contains an empty
header/anchor tag and no window hash."""
# Check if the page is still scrolled to the top
# after one second timeout.
app.wait_for_timeout(1000)
scroll_position = app.evaluate("window.scrollY")
# Usage of assert is fine here since we just need to verify that
# this is still scrolled to top, no need to wait for this to happen.
assert scroll_position == 0
def test_check_top_level_class(app: Page):
"""Check that the top level class is correctly set."""
check_top_level_class(app, "stHeading")