forked from streamlit/streamlit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathst_graphviz_chart_test.py
132 lines (93 loc) · 4.38 KB
/
st_graphviz_chart_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
# 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, wait_until
from e2e_playwright.shared.app_utils import check_top_level_class
def get_first_graph_svg(app: Page):
return app.get_by_test_id("stGraphVizChart").nth(0).locator("svg")
def click_fullscreen(app: Page):
app.get_by_test_id("StyledFullScreenButton").nth(0).click()
# Wait for the animation to finish
app.wait_for_timeout(1000)
def test_initial_setup(app: Page):
"""Initial setup: ensure charts are loaded."""
expect(
app.get_by_test_id("stGraphVizChart").locator("svg > g > title")
).to_have_count(6)
def test_shows_left_and_right_graph(app: Page):
"""Test if it shows left and right graph."""
expect(app.locator(".stGraphVizChart > svg > g > title").nth(3)).to_have_text(
"Left"
)
expect(app.locator(".stGraphVizChart > svg > g > title").nth(4)).to_have_text(
"Right"
)
def test_first_graph_dimensions(app: Page):
"""Test the dimensions of the first graph."""
first_graph_svg = get_first_graph_svg(app)
expect(first_graph_svg).to_have_attribute("width", "79pt")
expect(first_graph_svg).to_have_attribute("height", "116pt")
def test_first_graph_fullscreen(app: Page, assert_snapshot: ImageCompareFunction):
"""Test if the first graph shows in fullscreen."""
# Hover over the parent div
app.get_by_test_id("stGraphVizChart").nth(0).hover()
# Enter fullscreen
click_fullscreen(app)
first_graph_svg = get_first_graph_svg(app)
# The width and height unset on the element on fullscreen
expect(first_graph_svg).not_to_have_attribute("width", "79pt")
expect(first_graph_svg).not_to_have_attribute("height", "116pt")
def check_dimensions():
svg_dimensions = first_graph_svg.bounding_box()
return svg_dimensions["width"] == 1256 and svg_dimensions["height"] == 662
wait_until(app, check_dimensions)
assert_snapshot(first_graph_svg, name="st_graphviz-fullscreen")
def test_first_graph_after_exit_fullscreen(
app: Page, assert_snapshot: ImageCompareFunction
):
"""Test if the first graph has correct size after exiting fullscreen."""
# Hover over the parent div
app.get_by_test_id("stGraphVizChart").nth(0).hover()
# Enter and exit fullscreen
click_fullscreen(app)
click_fullscreen(app)
first_graph_svg = get_first_graph_svg(app)
expect(first_graph_svg).to_have_attribute("width", "79pt")
expect(first_graph_svg).to_have_attribute("height", "116pt")
assert_snapshot(first_graph_svg, name="st_graphviz-after_exit_fullscreen")
def test_renders_with_specified_engines(
app: Page, assert_snapshot: ImageCompareFunction
):
"""Test if it renders with specified engines."""
engines = ["dot", "neato", "twopi", "circo", "fdp", "osage", "patchwork"]
radios = app.query_selector_all('label[data-baseweb="radio"]')
for idx, engine in enumerate(engines):
radios[idx].click(force=True)
wait_for_app_run(app)
expect(app.get_by_test_id("stMarkdown").nth(0)).to_have_text(engine)
assert_snapshot(
app.get_by_test_id("stGraphVizChart").nth(2).locator("svg"),
name=f"st_graphviz_chart_engine-{engine}",
)
def test_dot_string(app: Page, assert_snapshot: ImageCompareFunction):
"""Test if it renders charts when input is a string (dot language)."""
title = app.locator(".stGraphVizChart > svg > g > title").nth(5)
expect(title).to_have_text("Dot")
assert_snapshot(
app.get_by_test_id("stGraphVizChart").nth(5).locator("svg"),
name="st_graphviz-chart_dot_string",
)
def test_check_top_level_class(app: Page):
"""Check that the top level class is correctly set."""
check_top_level_class(app, "stGraphVizChart")