class RoboTest::Driver

Attributes

driver[RW]

Public Class Methods

get_all_drivers() click to toggle source
# File lib/robotest/driver.rb, line 309
def self.get_all_drivers
  return @@drivers_with_names
end
new(driver_name = "Driver", browser = $conf['browser'], opts = {}) click to toggle source
# File lib/robotest/driver.rb, line 12
def initialize(driver_name = "Driver", browser = $conf['browser'], opts = {})
  begin
    start(driver_name,browser, opts)
    puts "#{driver_name} is initialized"
  rescue Exception => e
    puts "#{driver_name} is failed to initialize \nRetrying to initialize #{driver_name}"
    start(driver_name,browser, opts)
    puts "#{driver_name} is initialized after an exception"
  end
end
quit_all_drivers() click to toggle source
# File lib/robotest/driver.rb, line 302
def self.quit_all_drivers
  @@drivers.each do |driver|
    driver.quit if driver != self
  end
  puts "deleted all the browsers"
end
switch_to(driver) click to toggle source
# File lib/robotest/driver.rb, line 337
def self.switch_to(driver)
  $focus_driver = driver
end

Public Instance Methods

action() click to toggle source
# File lib/robotest/driver.rb, line 169
def action
  $focus_driver = self
  return @driver.action
end
alert(ok_cancel) click to toggle source
# File lib/robotest/driver.rb, line 313
def alert(ok_cancel)
  sleep 2
  alert = @driver.switch_to.alert
  alertMsg=alert.text
  if ok_cancel
    alert.accept
    puts "The alert was accepted in #{$focus_driver} with alert message #{alertMsg}"
  else
    alert.dismiss
    puts "The alert was dismissed in #{$focus_driver} with alert message #{alertMsg}"
  end
  return alertMsg
end
body_text() click to toggle source
# File lib/robotest/driver.rb, line 226
def body_text
  $focus_driver = self
  @driver.find_element(:css, 'body').text
end
browser() click to toggle source
# File lib/robotest/driver.rb, line 181
def browser
  $focus_driver = self
  @driver.browser
end
capabilities() click to toggle source
# File lib/robotest/driver.rb, line 186
def capabilities
  $focus_driver = self
  @driver.capabilities
end
close() click to toggle source
# File lib/robotest/driver.rb, line 281
def close
  $focus_driver = self
  @driver.close
  puts "Closed the browser - #{$focus_driver}"
end
current_url() click to toggle source
# File lib/robotest/driver.rb, line 191
def current_url
  $focus_driver = self
  @driver.current_url
end
drag_and_drop(source_locator, target_locator) click to toggle source
# File lib/robotest/driver.rb, line 341
def drag_and_drop(source_locator, target_locator)
  source = find_element(source_locator)
  target = find_element(target_locator)
  @driver.action.click_and_hold(source).perform
  @driver.action.move_to(target).release.perform
  sleep 3
  puts "In driver #{$focus_driver} - #{source_locator.how} => source_locator.what locator was dragged and moved to this locator #{target_locator.how} => #{target_locator.what}"
end
enable_chrome_headless_downloads(directory) click to toggle source
# File lib/robotest/driver.rb, line 121
def enable_chrome_headless_downloads(directory)
  bridge = @driver.send(:bridge)
  path = '/session/:session_id/chromium/send_command'
  path[':session_id'] = bridge.session_id
  bridge.http.call(:post, path, {
                     "cmd" => "Page.setDownloadBehavior",
                     "params" => {
                       "behavior" => "allow",
                       "downloadPath" => directory,
                     }
  })
end
execute_async_script(script, *args) click to toggle source
# File lib/robotest/driver.rb, line 196
def execute_async_script(script, *args)
  $focus_driver = self
  @driver.execute_async_script(script, *args)
end
execute_script(script) click to toggle source
# File lib/robotest/driver.rb, line 201
def execute_script(script)
  $focus_driver = self
  @driver.execute_script(script)
end
find_element(locator) click to toggle source
# File lib/robotest/driver.rb, line 146
def find_element(locator)
  $focus_driver = self
  RoboTest::Wait.wait_for_element(locator)
  return @driver.find_element(locator.how,locator.what)
