Class: OctAutomationUtilities::Utilities

Inherits:
Object
  • Object
show all
Includes:
Capybara::DSL, Capybara::RSpecMatchers, RSpec::Matchers
Defined in:
lib/oct-automation-utilities.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUtilities

Returns a new instance of Utilities.



19
20
21
22
23
24
25
26
27
28
# File 'lib/oct-automation-utilities.rb', line 19

def initialize
  app = AutomationFramework::Application.new
  self.project_directory = get_project_directory
  self.test_name = 'default'
  self.test_time = get_time
  @config = AutomationFramework::Configuration.new
  @log = Logger.new 'logger.log'
  self.setup_log_level app.log_level
  self.capture_screen = app.capture_screen_shot
end

Instance Attribute Details

#capture_screenObject

test data



14
15
16
# File 'lib/oct-automation-utilities.rb', line 14

def capture_screen
  @capture_screen
end

#exe_timeObject

test data



14
15
16
# File 'lib/oct-automation-utilities.rb', line 14

def exe_time
  @exe_time
end

#obj_expected_page_contentObject

List of Objext



17
18
19
# File 'lib/oct-automation-utilities.rb', line 17

def obj_expected_page_content
  @obj_expected_page_content
end

#project_directoryObject

test data



14
15
16
# File 'lib/oct-automation-utilities.rb', line 14

def project_directory
  @project_directory
end

#screen_shot_pathObject

test data



14
15
16
# File 'lib/oct-automation-utilities.rb', line 14

def screen_shot_path
  @screen_shot_path
end

#test_nameObject

test data



14
15
16
# File 'lib/oct-automation-utilities.rb', line 14

def test_name
  @test_name
end

#test_timeObject

test data



14
15
16
# File 'lib/oct-automation-utilities.rb', line 14

def test_time
  @test_time
end

Instance Method Details

#browser_backObject



153
154
155
# File 'lib/oct-automation-utilities.rb', line 153

def browser_back
  page.evaluate_script('window.history.back()')
end

#browser_launch(bro, url) ⇒ Object



212
213
214
215
216
# File 'lib/oct-automation-utilities.rb', line 212

def browser_launch(bro, url)
  visit url
  self.browser_maximize
  self.log("Browser #{bro} launched with url #{url}", 'info')
end

#browser_maximizeObject



206
207
208
209
210
# File 'lib/oct-automation-utilities.rb', line 206

def browser_maximize
  if @config.head_less == 'false'
    page.driver.browser.manage.window.maximize
  end
end

#capture_screen_shot(msg = '', status = '') ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/oct-automation-utilities.rb', line 103

def capture_screen_shot(msg='', status = '')
  begin
    if self.capture_screen == 'true' or status == 'fail'
      #used to sync the page so it can capture the screen shot.
      page.has_no_title?('foo')
      msgx =  "#{$test}/#{$step_num.to_s}-#{msg.to_s}"
      if status == 'fail'
        self.log "failed screenshot name: #{msgx}", "fatal"
        page.driver.save_screenshot("#{msgx[0,101].to_s}-#{status}.png")
      else
        self.log "screenshot name: #{msgx}", "debug"
        page.driver.save_screenshot("#{msgx[0,101].to_s}.png")
      end
      $step_num = ($step_num.to_i + 1).to_s
    end
    if status == ''
      status = 'PASSED'
    end
    self.log("#{msg} - #{status}", 'info')
  rescue Exception => e
    self.log("#{msg} - FAILED", 'info')
    raise e
  end
end

#check(attribute = 'name', obj) ⇒ Object



259
260
261
262
263
# File 'lib/oct-automation-utilities.rb', line 259

def check(attribute = 'name', obj)
  if is_checked(obj) == false
    page.find("[#{attribute}^='#{obj}']").click
  end
end

#click_ok_msgboxObject



165
166
167
# File 'lib/oct-automation-utilities.rb', line 165

