-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsite_element.py
409 lines (354 loc) · 13.8 KB
/
site_element.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
""" SiteElement class improves organization of site element
parameters. The class methods faciltate easy manipulation
of these elements
"""
import platform
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
class SiteElement:
"""Defines site elements in a structured way and provides a convenient
means for element manipulations (clicking, entering text, etc.)
"""
def __init__(self, by, locator):
self.by = by
self.locator = locator
def loc_it(self, driver):
"""
Identifies element on page, based on an element locator.
Waits until an element becomes available & visible in DOM, and
then until it becomes clickable.
"""
wait = WebDriverWait(driver, 10)
try:
wait.until(EC.visibility_of_element_located((self.by, self.locator)))
target_el = wait.until(EC.element_to_be_clickable((self.by, self.locator)))
except TimeoutException as e:
raise e
return target_el
def loc_hidden(self, driver, silent=False):
"""
Identifies potentially hidden element on page, based on an element locator.
"""
wait = WebDriverWait(driver, 10)
try:
target_el = wait.until(
EC.presence_of_element_located((self.by, self.locator))
)
except TimeoutException as e:
if not silent:
print(
"\nUnable to locate element by {}, "
"locator: '{}'".format(self.by, self.locator)
)
raise e
return target_el
def exists(self, driver):
"""
Checks if element is visible on the page.
"""
wait = WebDriverWait(driver, 3)
try:
wait.until(EC.visibility_of_element_located((self.by, self.locator)))
target_el = wait.until(EC.element_to_be_clickable((self.by, self.locator)))
return True
except TimeoutException:
return False
def exists_in_dom(self, driver):
"""
Checks if element exists within the DOM.
"""
wait = WebDriverWait(driver, 3)
try:
wait.until(EC.presence_of_element_located((self.by, self.locator)))
return True
except TimeoutException:
return False
def is_visible(self, driver):
"""
Checks if element is visible on the page.
"""
target_el = driver.find_element(self.by, self.locator)
return target_el.is_displayed()
def is_selected(self, driver):
"""
Checks if element is visible on the page.
"""
target_el = driver.find_element(self.by, self.locator)
return target_el.is_selected()
def click(self, driver):
"""Identifies an element on the page. After identification
the element is then clicked.
"""
target_el = self.loc_it(driver)
target_el.click()
def double_click(self, driver):
"""
Double click on element.
"""
target_el = self.loc_it(driver)
actionchains = ActionChains(driver)
actionchains.double_click(target_el).perform()
def javascript_click(self, driver):
"""
Clicks an element using JavaScript
"""
target_el = self.loc_it(driver)
driver.execute_script("arguments[0].click();", target_el)
def javascript_click_hidden(self, driver, silent=False):
"""
Simulate click on a hidden element using JavaScript
"""
target_el = self.loc_hidden(driver, silent)
driver.execute_script("arguments[0].click();", target_el)
def javascript_fill_hidden_text(self, driver, text):
"""
Set text using JavaScript for a potentially hidden element
"""
target_el = self.loc_hidden(driver)
driver.execute_script(f'arguments[0].value="{text}";', target_el)
def submit(self, driver):
"""Send ENTER to element, simulates submit"""
target_el = self.loc_it(driver)
target_el.send_keys(Keys.ENTER)
def submit_hidden(self, driver):
"""Send ENTER to element that is perhaps hidden,
simulates submit
"""
actions = ActionChains(driver)
actions.key_down(Keys.ENTER)
actions.key_up(Keys.ENTER)
actions.perform()
def multi_click(self, driver):
"""Clicks an element while holding the control key, as to enable
a multi-selection
"""
target_el = self.loc_it(driver)
actions = ActionChains(driver)
actions.move_to_element(target_el)
actions.key_down(Keys.LEFT_CONTROL)
actions.click(target_el)
actions.key_up(Keys.LEFT_CONTROL)
actions.perform()
def range_click(self, driver):
"""Clicks an element while holding the control key, as to enable
a range selection
"""
target_el = self.loc_it(driver)
actions = ActionChains(driver)
actions.move_to_element(target_el)
actions.key_down(Keys.LEFT_SHIFT)
actions.click(target_el)
actions.key_up(Keys.LEFT_SHIFT)
actions.perform()
def passive_click(self, driver):
"""Identifies an element on the page. After identification
the element is then clicked, regardless if it is "interactable"
or not
"""
target_el = self.loc_it(driver)
ActionChains(driver).move_to_element(target_el).click(target_el).perform()
def clear_all_text(self, driver):
"""Uses the Ctrl+A keys combination to select all text before using
BACKSPACE key to delete it
"""
target_el = self.loc_it(driver)
if platform.system() == "Darwin": # MacOs
ctrl_key = Keys.COMMAND
else:
ctrl_key = Keys.CONTROL
ActionChains(driver).move_to_element(target_el).key_down(ctrl_key).send_keys(
"a"
).key_up(ctrl_key).send_keys(Keys.BACKSPACE).perform()
def clear_text(self, driver, size):
"""Uses backspace to clear text from a field"""
target_el = self.loc_it(driver)
target_el.send_keys(Keys.END)
for i in range(0, size):
target_el.send_keys(Keys.BACK_SPACE)
def select_option(self, driver, select_choice):
"""Selects an option from a dropdown element"""
target_el = self.loc_it(driver)
select_el = Select(target_el)
select_el.select_by_value(select_choice)
def select_option_text(self, driver, select_choice):
"""Selects an option from dropdown given visible text"""
target_el = self.loc_it(driver)
select_el = Select(target_el)
select_el.select_by_visible_text(select_choice)
def scroll_to_hidden(self, driver):
"""After element identification, the window is scrolled
such that the element becomes visible in the window
"""
target_el = self.loc_hidden(driver)
target_el.location_once_scrolled_into_view
def scroll_to(self, driver):
"""After element identification, the window is scrolled
such that the element becomes visible in the window
"""
target_el = self.loc_it(driver)
target_el.location_once_scrolled_into_view
def scroll_right(self, driver):
"""Scroll right using Keys.ARROW_RIGHT
and a hold of one second
"""
target_el = self.loc_it(driver)
actions = ActionChains(driver)
actions.move_to_element(target_el)
actions.key_down(Keys.ARROW_RIGHT)
actions.perform()
time.sleep(1)
actions = ActionChains(driver)
actions.key_up(Keys.ARROW_RIGHT)
actions.perform()
def inject_text(self, driver, field_text):
"""Enters text into a field or other input-capable html
element using send keys
"""
target_el = self.loc_it(driver)
for i in range(0, len(field_text)):
target_el.send_keys(field_text[i])
def hidden_inject_text(self, driver, field_text):
"""Enters text into a field or other input-capable html
element that is hidden using send keys
"""
target_el = self.loc_hidden(driver)
for i in range(0, len(field_text)):
target_el.send_keys(field_text[i])
def send_caps(self, driver, field_text):
"""Enters capitalized text into a field or other input-capable
html element using send_keys_to_element
"""
actionchains = ActionChains(driver)
target_el = self.loc_it(driver)
actionchains.key_down(Keys.SHIFT).send_keys_to_element(
target_el, field_text
).key_up(Keys.SHIFT).perform()
def set_path(self, driver, field_text):
"""Enters text into a field or other input-capable html
element using send keys, best for setting path to files for upload
"""
target_el = self.loc_it(driver)
target_el.send_keys(field_text)
def iframe_in(self, driver):
"""Switches driver focus to an iframe within a page"""
target_el = self.loc_it(driver)
driver.switch_to.frame(target_el)
def iframe_out(self, driver):
"""Switches driver focus out of iframe and back to the
main page
"""
driver.switch_to.parent_frame()
def get_attribute(self, driver, attribute):
"""Returns any attribute of website element"""
target_el = self.loc_it(driver)
return target_el.get_attribute(attribute)
def get_attribute_hidden(self, driver, attribute):
"""Returns any attribute of website element"""
target_el = self.loc_hidden(driver)
return target_el.get_attribute(attribute)
def get_text(self, driver):
"""Returns content text of website element"""
target_el = self.loc_it(driver)
return target_el.text
def get_value(self, driver):
"""Returns content text of website element"""
target_el = self.loc_it(driver)
return target_el.get_attribute("value")
def get_href(self, driver, base_url=None):
"""Returns element href link, with relative links expanded
into an absolute link
"""
target_el = self.loc_it(driver)
target_href = target_el.get_attribute("href")
if target_href[0] == "/":
target_href = base_url + target_href
return target_href
def get_bag_url(self, driver, base_url=None):
"""Returns element href link, with relative links expanded
into an absolute link
"""
target_el = self.loc_it(driver)
target_href = target_el.get_attribute("data-bag-url")
if target_href[0] == "/":
target_href = base_url + target_href
return target_href
def get_child_count(self, driver):
"""Returns the number of child elements, given a parent
element specification
"""
target_el = self.loc_it(driver)
return len(target_el.find_elements_by_xpath(".//*"))
def get_relatives_by_xpath(self, driver, xpath):
"""Returns the relatives by xpath, given a parent
element specification
"""
target_el = self.loc_hidden(driver)
return target_el.find_elements_by_xpath(xpath)
def get_texts_from_xpath(self, driver, xpath):
"""Returns the text in relatives matching xpath, given a parent
element specification
"""
web_elements = self.get_relatives_by_xpath(driver, xpath)
keywords = []
for el in web_elements:
keywords.append(el.text)
return keywords
def get_parent(self, driver):
"""Returns the parent element"""
target_el = self.loc_hidden(driver)
return target_el.find_element_by_xpath("..")
def get_immediate_child_count(self, driver):
"""Returns the number of immediate child elements, given a parent
element specification
"""
target_el = self.loc_it(driver)
return len(target_el.find_elements(By.XPATH, "./*"))
def get_class(self, driver):
target_el = self.loc_it(driver)
target_class = target_el.get_attribute("class")
return target_class
def get_style(self, driver):
target_el = self.loc_it(driver)
target_style = target_el.get_attribute("style")
return target_style
def wait_on_visibility(self, driver, max_time):
locator = self.by, self.locator
WebDriverWait(driver, max_time).until(EC.visibility_of_element_located(locator))
def right_click(self, driver):
target_el = self.loc_it(driver)
actions = ActionChains(driver)
actions.context_click(target_el)
actions.perform()
class SiteElementsCollection:
"""
Provides a way to locate all page elements which are identified by a
common locator.
"""
def __init__(self, by, locator):
self.by = by
self.locator = locator
def loc_them(self, driver):
"""
Finds all elements on a page that match a given locator.
Waits until all elements become visible in a DOM.
"""
wait = WebDriverWait(driver, 30)
try:
elements = wait.until(
EC.visibility_of_all_elements_located((self.by, self.locator))
)
except TimeoutException as e:
print(
"\nUnable to locate elements by {}, "
"locator: '{}'".format(self.by, self.locator)
)
raise e
return elements
def items(self, driver):
return self.loc_them(driver)