-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_object.rb
359 lines (307 loc) · 9.59 KB
/
base_object.rb
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
require 'selenium-webdriver'
require 'rspec/expectations'
include RSpec::Matchers
class BaseObject
def setup_one(browser)
@@driver = Selenium::WebDriver.for browser
if browser!=:safari #SafariDriver currently has issue with manage.window.size
manage_window_size
end
rescue Exception => e
puts e.message
puts "Could not start driver #{@@driver}"
exit
end
# def setup_caps(platform, browser)
# puts "In setup_caps #{platform}, #{browser}"
# caps = Selenium::WebDriver::Remote::Capabilities.new
# if browser == "iPad"
# caps['appiumVersion'] = '1.6.3'
# caps['deviceName'] = 'iPad Simulator'
# caps['platformName'] = 'iOS'
# caps['platformVersion'] = '9.3'
# caps['deviceOrientation'] = 'landscape'
# caps['browserName'] = 'Safari'
# caps['rotatable'] = true
# elsif browser == "Android" #Need an Android Emulator that runs Chrome instead of generic Browser
# caps['appiumVersion'] = '1.6.3'
# caps['platformName'] = 'Android'
# caps['platformVersion'] = '6.0'
# caps['browserName'] ='Chrome'
# caps['deviceName'] = 'Android Emulator'
# caps['deviceOrientation'] = 'landscape'
# caps['nativeWebScreenshot'] = true
# caps['rotatable'] = true
# else
# caps['platform'] = platform
# caps['browserName'] = browser
# caps['logging_prefs'] = {:browser => "ALL"}
# end
# puts "Caps are #{caps}"
# return caps
# end
#
# #does not work for tests that need to open a CODAP file
# def setup_remote(caps)
# puts "in setup_remote #{caps}"
# @@driver = Selenium::WebDriver.for(:remote,
# :url => "http://eireland:[email protected]:80/wd/hub",
# :desired_capabilities => caps)
# rescue Exception => e
# puts e.message
# puts "Could not start driver #{@@driver}"
# exit
# end
#
# def setup_grid(caps)
# puts "in setup_remote #{caps}"
# @@driver = Selenium::WebDriver.for (
# :remote,
# :url=> 'http://localhost:4444/wd/hub',
# :desired_capabilities=> caps )
# rescue Exception => e
# puts e.message
# puts "Could not start driver #{@@driver}"
# exit
# end
def teardown
@@driver.quit
end
def manage_window_size
target_size = Selenium::WebDriver::Dimension.new(1680,1050)
@@driver.manage.window.size = target_size
end
def visit(url='/')
puts "in visit"
@@driver.get(url)
# if browser
# manage_window_size
end
def verify_page(title)
puts "Page title is #{@@driver.title}"
expect(@@driver.title).to include(title)
end
def get_url
puts "In get url"
@@driver.current_url
end
def get_page_title
return @@driver.title
end
def find(locator)
@@driver.find_element locator
end
def find_all(locator)
@@driver.find_elements locator
end
def clear(locator)
find(locator).clear
end
def type(locator, input)
find(locator).send_keys input
end
def click_on(locator)
find(locator).click
end
def click_with_offset(locator,xoffset, yoffset)
puts "in click with offset"
element = find(locator)
@@driver.action.move_to(element, xoffset, yoffset).click.perform
end
def click_hold(locator,duration)
element = @@driver.find(locator)
@@driver.action.click_and_hold(element).perform
sleep(duration)
@@driver.action.release(element).perform
end
def hover(locator)
@@driver.action.move_to(locator).perform
end
def displayed?(locator)
@@driver.find_element(locator).displayed?
true
rescue Selenium::WebDriver::Error::NoSuchElementError
false
end
def wait_until_disappears(locator, timeout: 45)
wait = Selenium::WebDriver::Wait.new(timeout: timeout)
wait.until { !@@driver.find_element(locator).displayed? }
end
def text_of(locator)
find(locator).text
end
def title
@@driver.title
end
def submit_form(locator, input)
puts "in submit form"
clear(locator)
find(locator).send_keys input
find(locator).send_keys :return
end
def save_screenshot(dir,page_title)
puts "in get_screenshot"
file_name = page_title.gsub(/[?\/\s]/, '_')
file_name = file_name
@@driver.save_screenshot "#{dir}/#{file_name}.png"
end
def write_log_file(dir_path, filename)
log = @@driver.manage.logs.get(:browser)
messages = ""
log.each {|item| messages += item.message + "\n"}
if !File.exist?("#{dir_path}/#{filename}.txt")
File.open("#{dir_path}/#{filename}.txt", "wb") do |log|
log<< messages unless messages == ""
end
else
File.open("#{dir_path}/#{filename}.txt", "a") do |log|
log << messages unless messages == ""
end
end
end
def write_to_file(dir_path, filename, message)
if !File.exist?("#{dir_path}/#{filename}.txt")
File.open("#{dir_path}/#{filename}.txt", "wb") do |responses|
responses<< message unless message == ""
end
else
File.open("#{dir_path}/#{filename}.txt", "a") do |responses|
responses << message unless message == ""
end
end
end
def wait_for(seconds=45)
Selenium::WebDriver::Wait.new(:timeout => seconds).until { yield }
end
def pop_up_click(locator)
puts "In pop_up_click"
@@driver.action.click(locator).perform
end
def pop_up_type(locator, input)
puts "In pop_up_type. Input is: #{input}"
@@driver.action.send_keys(locator, input).perform
end
def move_to(locator)
@@driver.action.move_to(locator).perform
end
def switch_to_main()
puts "in switch to main"
@@driver.switch_to.default_content()
end
def switch_to_alert()
puts "In switch to alert"
@@driver.switch_to.alert()
end
def switch_to_modal()
puts "In switch to modal"
@@driver.switch_to.active_element()
end
def switch_to_iframe(locator)
puts "In switch to iframe"
@@driver.switch_to.frame(locator)
end
def get_tab_handles
return @@driver.window_handles
end
def switch_to_tab(handle)
puts "in switch to tab"
@@driver.switch_to.window(handle)
end
def switch_to_first_tab()
puts "in switch to first tab"
@@driver.switch_to.window(@@driver.window_handles.first)
window_handle = @@driver.window_handles.first
return window_handle
end
def switch_to_last_tab()
puts "in switch to last tab"
# @@driver.action.send_keys(:control + :tab).perform
@@driver.switch_to.window(@@driver.window_handles.last)
window_handle = @@driver.window_handles.last
return window_handle
end
def close_tab(handle)
puts "in close tab"
@@driver.switch_to.window(handle)
@@driver.close
end
def switch_to_last_window()
@@driver.switch_to.window(@@driver.window_handles.last)
end
def select_menu_item(menu_item_text)
puts 'in select_menu_item'
menu_item = {xpath: "//div/a[contains(@class,'menu-item')]/span[contains(text(),'#{menu_item_text}')]"}
wait_for {displayed? (menu_item)}
click_on(menu_item)
end
def select_from_dropdown(dropdown, dropdown_locator, item)
puts "In select from dropdown"
dropdown_loc = find(dropdown)
puts "Found dropdown_loc at #{dropdown_loc}"
click_on(dropdown)
sleep(3)
list_item = {xpath: "#{dropdown_locator}ul/li/a[contains(text(),'#{item}')]"}#templatePulldown>span>ul>li
click_on(list_item)
end
def get_link(page_locator)
return page_locator.attribute('href')
end
def drag_attribute(source_element, target_element)
source_loc = find(source_element)
target_loc = find(target_element)
@@driver.action.drag_and_drop(source_loc, target_loc).perform
end
def chrome_print(type)
print_save_button = {css: '#print-header > div > button.print.default'}
change_destination = {css: '#destination-settings > div.right-column > button'}
pdf = {xpath: '//ul/li/span/span[contains(@title,"Save as PDF")]'}
case (type)
when "pdf"
destination = pdf
end
switch_to_last_window
click_on(change_destination)
sleep(2)
click_on(destination)
click_on(print_save_button)
sleep(1)
keys = 'return'
system('osascript -e \'tell application "System Events" to keystroke ' + keys + "'")
end
def compare_images(path_a, path_b)
return 100 if !File.exist?(path_a) || !File.exist?(path_b)
a = Magick::Image.read(path_a).first
b = Magick::Image.read(path_b).first
a.resize!(b.columns, b.rows) if a.columns != b.columns || a.rows != b.rows
a.compare_channel(b, Magick::RootMeanSquaredErrorMetric)[1] * 100
end
def compare_file_sizes(dir_a, dir_b)
file_mismatch = []
missing_expected_files = []
dir_a_files = Dir.entries(dir_a)
dir_b_files = Dir.entries(dir_b)
num_files_a = dir_a_files.length
num_files_b = dir_b_files.length
while dir_a_files.length > 0
file = dir_a_files[0]
# puts "File is #{file}. File size test is #{File.size("#{dir_a}/#{file}")}"
# puts "Expected file is #{dir_b}/#{file}. File size expected is #{File.size("#{dir_b}/#{file}")}"
if File.exists?("#{dir_b}/#{file}")
if File.size("#{dir_a}/#{file}") != File.size("#{dir_b}/#{file}")
if file!= '.'
file_mismatch.push(file)
end
end
else
missing_expected_files.push(file)
end
dir_a_files.shift
puts ("After shift dir_a_files is #{dir_a_files}")
end
if file_mismatch.length == 0 && missing_expected_files.length == 0
return "ALL FILES MATCH"
else
return "These files do not match #{file_mismatch}. These expected files are missing #{missing_expected_files}"
end
end
end