-
Notifications
You must be signed in to change notification settings - Fork 18
/
_webdriver_utils.py
executable file
·212 lines (167 loc) · 7.03 KB
/
_webdriver_utils.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python
# (c) Copyright 2015 by James Stout
# Licensed under the LGPL, see <http://www.gnu.org/licenses/>
"""Actions for manipulating Chrome via WebDriver."""
import json
import semantic_locators
from six.moves import urllib_request
from six.moves import urllib_error
from dragonfly import (DynStrActionBase)
import _dragonfly_local as local
# Needed for marionette_driver.
import sys
has_argv = hasattr(sys, "argv")
if not has_argv:
sys.argv = [""]
# Native driver for Firefox; more reliable.
import marionette_driver
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
class MarionetteWrapper(object):
"""Wraps a Marionette driver instance to make it compatible with the WebDriver API."""
def __init__(self, delegate):
self.delegate = delegate
def __getattr__(self, name):
try:
return super().__getattr__(self, name)
except AttributeError:
return getattr(self.delegate, name)
def execute_script(self, script, *args):
return self.delegate.execute_script(script, args, new_sandbox=False)
def create_driver():
global driver, browser
driver = None
browser = local.DEFAULT_BROWSER
if browser == "chrome":
try:
urllib_request.urlopen("http://127.0.0.1:9222/json")
except urllib_error.URLError:
print("Unable to start WebDriver, Chrome is not responding.")
return
chrome_options = webdriver.chrome.options.Options()
chrome_options.experimental_options["debuggerAddress"] = "127.0.0.1:9222"
driver = webdriver.Chrome(local.CHROME_DRIVER_PATH, chrome_options=chrome_options)
elif browser == "firefox":
driver = MarionetteWrapper(marionette_driver.marionette.Marionette())
else:
print("Unknown browser: " + browser)
browser = None
def quit_driver():
global driver, browser
if driver:
if browser == "chrome":
driver.quit()
elif browser == "firefox":
driver.delete_session()
driver = None
browser = None
def switch_to_active_tab():
if browser == "chrome":
# driver.switch_to.window("main")
tabs = json.load(urllib_request.urlopen("http://127.0.0.1:9222/json"))
# Chrome seems to order the tabs by when they were last updated, so we find
# the first one that is not an extension.
for tab in tabs:
if not tab["url"].startswith("chrome-extension://"):
active_tab = tab["id"]
break
for window in driver.window_handles:
# ChromeDriver adds to the raw ID, so we just look for substring match.
if active_tab in window:
driver.switch_to_window(window);
print("Switched Chrome to: " + driver.title.encode('ascii', 'backslashreplace').decode())
return
print("Did not find active tab in Chrome.")
elif browser == "firefox":
driver.delete_session()
driver.start_session()
def test_driver():
switch_to_active_tab()
driver.get('http://www.google.com/xhtml');
def find_nearest_element(get_elements_function, tracker):
# Get gaze location as early as possible.
gaze_location = tracker.get_gaze_point_or_default()
switch_to_active_tab()
elements = get_elements_function()
if not elements:
print("No matching elements found")
return
# Assume there is equal amount of browser chrome on the left and right sides of the screen.
canvas_left = driver.execute_script("return window.screenX + (window.outerWidth - window.innerWidth) / 2 - window.scrollX;")
# Assume all the browser chrome is on the top of the screen and none on the bottom.
canvas_top = driver.execute_script("return window.screenY + (window.outerHeight - window.innerHeight) - window.scrollY;")
canvas_bottom = driver.execute_script("return window.screenY + window.outerHeight - window.scrollY;")
nearest_element = None
nearest_element_distance_squared = float("inf")
for element in elements:
element_location = (element.rect["x"] + canvas_left + element.rect["width"] / 2,
element.rect["y"] + canvas_top + element.rect["height"] / 2)
if not element.is_displayed():
# Element is not visible onscreen.
continue
offsets = (element_location[0] - gaze_location[0], element_location[1] - gaze_location[1])
distance_squared = offsets[0] * offsets[0] + offsets[1] * offsets[1]
if distance_squared < nearest_element_distance_squared:
nearest_element = element
nearest_element_distance_squared = distance_squared
if not nearest_element:
print("Matching elements were not visible")
return
return nearest_element
def find_clickable_elements_by_name(name):
elements = []
for role in ("button", "tab"):
for name_candidate in (name, name.capitalize()):
elements.extend(semantic_locators.find_elements_by_semantic_locator(
driver, "{{{} '*{}*'}}".format(role, name_candidate)))
return elements
def click_element(element):
if browser == "chrome":
try:
element.click()
except:
# Move then click to avoid Chrome unwillingness to send a click
# which reaches an overlapping element.
ActionChains(driver).move_to_element(element).click().perform()
elif browser == "firefox":
element.click()
def double_click_element(element):
if browser == "chrome":
try:
element.double_click()
except:
# Move then click to avoid Chrome unwillingness to send a click
# which reaches an overlapping element.
ActionChains(driver).move_to_element(element).double_click().perform()
elif browser == "firefox":
element.double_click()
class ElementAction(DynStrActionBase):
def __init__(self, by, spec):
DynStrActionBase.__init__(self, spec)
self.by = by
def _parse_spec(self, spec):
return spec
def _execute_events(self, events):
switch_to_active_tab()
element = driver.find_element(self.by, events)
self._execute_on_element(element)
class ClickElementAction(ElementAction):
def _execute_on_element(self, element):
click_element(element)
class SmartElementAction(DynStrActionBase):
def __init__(self, by, spec, tracker):
DynStrActionBase.__init__(self, spec)
self.by = by
self.tracker = tracker
def _parse_spec(self, spec):
return spec
def _execute_events(self, events):
nearest_element = find_nearest_element(lambda: driver.find_elements(self.by, events), tracker)
if nearest_element:
self._execute_on_element(nearest_element)
class SmartClickElementAction(SmartElementAction):
def _execute_on_element(self, element):
click_element(element)
class DoubleClickElementAction(ElementAction):
def _execute_on_element(self, element):
double_click_element(element)