Skip to content

Commit ca54a1e

Browse files
author
Xing Han Lu
committed
Create test_interactions.py
1 parent 586278b commit ca54a1e

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed

tests/test_interactions.py

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import os
2+
import importlib
3+
import time
4+
import json
5+
6+
from .IntegrationTests import IntegrationTests
7+
from selenium.webdriver.common.by import By
8+
from selenium.webdriver.support.ui import WebDriverWait
9+
from selenium.webdriver.support import expected_conditions as EC
10+
from selenium.webdriver.common.action_chains import ActionChains
11+
12+
13+
class Tests(IntegrationTests):
14+
def test_interactions(self):
15+
app = importlib.import_module('usage-events').app
16+
self.startServer(app)
17+
WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.ID, "cytoscape")))
18+
19+
actions = ActionChains(self.driver)
20+
21+
def save_screenshot(dir_name, name):
22+
directory_path = os.path.join(
23+
os.path.dirname(__file__),
24+
'screenshots',
25+
dir_name
26+
)
27+
28+
# Create directory if it doesn't already exist
29+
if not os.path.exists(directory_path):
30+
os.makedirs(directory_path)
31+
32+
self.driver.save_screenshot(os.path.join(
33+
os.path.dirname(__file__),
34+
'screenshots',
35+
dir_name,
36+
name + '.png'
37+
))
38+
39+
def perform_dragging(x, y, delta_x, delta_y, elem, dir_name='interactions'):
40+
"""
41+
Performs dragging on a node, and return the difference from the start
42+
:param x: initial position of the node at the start of the action chain
43+
:param y: initial position of the node at the start of the action chain
44+
:param delta_x: how much we want to drag the node
45+
:param delta_y: how much we want to drag the node
46+
:param dir_name: The directory in which we store our screenshots
47+
:return: the difference between the position after drag and the starting position
48+
"""
49+
actions.reset_actions()
50+
actions.move_to_element_with_offset(
51+
self.driver.find_element_by_tag_name('body'), x, y
52+
)
53+
actions.drag_and_drop_by_offset(source=None, xoffset=delta_x, yoffset=delta_y)
54+
actions.click()
55+
actions.perform()
56+
time.sleep(1)
57+
58+
elem_json = json.loads(elem.text)
59+
new_pos = elem_json.get('renderedPosition')
60+
clicked_label = elem_json.get('data', {}).get('label')
61+
62+
diff_x = round(new_pos['x'] - x)
63+
diff_y = round(new_pos['y'] - y)
64+
65+
save_screenshot(
66+
dir_name,
67+
'Dragged{}By{}x{}y'.format(clicked_label.replace(' ', ''), diff_x, diff_y)
68+
)
69+
70+
return diff_x, diff_y
71+
72+
def perform_clicking(x, y, elem, dir_name='interactions'):
73+
"""
74+
:param x: The position on the screen where we want to click
75+
:param y: The position on the screen where we want to click
76+
:param elem: The element object from where we retrieve the JSON
77+
:param dir_name: The directory in which we store our screenshots
78+
:return: The label of element most recently clicked, if any
79+
"""
80+
actions.reset_actions()
81+
actions.move_to_element_with_offset(
82+
self.driver.find_element_by_tag_name('body'), x, y
83+
)
84+
actions.click()
85+
actions.perform()
86+
87+
time.sleep(1)
88+
clicked_label = json.loads(elem.text).get('data', {}).get('label')
89+
90+
save_screenshot(dir_name, 'Clicked' + clicked_label.replace(' ', ''))
91+
92+
return clicked_label
93+
94+
def perform_mouseover(x, y, elem, dir_name='interactions'):
95+
actions.reset_actions()
96+
actions.move_to_element_with_offset(
97+
self.driver.find_element_by_tag_name('body'), x - 100, y
98+
)
99+
actions.move_by_offset(100, 0)
100+
actions.perform()
101+
time.sleep(1)
102+
103+
mouseover_label = json.loads(elem.text).get('label')
104+
105+
save_screenshot(dir_name, 'Mouseover' + mouseover_label.replace(' ', ''))
106+
107+
return mouseover_label
108+
109+
drag_error = "Unable to drag Cytoscape nodes properly"
110+
click_error = "Unable to click Cytoscape nodes properly"
111+
mouseover_error = "Unable to mouseover Cytoscape nodes properly"
112+
113+
init_pos = {
114+
'Node 1': (80.94611044209678, 333.54879281525285),
115+
'Node 2': (375.64032747402433, 628.2430098471805),
116+
'Node 3': (277.40892179671516, 514.2945792615018 - 20),
117+
'Node 4': (768.5659501832611, 333.54879281525285),
118+
'Node 5': (473.8717331513335, 431.780198492562),
119+
'Node 6': (277.40892179671516, 530.0116041698712)
120+
}
121+
init_x, init_y = init_pos['Node 1']
122+
# Select the JSON output element
123+
elem_tap = self.driver.find_element_by_css_selector('pre#tap-node-json-output')
124+
125+
# # Test dragging the nodes around
126+
offset_x, offset_y = perform_dragging(init_x, init_y, 0, 0, elem_tap)
127+
init_x += offset_x
128+
init_y += offset_y
129+
130+
assert perform_dragging(init_x, init_y, 150, 0, elem_tap) == (150, 0), drag_error
131+
assert perform_dragging(init_x+150, init_y, 0, 150, elem_tap) == (0, 150), drag_error
132+
assert perform_dragging(init_x+150, init_y+150, -150, -150, elem_tap) == (-150, -150), \
133+
drag_error
134+
135+
# Test clicking the nodes
136+
for i in range(1, 7):
137+
label = 'Node {}'.format(i)
138+
assert perform_clicking(*init_pos[label], elem_tap) == label, click_error
139+
140+
# Open the Mouseover JSON tab
141+
actions.move_to_element(
142+
self.driver.find_element_by_css_selector('#tabs > div:nth-child(3)'))
143+
actions.click().perform()
144+
time.sleep(1)
145+
146+
# Select the JSON output element
147+
elem_mouseover = self.driver.find_element_by_css_selector(
148+
'pre#mouseover-node-data-json-output')
149+
150+
# Test hovering the nodes
151+
for i in range(1, 7):
152+
label = 'Node {}'.format(i)
153+
assert perform_mouseover(*init_pos[label], elem_mouseover) == label, mouseover_error

0 commit comments

Comments
 (0)