end
find_elements(locator) click to toggle source
# File lib/robotest/driver.rb, line 152
def find_elements(locator)
  $focus_driver = self
  return @driver.find_elements(locator.how,locator.what)
end
get(url) click to toggle source
# File lib/robotest/driver.rb, line 134
def get(url)
  $focus_driver = self
  @driver.get(url)
  puts "#{$focus_driver} loaded with - #{url}"
end
inspect() click to toggle source
# File lib/robotest/driver.rb, line 206
def inspect
  $focus_driver = self
  @driver.inspect
end
is_alert_present?() click to toggle source
# File lib/robotest/driver.rb, line 327
def is_alert_present?
  begin
    alert = @driver.switch_to.alert
    alertMsg=alert.text
    return true
  rescue Exception => e
    return false
  end
end
manage() click to toggle source
# File lib/robotest/driver.rb, line 211
def manage
  $focus_driver = self
  @driver.manage
end
mouse() click to toggle source
# File lib/robotest/driver.rb, line 164
def mouse
  $focus_driver = self
  return @driver.mouse
end
mouse_over(locator,index=1) click to toggle source
# File lib/robotest/driver.rb, line 157
def mouse_over(locator,index=1)
  $focus_driver = self
  element=find_elements(locator)[index-1]
  @driver.action.move_to(element).perform
  puts "mouse over for the element - #{locator.how} => #{locator.what} is done"
end
move_and_click(locator) click to toggle source
# File lib/robotest/driver.rb, line 174
def move_and_click(locator)
  $focus_driver = self
  ele=find_element(locator)
  @driver.action.move_to(ele).click.perform
  puts "Mouse over the locator and then click for - #{locator.how} => #{locator.what} is done"
end
navigate() click to toggle source
on() { || ... } click to toggle source
# File lib/robotest/driver.rb, line 350
def on
  $focus_driver = self
  yield
end
page_source() click to toggle source
# File lib/robotest/driver.rb, line 221
def page_source
  $focus_driver = self
  @driver.page_source
end
quit() click to toggle source
# File lib/robotest/driver.rb, line 287
def quit
  @driver.quit
  @@drivers.delete(self)
  $focus_driver = @@drivers[0]
  puts "Quit the browser - #{$focus_driver}"
end
quit_all() click to toggle source
# File lib/robotest/driver.rb, line 294
def quit_all
  @@drivers.each do |driver|
    driver.quit if driver != self
  end
  self.quit
  puts "deleted all the browsers"
end
refresh() click to toggle source
# File lib/robotest/driver.rb, line 140
def refresh
  $focus_driver = self
  navigate.refresh
  puts "#{$focus_driver} is refreshed"
end
revert_to(window=nil) click to toggle source
# File lib/robotest/driver.rb, line 270
def revert_to(window=nil)
  $focus_driver = self
  if window != nil
    @driver.switch_to.window(window)
    puts "Switched back to another window - #{window} in #{$focus_driver}"
  else
    @driver.switch_to.window(@main_window)
    puts "Switched back to main window in #{focus_driver}"
  end
end
save_screenshot(file_name = nil) click to toggle source
# File lib/robotest/driver.rb, line 231
def save_screenshot(file_name = nil)
  $focus_driver = self
  file_name = "#{Pathname.pwd}/#{$conf['screenshot_location']}/#{Time.new.strftime("%Y-%m-%d-%H-%M-%S-%L-%N")}.png" if file_name.nil?
  puts "#{$focus_driver}'s Screenshot saved in this path => #{file_name}"
  @driver.save_screenshot(file_name)
end
scroll_to_locator(locator) click to toggle source
# File lib/robotest/driver.rb, line 262
def scroll_to_locator(locator)
  $focus_driver = self
  element = find_element(locator)
  @driver.execute_script("arguments[0].scrollIntoView({behavior: 'smooth', block: 'center', inline: 'nearest'});",element)
  puts "Scroll to this locator - #{locator.how} => #{locator.what} on #{$focus_driver}"
  sleep 1
end
start(driver_name, browser, opts = {}) click to toggle source

Custom methods of robotest #

