module Howitzer::CapybaraHelpers

This module holds capybara helpers methods

Public Instance Methods

chrome_browser?() click to toggle source

@return [Boolean] whether or not current browser is Google Chrome. @raise [CloudBrowserNotSpecifiedError] if cloud driver and missing browser name @raise [SelBrowserNotSpecifiedError] if selenium driver and missing browser name

# File lib/howitzer/capybara_helpers.rb, line 75
def chrome_browser?
  browser? :chrome
end
cloud_driver(app, caps, url) click to toggle source

Buids selenium driver for a cloud service @param app [<Rack>] a rack application that this server will contain @param caps [Hash] remote capabilities @param url [String] a remote hub url @return [Capybara::Selenium::Driver]

# File lib/howitzer/capybara_helpers.rb, line 156
def cloud_driver(app, caps, url)
  http_client = ::Selenium::WebDriver::Remote::Http::Default.new
  http_client.read_timeout = Howitzer.cloud_http_idle_timeout
  http_client.open_timeout = Howitzer.cloud_http_idle_timeout
  options = {
    url: url,
    http_client: http_client,
    browser: :remote
  }
  options[w3c_selenium? ? :capabilities : :desired_capabilities] =
    ::Selenium::WebDriver::Remote::Capabilities.new(caps)
  driver = Capybara::Selenium::Driver.new(app, **options)
  driver.browser.file_detector = remote_file_detector
  driver
end
cloud_driver?() click to toggle source

@return [Boolean] true if current driver related with SauceLab,

Testingbot or Browserstack cloud service
# File lib/howitzer/capybara_helpers.rb, line 50
def cloud_driver?
  CLOUD_BROWSERS.include?(Howitzer.driver.to_sym)
end
cloud_resource_path(kind) click to toggle source

@return [String] path to cloud resources (logs, videos, etc.) @note Currently SauceLabs is supported only @raise [ArgumentError] if unknown kind

# File lib/howitzer/capybara_helpers.rb, line 176
def cloud_resource_path(kind)
  case Howitzer.driver.to_sym
  when SAUCE then sauce_resource_path(kind)
  else
    '[NOT IMPLEMENTED]'
  end
end
duration(time_in_numeric) click to toggle source

@param time_in_numeric [Integer] number of seconds @return [String] formatted duration time

# File lib/howitzer/capybara_helpers.rb, line 90
def duration(time_in_numeric)
  secs = time_in_numeric.to_i
  mins = secs / 60
  hours = mins / 60
  return "[#{hours}h #{mins % 60}m #{secs % 60}s]" if hours.positive?
  return "[#{mins}m #{secs % 60}s]" if mins.positive?
  return "[0m #{secs}s]" if secs >= 0
end
ff_browser?() click to toggle source

@return [Boolean] whether or not current browser is FireFox. @raise [CloudBrowserNotSpecifiedError] if cloud driver and missing browser name @raise [SelBrowserNotSpecifiedError] if selenium driver and missing browser name

# File lib/howitzer/capybara_helpers.rb, line 67
def ff_browser?
  browser? :ff, :firefox
end
ie_browser?() click to toggle source

@return [Boolean] whether or not current browser is

Internet Explorer.

@raise [CloudBrowserNotSpecifiedError] if cloud driver and missing browser name @raise [SelBrowserNotSpecifiedError] if selenium driver and missing browser name

# File lib/howitzer/capybara_helpers.rb, line 59
def ie_browser?
  browser? :ie, :iexplore
end
load_driver_gem!(driver, lib, gem) click to toggle source

Tries to load appropriate driver gem @param driver [String] a driver name @param lib [String] what is required to load @param gem [String] a gem name @raise [LoadError] if the gem is missing in a bunder context

# File lib/howitzer/capybara_helpers.rb, line 117
def load_driver_gem!(driver, lib, gem)
  require lib
rescue LoadError
  raise LoadError,
        "`:#{driver}` driver is unable to load `#{lib}`, please add `gem '#{gem}'` to your Gemfile."
end
required_cloud_caps() click to toggle source

@return [Hash] selenium capabilities required for a cloud driver

# File lib/howitzer/capybara_helpers.rb, line 126
def required_cloud_caps
  {
    platform: Howitzer.cloud_platform,
    browserName: Howitzer.cloud_browser_name,
    version: Howitzer.cloud_browser_version,
    name: "#{prefix_name} #{Howitzer.cloud_browser_name}"
  }
end
required_w3c_cloud_caps() click to toggle source

@return [Hash] selenium W3C capabilities required for a cloud driver

# File lib/howitzer/capybara_helpers.rb, line 137
def required_w3c_cloud_caps
  {
    browserName: Howitzer.cloud_browser_name,
    browserVersion: Howitzer.cloud_browser_version
  }
end
safari_browser?() click to toggle source