def click_ok_msgbox
  page.driver.browser.switch_to.alert.accept
end

#click_on_ok_popupObject

Description : Click on OK button on pop-up



245
246
247
# File 'lib/oct-automation-utilities.rb', line 245

def click_on_ok_popup
  page.driver.browser.switch_to.alert.accept
end

#close_all_windowsObject



146
147
148
149
150
151
# File 'lib/oct-automation-utilities.rb', line 146

def close_all_windows
    page.driver.browser.window_handles.each do |handle|
    page.driver.browser.switch_to.window(page.driver.browser.window_handles.last)
    page.execute_script "window.close()"
  end
end

#close_current_windowObject



139
140
141
142
143
144
# File 'lib/oct-automation-utilities.rb', line 139

def close_current_window
  Capybara.reset_sessions! # Guarantees the session ID/Cache is cleared.
  page.driver.browser.switch_to.window(page.driver.browser.window_handles.last)
  page.execute_script "window.close();"
  page.driver.browser.switch_to.window(page.driver.browser.window_handles.first)
end

#close_windowsObject



132
133
134
135
136
137
# File 'lib/oct-automation-utilities.rb', line 132

def close_windows
  page.driver.browser.window_handles.each do |handle|
    page.driver.browser.switch_to.window(handle)
    page.exit(0)
  end
end

#compare_strings(expected_string, test_string, description, case_sensitive = false) ⇒ Object

compare_strings compares two strings, as when verifying on-screen content. Logs results, throws exception when false. Returns true when strings match, false when they do not. Parameters:

expected_string: a string containing the expected content to be verified
test_string: a string containing the test content to compare to expected_string
description: a string describing the expected content, i.e. "Sender's Email Address"
case_sensitive: a boolean that controls whether the comparison is case_sensitive, defaults to false.


286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/oct-automation-utilities.rb', line 286

def compare_strings (expected_string, test_string, description, case_sensitive = false)
  estr = case_sensitive ? expected_string.upcase : expected_string
  tstr = case_sensitive ? test_string.upcase : test_string
  if estr == tstr
    log "'#{description}' verified equal to '#{expected_string}'."
    return true
  else
    error_str = "'#{description}' verification FAILED. Expected '#{expected_string}', got '#{test_string}'."
    log error_str
    raise Capybara::ExpectationNotMet, error_str
    return false
  end
end

#confirm_popup_message(message, response = 'no') ⇒ Object

Confirm pop up contains proper message



229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/oct-automation-utilities.rb', line 229

def confirm_popup_message(message, response = 'no')
  a = page.driver.browser.switch_to.alert.text
  if response == 'yes'
    page.driver.browser.switch_to.alert.accept
  else
    page.driver.browser.switch_to.alert.dismiss
  end

  if a.include? message
    return 0
  else
    return 1
  end
end

#create_results_folderObject



75
76
77
# File 'lib/oct-automation-utilities.rb', line 75

def create_results_folder
  Dir.mkdir("#{self.project_directory}/results")
end

#create_test_folder(test_name) ⇒ Object



79
80
81
82
# File 'lib/oct-automation-utilities.rb', line 79

def create_test_folder(test_name)
  puts "create_test_folder #{self.project_directory}/results/#{test_name}"
  Dir.mkdir("#{self.project_directory}/results/#{test_name}")
end

#create_time_results_folder(test_name) ⇒ Object



84
85
86
87
88
# File 'lib/oct-automation-utilities.rb', line 84

def create_time_results_folder(test_name)
  unless File.exists?("#{self.project_directory}/results/#{test_name}/#{self.test_time}")
    Dir.mkdir("#{self.project_directory}/results/#{test_name}/#{self.test_time}")
  end
end

#get_project_directoryObject



157
158
159
# File 'lib/oct-automation-utilities.rb', line 157

def get_project_directory
  File.expand_path('../../../', __FILE__)
end

#get_timeObject



