Skip to content

Commit 13963bf

Browse files
committed
Add tests for dragging
1 parent e21503e commit 13963bf

File tree

2 files changed

+61
-22
lines changed

2 files changed

+61
-22
lines changed

tests/test_interactions.py

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import json
2626

2727
from selenium.webdriver.common.action_chains import ActionChains
28+
from selenium.webdriver.common.by import By
2829

2930

3031
def create_app(dash_duo):
@@ -73,27 +74,30 @@ def perform_dragging(
7374
"""
7475
actions.reset_actions()
7576
actions.move_to_element_with_offset(
76-
dash_duo.driver.find_element_by_tag_name("body"), x, y
77+
dash_duo.driver.find_element(By.TAG_NAME, "body"), x, y
7778
)
7879
actions.drag_and_drop_by_offset(source=None, xoffset=delta_x, yoffset=delta_y)
79-
actions.click()
8080
actions.perform()
8181
time.sleep(1)
8282

8383
elem_json = json.loads(elem.text)
84-
new_pos = elem_json.get("renderedPosition")
85-
clicked_label = elem_json.get("data", {}).get("label")
84+
new_pos = (
85+
elem_json[0].get("position")
86+
if "position" in elem_json[0].keys()
87+
else elem_json[0].get("renderedPosition")
88+
)
89+
dragged_label = elem_json[0].get("data", {}).get("label")
8690

87-
diff_x = round(new_pos["x"] - x)
88-
diff_y = round(new_pos["y"] - y)
91+
node_x = round(new_pos["x"])
92+
node_y = round(new_pos["y"])
8993

9094
save_screenshot(
9195
dash_duo,
9296
dir_name,
93-
f"Dragged{clicked_label.replace(' ', '')}By{diff_x}x{diff_y}y",
97+
f"Dragged{dragged_label.replace(' ', '')}By{node_x}x{node_y}y",
9498
)
9599

96-
return diff_x, diff_y
100+
return node_x, node_y
97101

98102

99103
def perform_clicking(dash_duo, x, y, elem, actions, dir_name="interactions"):
@@ -106,7 +110,7 @@ def perform_clicking(dash_duo, x, y, elem, actions, dir_name="interactions"):
106110
"""
107111
actions.reset_actions()
108112
actions.move_to_element_with_offset(
109-
dash_duo.driver.find_element_by_tag_name("body"), x, y
113+
dash_duo.driver.find_element(By.TAG_NAME, "body"), x, y
110114
)
111115
actions.click()
112116
actions.perform()
@@ -124,7 +128,7 @@ def perform_mouseover(
124128
):
125129
actions.reset_actions()
126130
actions.move_to_element_with_offset(
127-
dash_duo.driver.find_element_by_tag_name("body"), x - 50, y
131+
dash_duo.driver.find_element(By.TAG_NAME, "body"), x - 50, y
128132
)
129133
actions.move_by_offset(50, 0)
130134
actions.perform()
@@ -148,26 +152,38 @@ def test_cyin001_dragging(dash_duo):
148152
# View module docstring for more information about initial positions
149153
init_x, init_y = init_pos["Node 1"]
150154

155+
# Open the Drag data JSON tab
156+
actions.move_to_element(dash_duo.find_element("#tabs > div:nth-child(5)"))
157+
actions.click().perform()
158+
time.sleep(1)
159+
151160
# Select the JSON output element
152-
elem_tap = dash_duo.find_element("pre#tap-node-json-output")
161+
elem_tap = dash_duo.find_element("pre#elements-data-json-output")
153162

154-
# Test dragging the nodes around
155-
offset_x, offset_y = perform_dragging(
156-
dash_duo, init_x, init_y, 0, 0, elem_tap, actions
163+
# Get initial positions. Not actualy dragging
164+
init_node_x, init_node_y = perform_dragging(
165+
dash_duo, init_x, init_y, 1, 1, elem_tap, actions
157166
)
158-
init_x += offset_x
159-
init_y += offset_y
160167

168+
pixels_to_position_conv_factor = 1280 * 0.00085
169+
# Test dragging the nodes around
161170
assert perform_dragging(dash_duo, init_x, init_y, 150, 0, elem_tap, actions) == (
162-
150,
163-
0,
164-
), drag_error
171+
round(150 / pixels_to_position_conv_factor) + init_node_x,
172+
0 + init_node_y,
173+
)
165174
assert perform_dragging(
166175
dash_duo, init_x + 150, init_y, 0, 150, elem_tap, actions
167-
) == (0, 150), drag_error
176+
) == (
177+
round(150 / pixels_to_position_conv_factor) + init_node_x,
178+
round(150 / pixels_to_position_conv_factor) + init_node_y,
179+
)
168180
assert perform_dragging(
169181
dash_duo, init_x + 150, init_y + 150, -150, -150, elem_tap, actions
170-
) == (-150, -150), drag_error
182+
) == (init_node_x, init_node_y)
183+
assert perform_dragging(dash_duo, init_x, init_y, 100, -100, elem_tap, actions) == (
184+
round(100 / pixels_to_position_conv_factor) + init_node_x,
185+
round(-100 / pixels_to_position_conv_factor) + init_node_y,
186+
)
171187

172188

173189
def test_cyin002_clicking(dash_duo):
@@ -263,7 +279,7 @@ def test_cyin005_click_twice(dash_duo):
263279

264280
actions.reset_actions()
265281
actions.move_to_element_with_offset(
266-
dash_duo.driver.find_element_by_tag_name("body"), x, y
282+
dash_duo.driver.find_element(By.TAG_NAME, "body"), x, y
267283
)
268284
actions.click()
269285
actions.perform()

usage-events.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,21 @@
206206
)
207207
],
208208
),
209+
dcc.Tab(
210+
label="Drag Data",
211+
children=[
212+
html.Div(
213+
style=styles["tab"],
214+
children=[
215+
html.P("Elements Data JSON:"),
216+
html.Pre(
217+
id="elements-data-json-output",
218+
style=styles["json-output"],
219+
),
220+
],
221+
)
222+
],
223+
),
209224
],
210225
),
211226
],
@@ -271,5 +286,13 @@ def displaySelectedEdgeData(data):
271286
return json.dumps(data, indent=2)
272287

273288

289+
@callback(
290+
Output("elements-data-json-output", "children"),
291+
Input("cytoscape", "elements"),
292+
)
293+
def displayElementsData(data):
294+
return json.dumps(data, indent=2)
295+
296+
274297
if __name__ == "__main__":
275298
app.run_server(debug=True)

0 commit comments

Comments
 (0)