class TrueAutomation::Driver::Capybara

Public Class Methods

new(app, **options) click to toggle source
Calls superclass method
# File lib/true_automation/driver/capybara.rb, line 69
def initialize(app, **options)
  options = fetch_options(options)
  @port = options.delete(:port) || find_available_port('localhost')
  @driver = options.delete(:driver)
  @driver_version = options.delete(:driver_version)

  if options && options[:ta_debug]
    @ta_debug = ' --ta-debug'
    options.delete(:ta_debug)
  end

  if options[:ta_service]
    @ta_service = options.delete(:ta_service)
  end

  super(app, options)

  @ta_client = TrueAutomation::Client.new
  @remote = ''

  options ||= {}
  ta_url = options[:ta_url] || "http://localhost:#{@port}/"

  capabilities = options[:desired_capabilities] || {}

  if options and options[:browser] == :remote
    raise 'Remote driver URL is not specified' unless options[:url]
    capabilities[:taRemoteUrl] = options[:url]
    @remote = ' --remote'
  else
    capabilities[:browser] = options[:browser] || :chrome
  end

  @options.merge!(browser: :remote,
                  url: ta_url,
                  desired_capabilities: capabilities)
end

Public Instance Methods

browser() click to toggle source
Calls superclass method
# File lib/true_automation/driver/capybara.rb, line 107
def browser
  unless @browser
    @ta_client.start(port: @port,
                     remote: @remote,
                     ta_debug: @ta_debug,
                     driver: @driver,
                     ta_service_path: @ta_service&.executable_path,
                     driver_version: @driver_version)

    @ta_client.wait_until_start

    at_exit do
      @ta_client.stop
    end
    super
  end
  @browser
end
quit() click to toggle source
Calls superclass method
# File lib/true_automation/driver/capybara.rb, line 126
def quit
  super
  @ta_client.stop
end
specialize_driver(*args) click to toggle source
Calls superclass method
# File lib/true_automation/driver/capybara.rb, line 131
def specialize_driver(*args)
  if ::Capybara::Selenium::Driver.respond_to?(:register_specialization)
    browser_type = browser.browser
    # Original method uses self.class, and all classes use Capybara::Selenium::Driver.
    # Thereby no specializations in the list for TA driver class and error occured.
    ::Capybara::Selenium::Driver.specializations.select { |k, _v| k === browser_type }.each_value do |specialization| # rubocop:disable Style/CaseEquality
      extend specialization
    end
  else
    super
  end
end

Private Instance Methods

fetch_options(options) click to toggle source
# File lib/true_automation/driver/capybara.rb, line 145
def fetch_options(options)
  if options.key?(:options)
    browser = options[:options].class.name.split('::')[2]
    desCaps = Selenium::WebDriver::Remote::Capabilities.send(browser.downcase)
    opts = options[:options].as_json
    desCaps[opts.keys.first] = opts[opts.keys.first]
    options[:desired_capabilities] = desCaps
    options.delete(:options)
  end
  options
end
find_available_port(host) click to toggle source
# File lib/true_automation/driver/capybara.rb, line 157
def find_available_port(host)
  server = TCPServer.new(host, rand(9515..65515))
  server.addr[1]
ensure
  server&.close
end