@return [Boolean] whether or not current browser is Safari. @raise [CloudBrowserNotSpecifiedError] if cloud driver and missing browser name @raise [SelBrowserNotSpecifiedError] if selenium driver and missing browser name

# File lib/howitzer/capybara_helpers.rb, line 83
def safari_browser?
  browser? :safari
end
update_cloud_job_status(json_data = {}) click to toggle source

Updates a job status on the job cloud @note SauceLabs is currently supported only @param json_data [Hash] for example, (passed: true)

# File lib/howitzer/capybara_helpers.rb, line 103
def update_cloud_job_status(json_data = {})
  case Howitzer.driver.to_sym
  when SAUCE then update_sauce_job_status(json_data)
  else
    '[NOT IMPLEMENTED]'
  end
end
w3c_selenium?() click to toggle source

@return [Boolean] whether or not Selenium is W3C compatible.

# File lib/howitzer/capybara_helpers.rb, line 146
def w3c_selenium?
  Gem::Requirement.new('>=4').satisfied_by?(Gem::Version.new(Selenium::WebDriver::VERSION))
end

Private Instance Methods

browser?(*browser_aliases) click to toggle source
# File lib/howitzer/capybara_helpers.rb, line 186
def browser?(*browser_aliases)
  return cloud_browser?(*browser_aliases) if cloud_driver?
  return selenium_browser?(*browser_aliases) if selenium_driver? || selenium_grid_driver?
end
cloud_browser?(*browser_aliases) click to toggle source
# File lib/howitzer/capybara_helpers.rb, line 191
def cloud_browser?(*browser_aliases)
  unless Howitzer.cloud_browser_name.nil?
    return browser_aliases.include?(Howitzer.cloud_browser_name.to_s.downcase.to_sym)
  end

  raise Howitzer::CloudBrowserNotSpecifiedError, CHECK_YOUR_SETTINGS_MSG
end
headless_chrome_driver?() click to toggle source
# File lib/howitzer/capybara_helpers.rb, line 209
def headless_chrome_driver?
  Howitzer.driver.to_sym == HEADLESS_CHROME
end
headless_firefox_driver?() click to toggle source
# File lib/howitzer/capybara_helpers.rb, line 213
def headless_firefox_driver?
  Howitzer.driver.to_sym == HEADLESS_FIREFOX
end
prefix_name() click to toggle source
# File lib/howitzer/capybara_helpers.rb, line 221
def prefix_name
  (Howitzer.current_rake_task || 'ALL').upcase
end
remote_file_detector() click to toggle source
# File lib/howitzer/capybara_helpers.rb, line 249
def remote_file_detector
  lambda do |args|
    str = args.first.to_s
    str if File.exist?(str)
  end
end
sauce_resource_path(kind) click to toggle source
# File lib/howitzer/capybara_helpers.rb, line 225
def sauce_resource_path(kind)
  name =
    case kind
    when :video then 'video.flv'
    when :server_log then 'selenium-server.log'
    else
      raise ArgumentError, "Unknown '#{kind}' kind"
    end
  host = "https://#{Howitzer.cloud_auth_login}:#{Howitzer.cloud_auth_pass}@saucelabs.com"
  path = "/rest/#{Howitzer.cloud_auth_login}/jobs/#{session_id}/results/#{name}"
  "#{host}#{path}"
end
selenium_browser?(*browser_aliases) click to toggle source
# File lib/howitzer/capybara_helpers.rb, line 199
def selenium_browser?(*browser_aliases)
  return browser_aliases.include?(Howitzer.selenium_browser.to_s.to_sym) unless Howitzer.selenium_browser.nil?

  raise Howitzer::SelBrowserNotSpecifiedError, CHECK_YOUR_SETTINGS_MSG
end
selenium_driver?() click to toggle source
# File lib/howitzer/capybara_helpers.rb, line 205
def selenium_driver?
  Howitzer.driver.to_sym == SELENIUM
end
selenium_grid_driver?() click to toggle source
# File lib/howitzer/capybara_helpers.rb, line 217
def selenium_grid_driver?
  Howitzer.driver.to_sym == SELENIUM_GRID
end
session_id() click to toggle source
# File lib/howitzer/capybara_helpers.rb, line 245
def session_id
  Capybara.current_session.driver.browser.instance_variable_get(:@bridge).session_id
end
update_sauce_job_status(json_data = {}) click to toggle source
# File lib/howitzer/capybara_helpers.rb, line 238
def update_sauce_job_status(json_data = {})
  host = "https://#{Howitzer.cloud_auth_login}:#{Howitzer.cloud_auth_pass}@saucelabs.com"
  path = "/rest/v1/#{Howitzer.cloud_auth_login}/jobs/#{session_id}"
  url = "#{host}#{path}"
  ::RestClient.put url, json_data.to_json, content_type: :json, accept: :json
end