-
Notifications
You must be signed in to change notification settings - Fork 224
/
Copy pathtest_navlink.py
148 lines (127 loc) · 4.57 KB
/
test_navlink.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
from dash import Dash, dcc, html
from dash.dependencies import Input, Output
from dash_bootstrap_components import NavLink
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
def test_dbnl001_auto_active(dash_duo):
"""
Checks that NavLink is able to automatically set active status based on the
current location.
"""
app = Dash()
app.layout = html.Div(
[
# dcc.Location is required to fire events to History
dcc.Location(id="url"),
NavLink("Page 1", id="page-1-link", href="/page-1", active=True),
NavLink("Page 2", id="page-2-link", href="/page-2", active=False),
NavLink("Page 3", id="page-3-link", href="/page-3", active="partial"),
NavLink(
"Page 3 - extra",
id="page-3-extra-link",
href="/page-3/extra",
active="exact",
),
html.Div(id="content"),
]
)
dash_duo.start_server(app)
assert "active" in dash_duo.wait_for_element_by_id("page-1-link").get_attribute(
"class"
)
assert "active" not in dash_duo.wait_for_element_by_id("page-2-link").get_attribute(
"class"
)
assert "active" not in dash_duo.wait_for_element_by_id("page-3-link").get_attribute(
"class"
)
assert "active" not in dash_duo.wait_for_element_by_id(
"page-3-extra-link"
).get_attribute("class")
dash_duo.wait_for_element_by_id("page-3-link").click()
assert "active" in dash_duo.wait_for_element_by_id("page-1-link").get_attribute(
"class"
)
assert "active" not in dash_duo.wait_for_element_by_id("page-2-link").get_attribute(
"class"
)
assert "active" in dash_duo.wait_for_element_by_id("page-3-link").get_attribute(
"class"
)
assert "active" not in dash_duo.wait_for_element_by_id(
"page-3-extra-link"
).get_attribute("class")
dash_duo.wait_for_element_by_id("page-3-extra-link").click()
assert "active" in dash_duo.wait_for_element_by_id("page-1-link").get_attribute(
"class"
)
assert "active" not in dash_duo.wait_for_element_by_id("page-2-link").get_attribute(
"class"
)
assert "active" in dash_duo.wait_for_element_by_id("page-3-link").get_attribute(
"class"
)
assert "active" in dash_duo.wait_for_element_by_id(
"page-3-extra-link"
).get_attribute("class")
def test_dbnl_002_manual_active(dash_duo):
"""
Update active status using a callback.
"""
app = Dash()
app.layout = html.Div(
[
# dcc.Location is required to fire events to History
dcc.Location(id="url"),
NavLink("Page 1", id="page-1-link", href="/page-1"),
NavLink("Page 2", id="page-2-link", href="/page-2"),
NavLink("Page 3", id="page-3-link", href="/page-3"),
html.Div(id="content"),
]
)
@app.callback(
[Output("page-{}-link".format(i), "active") for i in range(1, 4)],
Input("url", "pathname"),
)
def set_active(pathname):
return [pathname == "/page-{}".format(i) for i in range(1, 4)]
dash_duo.start_server(app)
assert all(
[
"active"
not in dash_duo.wait_for_element_by_id(
"page-{}-link".format(i)
).get_attribute("class")
for i in range(1, 4)
]
)
dash_duo.wait_for_element_by_id("page-1-link").click()
# wait for callback to update page
WebDriverWait(dash_duo.driver, timeout=10).until(
lambda d: "active"
in d.find_element(By.ID, "page-1-link").get_attribute("class")
)
assert "active" in dash_duo.wait_for_element_by_id("page-1-link").get_attribute(
"class"
)
assert "active" not in dash_duo.wait_for_element_by_id("page-2-link").get_attribute(
"class"
)
assert "active" not in dash_duo.wait_for_element_by_id("page-3-link").get_attribute(
"class"
)
dash_duo.wait_for_element_by_id("page-3-link").click()
# wait for callback to update page
WebDriverWait(dash_duo.driver, timeout=10).until(
lambda d: "active"
not in d.find_element(By.ID, "page-1-link").get_attribute("class")
)
assert "active" not in dash_duo.wait_for_element_by_id("page-1-link").get_attribute(
"class"
)
assert "active" not in dash_duo.wait_for_element_by_id("page-2-link").get_attribute(
"class"
)
assert "active" in dash_duo.wait_for_element_by_id("page-3-link").get_attribute(
"class"
)