Class: Quke::DriverConfiguration

Inherits:
Object
  • Object
show all
Defined in:
lib/quke/driver_configuration.rb

Overview

Helper class that manages the options, switches and capabilities for each of the different drivers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ DriverConfiguration

Instantiate an instance of Quke::DriverConfiguration.

It expects an instance of Quke::Configuration which will be used internally to determine how to set the options it passes back to each of the drivers. to be used and any related options



19
20
21
# File 'lib/quke/driver_configuration.rb', line 19

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Access the instance of Quke::Configuration passed to this instance of Quke::DriverOptions when it was initialized.



11
12
13
# File 'lib/quke/driver_configuration.rb', line 11

def config
  @config
end

Instance Method Details

#browserstackObject

Returns an instance of Selenium::WebDriver::Remote::Capabilities to be used when registering an instance of Capybara::Selenium::Driver, configured to run using the Browserstack service.

For example when initialising the driver like this

my_capabilites = Selenium::WebDriver::Remote::Capabilities.new
my_capabilites['build'] = my_config.browserstack['build']
# ... set rest of capabilities
Capybara::Selenium::Driver.new(
  app,
  browser: :remote,
  url: 'http://jdoe:[email protected]/wd/hub',
  desired_capabilities: my_capabilites
)

You can instead call Quke::DriverConfiguration.browserstack which will manage instantiating and setting up the Selenium::WebDriver::Remote::Capabilities instance based on the properties of the Quke::Configuration instance its initialised with

Capybara::Selenium::Driver.new(
  app,
  browser: :remote,
  url: my_driver_config.browserstack_url,
  desired_capabilities: my_driver_config.browserstack
)

For further reference on browserstack capabilities www.browserstack.com/automate/capabilities www.browserstack.com/automate/ruby#configure-capabilities



237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/quke/driver_configuration.rb', line 237

def browserstack
  # Documentation and the code for this class can be found here
  # http://www.rubydoc.info/gems/selenium-webdriver/0.0.28/Selenium/WebDriver/Remote/Capabilities
  # https://github.com/SeleniumHQ/selenium/blob/master/rb/lib/selenium/webdriver/remote/capabilities.rb
  capabilities = Selenium::WebDriver::Remote::Capabilities.new

  config.browserstack.capabilities.each do |key, value|
    capabilities[key] = value
  end

  capabilities
end

#chromeObject

Returns an array to be used in conjunction with the :switches argument when initialising a Capybara::Selenium::Driver set for Chrome.

For example when initialising the driver like this

Capybara::Selenium::Driver.new(
  app,
  browser: :chrome,
  switches: [
    "--proxy-server=localhost:8080",
    "--proxy-bypass-list=127.0.0.1,192.168.0.1",
    "--user-agent=Mozilla/5.0 (MSIE 10.0; Windows NT 6.1; Trident/5.0)"
  ]
)

Rather than setting the switches manually Quke::DriverConfiguration.chrome is intended to manage what they should be based on the properties of the Quke::Configuration instance its initialised with

Capybara::Selenium::Driver.new(
  app,
  browser: :chrome,
  switches: my_driver_config.chrome
)

rubocop:disable Metrics/AbcSize



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/quke/driver_configuration.rb', line 144

def chrome
  result = []

  host = config.proxy['host']
  port = config.proxy['port']
  no_proxy = config.proxy['no_proxy'].tr(',', ';')

  result.push("--proxy-server=#{host}:#{port}") if config.use_proxy?
  result.push("--proxy-bypass-list=#{no_proxy}") unless config.proxy['no_proxy'].empty?

  result.push("--user-agent=#{config.user_agent}") unless config.user_agent.empty?

  result
end

#firefoxObject

Returns an instance of Selenium::WebDriver::Remote::Capabilities to be used when registering an instance of Capybara::Selenium::Driver, configured to run using Firefox (the default).

For example when initialising the driver like this

