-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathtest_pages_layout.py
181 lines (151 loc) · 6.78 KB
/
test_pages_layout.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
import pytest
import dash
from dash import Dash, dcc, html
from dash.exceptions import NoLayoutException
def get_app(path1="/", path2="/layout2"):
app = Dash(__name__, use_pages=True)
# test for storing arbitrary keyword arguments: An `id` prop is defined for every page
# test for defining multiple pages within a single file: layout is passed directly to `register_page`
# in the following two modules:
dash.register_page(
"multi_layout1",
layout=html.Div("text for multi_layout1", id="text_multi_layout1"),
path=path1,
title="Supplied Title",
description="This is the supplied description",
name="Supplied name",
image="birds.jpeg",
id="multi_layout1",
)
dash.register_page(
"multi_layout2",
layout=html.Div("text for multi_layout2", id="text_multi_layout2"),
path=path2,
id="multi_layout2",
)
app.layout = html.Div(
[
html.Div(
[
html.Div(
dcc.Link(
f"{page['name']} - {page['path']}",
id=page["id"],
href=page["path"],
)
)
for page in dash.page_registry.values()
]
),
dash.page_container,
dcc.Location(id="url", refresh=True),
]
)
return app
def test_pala001_layout(dash_duo):
dash_duo.start_server(get_app())
# test layout and title for each page in `page_registry` with link navigation
for page in dash.page_registry.values():
dash_duo.find_element("#" + page["id"]).click()
dash_duo.wait_for_text_to_equal("#text_" + page["id"], "text for " + page["id"])
assert dash_duo.driver.title == page["title"], "check that page title updates"
# test redirects
dash_duo.wait_for_page(url=f"http://localhost:{dash_duo.server.port}/v2")
dash_duo.wait_for_text_to_equal("#text_redirect", "text for redirect")
dash_duo.wait_for_page(url=f"http://localhost:{dash_duo.server.port}/old-home-page")
dash_duo.wait_for_text_to_equal("#text_redirect", "text for redirect")
assert (
dash_duo.driver.current_url
== f"http://localhost:{dash_duo.server.port}/redirect"
)
# test redirect with button and user defined dcc.Location
# note: dcc.Location must be defined in app.py
dash_duo.wait_for_page(url=f"http://localhost:{dash_duo.server.port}/page1")
dash_duo.find_element("#btn1").click()
dash_duo.wait_for_text_to_equal("#text_page2", "text for page2")
# test query strings
dash_duo.wait_for_page(
url=f"http://localhost:{dash_duo.server.port}/query-string?velocity=10"
)
assert (
dash_duo.find_element("#velocity").get_attribute("value") == "10"
), "query string passed to layout"
# test path variables
dash_duo.wait_for_page(url=f"http://localhost:{dash_duo.server.port}/a/none/b/none")
dash_duo.wait_for_text_to_equal("#path_vars", "variables from pathname:none none")
dash_duo.wait_for_page(url=f"http://localhost:{dash_duo.server.port}/a/var1/b/var2")
dash_duo.wait_for_text_to_equal("#path_vars", "variables from pathname:var1 var2")
# test page not found
dash_duo.wait_for_page(url=f"http://localhost:{dash_duo.server.port}/find_me")
dash_duo.wait_for_text_to_equal("#text_not_found_404", "text for not_found_404")
assert dash_duo.get_logs() == [], "browser console should contain no error"
# dash_duo.percy_snapshot("pala001_layout")
def check_metas(dash_duo, metas):
meta = dash_duo.find_elements("meta")
# -3 for the meta charset and http-equiv and viewport.
assert len(meta) == len(metas) + 3, "Should have extra meta tags"
assert meta[0].get_attribute("name") == metas[0]["name"]
assert meta[0].get_attribute("content") == metas[0]["content"]
for i in range(1, len(meta) - 3):
assert meta[i].get_attribute("property") == metas[i]["property"]
assert meta[i].get_attribute("content") == metas[i]["content"]
def test_pala002_meta_tags_default(dash_duo):
dash_duo.start_server(get_app(path1="/layout1", path2="/"))
# These are the inferred defaults if description, title, image are not supplied
metas_layout2 = [
{"name": "description", "content": ""},
{"property": "twitter:card", "content": "summary_large_image"},
{
"property": "twitter:url",
"content": f"http://localhost:{dash_duo.server.port}/",
},
{"property": "twitter:title", "content": "Multi layout2"},
{"property": "twitter:description", "content": ""},
{
"property": "twitter:image",
"content": f"http://localhost:{dash_duo.server.port}/assets/app.jpeg",
},
{"property": "og:title", "content": "Multi layout2"},
{"property": "og:type", "content": "website"},
{"property": "og:description", "content": ""},
{
"property": "og:image",
"content": f"http://localhost:{dash_duo.server.port}/assets/app.jpeg",
},
]
check_metas(dash_duo, metas_layout2)
def test_pala003_meta_tags_custom(dash_duo):
dash_duo.start_server(get_app())
# In the "multi_layout1" module, the description, title, image are supplied
metas_layout1 = [
{"name": "description", "content": "This is the supplied description"},
{"property": "twitter:card", "content": "summary_large_image"},
{
"property": "twitter:url",
"content": f"http://localhost:{dash_duo.server.port}/",
},
{"property": "twitter:title", "content": "Supplied Title"},
{
"property": "twitter:description",
"content": "This is the supplied description",
},
{
"property": "twitter:image",
"content": f"http://localhost:{dash_duo.server.port}/assets/birds.jpeg",
},
{"property": "og:title", "content": "Supplied Title"},
{"property": "og:type", "content": "website"},
{"property": "og:description", "content": "This is the supplied description"},
{
"property": "og:image",
"content": f"http://localhost:{dash_duo.server.port}/assets/birds.jpeg",
},
]
check_metas(dash_duo, metas_layout1)
def test_pala004_no_layout_exception():
error_msg = 'No layout found in module pages_error.no_layout_page\nA variable or a function named "layout" is required.'
with pytest.raises(NoLayoutException) as err:
Dash(__name__, use_pages=True, pages_folder="pages_error")
# clean up after this test, so the broken entry doesn't affect other pages tests
del dash.page_registry["pages_error.no_layout_page"]
assert error_msg in err.value.args[0]