90
91
92
# File 'lib/oct-automation-utilities.rb', line 90

def get_time
  "#{Time.now.day}#{Time.now.month}#{Time.now.year}#{Time.now.hour.to_i - Time.now.zone.to_i}#{Time.now.min}#{Time.now.sec}"
end

#is_checked(attribute = 'name', obj) ⇒ Object



255
256
257
# File 'lib/oct-automation-utilities.rb', line 255

def is_checked(attribute = 'name', obj)
  page.find("[#{attribute}^='#{obj}']").checked?
end

#is_content_visible(content) ⇒ Object

Desc: Verifies content of the text and returns true or false. This method is different from Verify_page_content. Verify_page_content fails and exits test if expected content is not visible. This method returns false when content is not visible and continue to the next step



251
252
253
# File 'lib/oct-automation-utilities.rb', line 251

def is_content_visible(content)
  page.body.include?(content)
end

#log(msg, severity = 'info') ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/oct-automation-utilities.rb', line 57

def log(msg, severity = 'info')
  case severity
  when 'progname'
    @log.progname = msg
  when 'debug'
    @log.debug msg
  when 'info'
    @log.info msg
  when 'warn'
    @log.warn msg
  when 'error'
    @log.error msg
  when 'fatal'
    @log.fatal msg
  end
  puts msg
end

#retrieve_cell_content(table_index, column, row_num) ⇒ Object

Description: retrieve row text of a given row and column, row number starts from 0



191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/oct-automation-utilities.rb', line 191

def retrieve_cell_content(table_index,column,row_num)
  row_number = 0
  text_to_return = ''
  source = page.evaluate_script("document.getElementsByTagName('table')[#{table_index}].innerHTML")
  doc = Nokogiri::HTML(source)
  doc.css('tr').each do |row|
    if row_number == row_num
      text_to_return =  row.xpath("./td[#{column.to_i}]").text
      break
    end
    row_number = row_number+1
  end
  return text_to_return
end

#retrieve_row_number(table_index, column = '', text_to_verify) ⇒ Object

Description: retrieve row number for the given text in a column



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/oct-automation-utilities.rb', line 170

def retrieve_row_number(table_index,column='',text_to_verify)
  row_number = 1
  text_check = false
  source = page.evaluate_script("document.getElementsByTagName('table')[#{table_index}].innerHTML")
  doc = Nokogiri::HTML(source)
  doc.css('tr').each do |row|
    if row.xpath("./td[#{column.to_i}]").text == text_to_verify
      text_check = true
      break
    end
    row_number = row_number + 1
  end
  if text_check == true
    return row_number
  else
    return -1
  end

end

#scroll(scroll_position) ⇒ Object

Desc:moves the scroll bar position



224
225
226
# File 'lib/oct-automation-utilities.rb', line 224

def scroll(scroll_position)
  page.execute_script "window.scrollBy(0,#{scroll_position})"
end

#set_checkbox(checkbox_obj, checkbox_title, check_uncheck) ⇒ Object

set_checkbox checks or unchecks a checkbox using its ID, name, or label text. It logs its results, and does not care whether the box is initially checked or unchecked. Logs results. Returns true when Parameters:

checkbox_obj is the ID, name, or label text of the checkbox
checkbox_title: a string describing the checkbox, i.e. "notify me when viewed"
check_uncheck: a string with one of two values: "check" or "uncheck"


344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/oct-automation-utilities.rb', line 344

def set_checkbox (checkbox_obj, checkbox_title, check_uncheck)
  if check_uncheck.downcase == "check"
    Capybara::check(checkbox_obj)
  elsif check_uncheck.downcase == "uncheck"
    Capybara::uncheck(checkbox_obj)
  else
    raise(Capybara::ExpectationNotMet, "set_checkbox: check_uncheck invalid value '#{check_uncheck}'. Valid values: 'check' or 'uncheck'.")
    return(false)
  end
  log("Checkbox '#{checkbox_title}' successful action: '#{check_uncheck}'.", "info")
  return(true)
