class Webdrivers::Common

Constants

EMPTY_VERSION

Attributes

required_version[W]

Public Class Methods

driver_path() click to toggle source

Returns path to the driver binary.

@return [String]

# File lib/webdrivers/common.rb, line 115
def driver_path
  File.absolute_path File.join(System.install_dir, file_name)
end
remove() click to toggle source

Deletes the existing driver binary.

# File lib/webdrivers/common.rb, line 104
def remove
  @download_url = nil
  @latest_version = nil
  System.delete "#{System.install_dir}/#{file_name.gsub('.exe', '')}.version"
  System.delete driver_path
end
required_version() click to toggle source

Returns the user defined required version.

@return [Gem::Version]

# File lib/webdrivers/common.rb, line 82
def required_version
  normalize_version(@required_version ||= nil)
end
update() click to toggle source

Triggers an update check.

@return [String] Path to the driver binary.

# File lib/webdrivers/common.rb, line 90
def update
  if correct_binary?
    msg = required_version != EMPTY_VERSION ?  'The required webdriver version' : 'A working webdriver version'
    Webdrivers.logger.debug "#{msg} is already on the system"
    return driver_path
  end

  remove
  System.download(download_url, driver_path)
end

Private Class Methods

binary_version() click to toggle source
# File lib/webdrivers/common.rb, line 151
def binary_version
  version = System.call(driver_path, '--version')
  Webdrivers.logger.debug "Current version of #{driver_path} is #{version}"
  version
rescue Errno::ENOENT
  Webdrivers.logger.debug "No Such File or Directory: #{driver_path}"
  nil
end
correct_binary?() click to toggle source
# File lib/webdrivers/common.rb, line 133
def correct_binary?
  current_version == if required_version == EMPTY_VERSION
                       latest_version
                     else
                       normalize_version(required_version)
                     end
rescue ConnectionError, VersionError
  driver_path if sufficient_binary?
end
download_url() click to toggle source
# File lib/webdrivers/common.rb, line 121
def download_url
  @download_url ||= if required_version == EMPTY_VERSION
                      downloads[downloads.keys.max]
                    else
                      downloads[normalize_version(required_version)]
                    end
end
exists?() click to toggle source
# File lib/webdrivers/common.rb, line 129
def exists?
  System.exists? driver_path
end
normalize_version(version) click to toggle source
# File lib/webdrivers/common.rb, line 147
def normalize_version(version)
  Gem::Version.new(version.to_s)
end
sufficient_binary?() click to toggle source
# File lib/webdrivers/common.rb, line 143
def sufficient_binary?
  exists?
end
with_cache(file_name, driver_build = nil, browser_build = nil) { || ... } click to toggle source

Returns cached driver version if cache is still valid and the driver binary exists. Otherwise caches the given version (typically the latest available) In case of Chrome, it also verifies that the driver build and browser build versions are compatible. Example usage: lib/webdrivers/chromedriver.rb:34

# File lib/webdrivers/common.rb, line 164
def with_cache(file_name, driver_build = nil, browser_build = nil)
  if System.valid_cache?(file_name) && exists? && (driver_build == browser_build)
    cached_version = System.cached_version(file_name)
    Webdrivers.logger.debug "using cached version as latest: #{cached_version}"
    normalize_version cached_version
  else
    version = yield
    System.cache_version(file_name, version)
    normalize_version version
  end
end