# File lib/robotest/driver.rb, line 27
def start(driver_name, browser, opts = {})
  dimensions = $conf['dimensions']

  if (!dimensions['horizontal'] or !dimensions['vertical'])
    dimensions = {'horizontal' => 1366, 'vertical' => 768}
  end

  if (!$conf['implicit_wait'])
    puts "Specify implicit_wait for your project in the .yml files"
    $conf['implicit_wait'] = 20
  end

  prefs = {}
  if opts.empty?
    switches = $conf['switches']
    prefs = $conf['prefs'] if $conf.key?('prefs')
  elsif opts.key?(:switches)
    switches = opts[:switches]
  elsif opts.key?(:prefs)
    prefs = opts[:prefs]
  end

  case browser

  when 'firefox', 'ff'
    options = Selenium::WebDriver::Firefox::Options.new
    switches.map { |k| options.add_argument(k) }
    if prefs['profile']
      if prefs['profile']['name']
        profile = Selenium::WebDriver::Firefox::Profile.from_name(prefs['profile']['name'])
        options.profile = profile
      end
    end
    if prefs['download']
      options.add_preference("browser.download.folderList", 2)
      options.add_preference("browser.download.dir", "#{Pathname.pwd}/#{prefs['download']['default_directory']}")
      options.add_preference("browser.download.manager.alertOnEXEOpen", false)
      options.add_preference("browser.helperApps.neverAsk.saveToDisk" , "application/msword,application/csv,text/csv,image/png ,image/jpeg, application/pdf, text/html,text/plain,application/octet-stream")
    end
    caps = Selenium::WebDriver::Remote::Capabilities.firefox(
      'marionette' => true,
      'moz:useNonSpecCompliantPointerOrigin' => false,
      'moz:webdriverClick' => false
    )
    @driver = Selenium::WebDriver.for :firefox, :desired_capabilities => caps, options: options
    
  when 'ie', 'internet_explorer'
    caps = Selenium::WebDriver::Remote::Capabilities.internet_explorer('ie.ensureCleanSession' => true, 'ie.browserCommandLineSwitches' => 'private')
    @driver = Selenium::WebDriver.for(:internet_explorer, :desired_capabilities => caps)

  when 'edge'
    @driver = Selenium::WebDriver.for :edge

  when 'chrome'
    options = Selenium::WebDriver::Chrome::Options.new
    # options.add_preference(:download, prefs["download"]) if prefs["download"]
    if prefs
      prefs.each do |key, value|
        options.add_preference(key, value)
      end
    end

    if opts.key?(:extension)
      options.add_extension(opts[:extension])
    end
    switches.map { |k| options.add_argument(k) }
    @driver = Selenium::WebDriver.for(:chrome, options: options)
    if prefs["download"]
      enable_chrome_headless_downloads("#{Pathname.pwd}/#{prefs["download"]["default_directory"]}")
    else
      enable_chrome_headless_downloads(Pathname.pwd)
      puts "WARNING: Download path is set to the current root folder if it is not specified in the config file.\nExample: "
      puts "prefs:
                download:
                  default_directory: downloads/"
    end

  when 'safari'
    @driver = Selenium::WebDriver.for(:safari, opts)

  else
    raise ArgumentError, "Specify a proper browser while initiating a driver \n \n#{browser.inspect}"
  end

  target_size = Selenium::WebDriver::Dimension.new(dimensions["horizontal"], dimensions["vertical"])
  @driver.manage.window.size = target_size
  @click_exception_count=0
  @@drivers.push(self)
  @@drivers_with_names[self] = "#{driver_name}"
  $focus_driver = self
  puts "#{driver_name} - #{self}"
  return self
end
switch_to_frame(locator) click to toggle source
# File lib/robotest/driver.rb, line 238
def switch_to_frame(locator)
  $focus_driver = self
  @main_window=@driver.window_handle
  @driver.switch_to.frame(find_element(locator))
  puts "Switched to iframe - #{locator.how} => #{locator.what} on #{$focus_driver}"
  return @main_window
end
switch_to_window(locator=nil) click to toggle source
# File lib/robotest/driver.rb, line 246
def switch_to_window(locator=nil)
  $focus_driver = self
  @main_window=@driver.window_handle
  locator.click if locator != nil
  windows=@driver.window_handles
  new_window=nil;
  windows.length.times do |i|
    if windows[i] != @main_window
      new_window=windows[i]
    end
  end
  @driver.switch_to.window(new_window)
  puts "Switched to new window on #{$focus_driver}"
  return @main_window
end