end

#set_checkbox_xpath(checkbox_obj, checkbox_title, check_uncheck) ⇒ Object

This is the same as set_checkbox, but uses an xpath instead of an ID, name, or label



358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/oct-automation-utilities.rb', line 358

def set_checkbox_xpath (checkbox_obj, checkbox_title, check_uncheck)
  if check_uncheck.downcase == "check"
    page.find(:xpath, checkbox_obj).set(true)
  elsif check_uncheck.downcase == "uncheck"
    page.find(:xpath, checkbox_obj).set(false)
  else
    raise(Capybara::ExpectationNotMet, "set_checkbox_xpath: check_uncheck invalid value '#{check_uncheck}'. Valid values: 'check' or 'uncheck'.")
    return(false)
  end
  log("Checkbox '#{checkbox_title}' successful action: '#{check_uncheck}'.")
  return(true)
end

#set_default_wait_time(wait_time) ⇒ Object

Desc:changed the default wait time(2) to the wait time passing through parameter



272
273
274
275
276
# File 'lib/oct-automation-utilities.rb', line 272

def set_default_wait_time(wait_time)
  if wait_time !=''
    Capybara.default_wait_time=Capybara.default_wait_time.to_i+wait_time.to_i
  end
end

#setup_directory(test_name = "default#{get_time}") ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/oct-automation-utilities.rb', line 30

def setup_directory(test_name = "default#{get_time}")
  Dir.exist?("#{self.project_directory}/results") ? true : create_results_folder
  Dir.exist?("#{self.project_directory}/results/#{test_name}") ? true : create_test_folder(test_name)
  create_time_results_folder(test_name)
  #Need to set self.screen_shot_path so that we can override the path as needed.
  self.screen_shot_path = "#{self.project_directory}/results/#{test_name}/#{self.test_time}"
  $test = self.screen_shot_path
  $step_num = 01
  self.log("Started - #{test_name}\n\nResults Path: #{self.screen_shot_path}", 'info')

end

#setup_log_level(log_level) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/oct-automation-utilities.rb', line 42

def setup_log_level(log_level)
  case log_level
  when 'info'
    @log.level = Logger::INFO
  when 'debug'
    @log.level = Logger::DEBUG
  when 'warn'
    @log.level = Logger::WARN
  when 'error'
    @log.level = Logger::ERROR
  when 'fatal'
    @log.level = Logger::FATAL
  end
end

#string_is_numeric?(a_string) ⇒ Boolean

string_is_numeric? returns true if the string only contains digits, or false if there is anything else in the string, even + or -, or a decimal. It uses regular expression evaluation. I ripped this off from this web site: www.ruby-forum.com

Returns:

  • (Boolean)


375
376
377
# File 'lib/oct-automation-utilities.rb', line 375

def string_is_numeric? (a_string)
  /\A\d+\z/ === a_string
end

#switch_windows(window_title) ⇒ Object



128
129
130
# File 'lib/oct-automation-utilities.rb', line 128

def switch_windows(window_title)
  page.within_window(window_title)
end

#switchtonewlyopenedwindowObject



387
388
389
# File 'lib/oct-automation-utilities.rb', line 387

def switchtonewlyopenedwindow
  page.driver.browser.switch_to.window(page.driver.browser.window_handles.last)
end

#todayMMDDYYYYObject



379
380
381
382
383
384
385
# File 'lib/oct-automation-utilities.rb', line 379

def todayMMDDYYYY
  right_now = Time.new
  day = ("00" + right_now.day.to_s)[-2,2]
  month = ("00" + right_now.month.to_s)[-2,2]
  year = ("0000" + right_now.year.to_s)[-4,4]
  return "#{month}/#{day}/#{year}"
end

#uncheck(attribute = 'name', obj) ⇒ Object



