/
element_interaction.py
289 lines (246 loc) · 11.5 KB
/
element_interaction.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
from element_identification import ElementIdentification
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support.ui import Select
from selenium.selenium import selenium
import pickle
import time
import re
class ElementInteraction(ElementIdentification):
def __init__(self, driver):
self.driver = driver
self.ei = ElementIdentification(self.driver)
'''def __soft_assert_page_changed(self, preclick, postclick):
pickled_preclick = pickle.dumps(preclick)
pickled_postclick = pickle.dumps(postclick)
if pickled_preclick == pickled_postclick:
return True
else:
return False'''
def __error_check(self):
# Determine if page contains any common errors
# Check config for custom errors
pass
def __post_back_check(self, element):
match = re.search("(P|p)ost(\s|_)*(B|b)ack", element.get_attribute('outerHTML'))
if match:
return True
else:
return False
def ajax_wait(self, timeout=5000):
condition = "self.driver.get_current_window().jQuery.active == 0"
selenium.wait_for_condition(condition, timeout)
def __wait_for_post_back_to_complete(self, element, timeout=None):
# Init state
state = element.is_enabled()
if timeout is None:
timeout = 30
current_time = 0
# Continuously check state until timeout or ready
while state is False and current_time < timeout:
time.sleep(.2)
state = element.is_enabled()
current_time += .2
print(current_time)
if state is False and current_time == timeout:
raise Exception('Exceeded timeout waiting for element')
def change_window_connection(self):
# Select the last active window
self.driver.switch_to_window(self.driver.window_handles[-1])
def handle_alert(self, obj):
# Set parameters
alert_action = obj.get('alert_action')
if alert_action is None:
raise Exception('No child_window')
child_window = obj.get('child_window')
if child_window is None:
raise Exception('No child_window')
alert_type = obj.get('alert_type')
message_array = obj.get('message_array')
action = None
if alert_type is None:
# Standard 'Ok', 'Cancel' alert
if alert_action == 'accept':
Alert(self.driver).accept()
action = "Click accept button on alert"
elif alert_action == 'dismiss':
Alert(self.driver).dismiss()
action = "Click dismiss button on alert"
else:
raise Exception('Unknown generic alert type')
elif alert_type == 'auth':
# Authentication alert
if type(message_array) is 'list':
Alert(self.driver).authenticate(message_array[0], message_array[1])
elif type(message_array) is 'dict':
Alert(self.driver).authenticate(message_array.get('username', 'Username not found'),
message_array.get('password', 'Password not found'))
action = 'Entered username and password into the alert.'
elif alert_type == 'custom':
# Future home of customer alerts when they are needed.
print('No custom alerts exist at this time')
action = 'No custom alerts exist at this time'
# Determine if action changes browser count
if child_window:
self.change_window_connection()
print(action)
return action
def click_object(self, obj):
# Set local parameters
step = obj.get('order')
identifier_dict = obj.get('identifier')
if identifier_dict is None:
raise Exception('No identifier')
child_window = obj.get('child_window')
if child_window is None:
raise Exception('No child_window')
element = self.ei.uniquely_identify_element(identifier_dict)
# Report no element found
if element is None:
action = 'No element found'
return action
# Determine the human readable element name
if str(self.ei.get_element_type(element)) == 'checkbox' or str(self.ei.get_element_type(element)) == 'radio':
text_value = str(self.find_html_for(element.get_attribute('id')))
elif str(self.ei.get_element_type(element)) == 'submit':
text_value = element.get_attribute('value')
elif element.tag_name == 'img':
text_value = element.get_attribute('alt')
else:
text_value = element.get_attribute('innerText').strip()
if element is not None:
action = "Click the '" + text_value + "' " + str(self.ei.get_element_type(element))
# Prepare element for action
self.__wait_for_post_back_to_complete(element, obj.get('timeout'))
self.driver.execute_script('return arguments[0].scrollIntoView();', element)
self.driver.execute_script('window.scrollBy(0, -150);')
element.is_enabled()
element.click()
# Determine if action changes browser count
if child_window:
self.change_window_connection()
print(action)
return action
def enter_text(self, obj):
# Set local parameters
step = obj.get('order')
identifier_dict = obj.get('identifier')
if identifier_dict is None:
raise Exception('No identifier')
child_window = obj.get('child_window')
if child_window is None:
raise Exception('No child_window')
text_value = obj.get('text_value')
element = self.ei.uniquely_identify_element(identifier_dict)
# Report no element found
if element is None:
action = 'No element found'
return action
if element is not None:
# Determine if element is visible
action = "Enter '" + str(text_value) + "' in the '" + str(
self.find_html_for(element.get_attribute('id'))) + "' textbox"
# Prepare element for action
self.__wait_for_post_back_to_complete(element, obj.get('timeout'))
self.driver.execute_script('return arguments[0].scrollIntoView();', element)
self.driver.execute_script('window.scrollBy(0, -100);')
# Clear existing text and send new text
element.is_enabled()
element.clear()
element.send_keys(text_value)
# Determine if action changes browser count
if child_window:
self.change_window_connection()
print(action)
return action
def select_option(self, obj):
# Set local parameters
step = obj.get('order')
identifier_dict = obj.get('identifier')
if identifier_dict is None:
raise Exception('No identifier')
child_window = obj.get('child_window')
if child_window is None:
raise Exception('No child_window')
selection_value = obj.get('selection_value')
if selection_value is None:
raise Exception('No selection_value')
selection_type = obj.get('selection_type')
if selection_type is None:
raise Exception('No selection_type')
multiline = obj.get('multiline')
action = None
element = self.ei.uniquely_identify_element(identifier_dict)
# Report no element found
if element is None:
action = 'No element found'
return action
if element is not None:
# Prepare element for action
self.__wait_for_post_back_to_complete(element, obj.get('timeout'))
self.driver.execute_script('return arguments[0].scrollIntoView();', element)
self.driver.execute_script(' window.scrollBy(0, -100);')
selection = Select(element)
options = selection.options
acted_upon_options = self.__determine_acted_upon_option(options, selection_value, selection_type)
# Determine how we are interacting with the select
if selection_type.lower() == "index":
action = "Select '" + acted_upon_options + "' from the '" + str(
self.find_html_for(element.get_attribute('id'))) + "' dropdown"
selection.select_by_index(selection_value)
elif selection_type.lower() == "text":
action = "Select '" + acted_upon_options + "' from the '" + str(
self.find_html_for(element.get_attribute('id'))) + "' dropdown"
selection.select_by_visible_text(selection_value)
elif selection_type.lower() == "value":
action = "Select '" + acted_upon_options + "' from the '" + str(
self.find_html_for(element.get_attribute('id'))) + "' dropdown"
selection.select_by_value(selection_value)
elif selection_type.lower() == "deindex":
action = "Deselect '" + acted_upon_options + "' from the '" + str(
self.find_html_for(element.get_attribute('id'))) + "' dropdown"
selection.deselect_by_index(selection_value)
elif selection_type.lower() == "detext":
action = "Deselect '" + acted_upon_options + "' from the '" + str(
self.find_html_for(element.get_attribute('id'))) + "' dropdown"
selection.deselect_by_visible_text(selection_value)
elif selection_type.lower() == "devalue":
action = "Deselect '" + acted_upon_options + "' from the '" + str(
self.find_html_for(element.get_attribute('id'))) + "' dropdown"
selection.deselect_by_value(selection_value)
elif selection_type.lower == "deall":
action = "Deselected all values from the '" + str(
self.find_html_for(element.get_attribute('id'))) + "' dropdown"
selection.deselect_all()
else:
# This should really just be reported and not raise an exception
raise Exception("Unknown selection type.")
# Determine if action changes browser count
if child_window:
self.change_window_connection()
print(action)
return action
def __determine_acted_upon_option(self, options, selection_value, selection_type):
acted_upon_options = None
if selection_type == 'devalue':
selection_type = 'value'
elif selection_type == 'detext':
selection_type = 'text'
elif selection_type == 'deindex':
selection_type = 'index'
test = type(selection_value)
if type(selection_value) is str:
for option in options:
if selection_value == option.get_attribute(selection_type):
if acted_upon_options is None:
acted_upon_options = str(option.text)
else:
acted_upon_options += ', ' + str(option.text)
else:
for value in selection_value:
for option in options:
if value == option.get_attribute(selection_type):
if acted_upon_options is None:
acted_upon_options = str(option.text)
else:
acted_upon_options += ', ' + str(option.text)
return acted_upon_options