my_profile = Selenium::WebDriver::Firefox::Profile.new
my_profile.proxy = Selenium::WebDriver::Proxy.new(
  http: "10.10.2.70:8080",
  ssl: "10.10.2.70:8080"
)
my_profile['general.useragent.override'] = "Mozilla/5.0 (MSIE 10.0; Windows NT 6.1; Trident/5.0)"
Capybara::Selenium::Driver.new(
  app,
  profile: my_profile
)

You can instead call Quke::DriverConfiguration.firefox which will manage instantiating and setting up the Selenium::WebDriver::Firefox::Profile instance based on the properties of the Quke::Configuration instance its initialised with

Capybara::Selenium::Driver.new(
  app,
  profile: my_driver_config.firefox
)

rubocop:disable Metrics/AbcSize



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/quke/driver_configuration.rb', line 188

def firefox
  profile = Selenium::WebDriver::Firefox::Profile.new

  settings = {}

  settings[:http] = "#{config.proxy['host']}:#{config.proxy['port']}" if config.use_proxy?
  settings[:ssl] = settings[:http] if config.use_proxy?
  settings[:no_proxy] = config.proxy['no_proxy'] unless config.proxy['no_proxy'].empty?

  profile.proxy = Selenium::WebDriver::Proxy.new(settings) if config.use_proxy?

  profile['general.useragent.override'] = config.user_agent unless config.user_agent.empty?

  profile
end

#phantomjsObject

Returns an array used as part of the poltergeist settings, which are passed in when initialising a Capybara::Poltergeist::Driver.

For example when initialising the driver like this

Capybara::Poltergeist::Driver.new(app,
  {
    js_errors: true,
    timeout: 30,
    debug: false,
    phantomjs_options: [
      '--load-images=no',
      '--disk-cache=false',
      '--ignore-ssl-errors=yes',
      '--proxy=10.10.2.70:8080'
    ],
    inspector: true
  }
)

Rather than setting the phantomjs_options: manually Quke::DriverConfiguration.phantomjs is intended to manage what they should be based on the properties of the Quke::Configuration instance its initialised with

Capybara::Poltergeist::Driver.new(app,
  {
    js_errors: true,
    timeout: 30,
    debug: false,
    phantomjs_options: my_driver_config.phantomjs,
    inspector: true
  }
)


103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/quke/driver_configuration.rb', line 103

def phantomjs
  # For future reference the options we pass through to phantomjs appear to
  # mirror those you can actually supply on the command line.
  # http://phantomjs.org/api/command-line.html
  options = [
    '--load-images=no',
    '--disk-cache=false',
    '--ignore-ssl-errors=yes'
  ]

  options.push("--proxy=#{config.proxy['host']}:#{config.proxy['port']}") if config.use_proxy?

  options
end

#poltergeistObject

The hash returned from this method is intended to used when initialising an instance of Capybara::Poltergeist::Driver.

For example when initialising the driver like this

Capybara::Poltergeist::Driver.new(app,
  {
    js_errors: true,
    timeout: 30,
    debug: false,
    phantomjs_options: [
      '--load-images=no',
      '--disk-cache=false',
      '--ignore-ssl-errors=yes',
      '--proxy=10.10.2.70:8080'
    ],
    inspector: true
  }
)

Rather than setting the options manually Quke::DriverConfiguration.poltergeist is intended to manage what they should be based on the properties of the Quke::Configuration instance its initialised with

Capybara::Poltergeist::Driver.new(app, my_driver_config.poltergeist)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/quke/driver_configuration.rb', line 50

def poltergeist
  # The arguments we can pass to poltergeist are documented here
  # https://github.com/teampoltergeist/poltergeist#customization
  {
    # Javascript errors will get re-raised in our tests causing them to fail
    js_errors: config.javascript_errors,
    # How long in seconds we'll wait for response when communicating with
    # Phantomjs
    timeout: 30,
    # When true debug output will be logged to STDERR (a terminal thing!)
    debug: false,
    # Poltergeist can pass on options for configuring phantomjs
    phantomjs_options: phantomjs,
    # The internet told me to put this here (???)
    inspector: true
  }
end