265
266
267
268
269
# File 'lib/oct-automation-utilities.rb', line 265

def uncheck(attribute = 'name', obj)
  if is_checked(obj) == true
    page.find("[#{attribute}^='#{obj}']").click
  end
end

#unique_idObject



161
162
163
# File 'lib/oct-automation-utilities.rb', line 161

def unique_id
  Random.rand(0..9999999999).to_s
end

#verify_page_contains_button(text_to_verify) ⇒ Object

Verify the text passed in is located on the page.



313
314
315
316
# File 'lib/oct-automation-utilities.rb', line 313

def verify_page_contains_button(text_to_verify)
  log("Verify page has button: #{text_to_verify}")
  page.has_button?(text_to_verify) ? true : raise(Capybara::ExpectationNotMet, "Couldn't locate #{text_to_verify} button on the page.")
end

#verify_page_contains_field(id_name_label, text, description) ⇒ Object

Verify the field exists and contains the text. Description is simply the label of the control



331
332
333
334
335
# File 'lib/oct-automation-utilities.rb', line 331

def verify_page_contains_field(id_name_label, text, description)
  log("Verify page has field for: #{description}")
  page.has_field?(id_name_label, :with => text) ? true : raise(Capybara::ExpectationNotMet, "Couldn't locate #{description} on the page.")
  #page.has_field?(id_name_label) ? true : raise(Capybara::ExpectationNotMet, "Couldn't locate #{description} on the page.")
end

Verify the text passed in is located on the page.



307
308
309
310
# File 'lib/oct-automation-utilities.rb', line 307

def verify_page_contains_link(text_to_verify)
  log("Verify page has link: #{text_to_verify}")
  page.has_link?(text_to_verify) ? true : raise(Capybara::ExpectationNotMet, "Couldn't locate #{text_to_verify} link on the page.")
end

#verify_page_contains_text(text_to_verify) ⇒ Object

Verify the text passed in is located on the page.



301
302
303
304
# File 'lib/oct-automation-utilities.rb', line 301

def verify_page_contains_text(text_to_verify)
  log("Verify page has text: #{text_to_verify}")
  page.has_xpath?(".//*[contains(*," + 34.chr + text_to_verify + 34.chr + ")]") ? true : raise(Capybara::ExpectationNotMet, "Couldn't locate #{text_to_verify} on the page.")
end

#verify_page_contains_xpath(text_to_verify, description) ⇒ Object

Verify the text passed in is located on the page.



319
320
321
322
# File 'lib/oct-automation-utilities.rb', line 319

def verify_page_contains_xpath(text_to_verify, description)
  log("Verify page has xpath for: #{description}")
  page.has_xpath?(text_to_verify) ? true : raise(Capybara::ExpectationNotMet, "Couldn't locate #{description} on the page.")
end

#verify_page_contains_xpath_with_text(xpath, text_to_verify) ⇒ Object

Verify the text passed in is located on the page.



325
326
327
328
# File 'lib/oct-automation-utilities.rb', line 325

def verify_page_contains_xpath_with_text(xpath, text_to_verify)
  log("Verify page has xpath for: #{text_to_verify}")
  page.has_xpath?(xpath, :text => text_to_verify) ? true : raise(Capybara::ExpectationNotMet, "Couldn't locate #{text_to_verify} on the page.")
end

#verify_page_content(content = '') ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/oct-automation-utilities.rb', line 94

def verify_page_content(content = '')
  expect(page).to_not have_content('HTTP Status')
  if content != nil
    self.obj_expected_page_content = content
  end
  expect(page.body).to have_content(self.obj_expected_page_content)
 self.capture_screen_shot("Verify content - #{self.obj_expected_page_content}")
end

#wait_until_text_appears(content) ⇒ Object

Description: waits until given text appears on the page



219
220
221
# File 'lib/oct-automation-utilities.rb', line 219

def wait_until_text_appears(content)
  page.has_content?(content)
end