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



21
22
23
# File 'lib/quke/driver_configuration.rb', line 21

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.



13
14
15
# File 'lib/quke/driver_configuration.rb', line 13

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



231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/quke/driver_configuration.rb', line 231

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 instance of Selenium::WebDriver::Chrome::Options to be used when registering an instance of Capybara::Selenium::Driver, configured to run using Chrome.

For example when initialising the driver like this

args = [
  "--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)"
]

Capybara::Selenium::Driver.new(
  app,
  browser: :chrome,
  options: Selenium::WebDriver::Firefox::Options.new(args: args)
)

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

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


149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/quke/driver_configuration.rb', line 149

def chrome
  no_proxy = config.proxy.no_proxy.tr(",", ";")

  options = Selenium::WebDriver::Chrome::Options.new
  options.headless! if config.headless

  options.add_argument("--proxy-server=#{config.proxy.host}:#{config.proxy.port}") if config.proxy.use_proxy?
  options.add_argument("--proxy-bypass-list=#{no_proxy}") unless config.proxy.no_proxy.empty?

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

  options
end

#firefoxObject

Returns an instance of Selenium::WebDriver::Firefox::Options 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,
  browser: :firefox,
  options: Selenium::WebDriver::Firefox::Options.new(profile: my_profile)
)

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

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


192
193
194
195
196
197
# File 'lib/quke/driver_configuration.rb', line 192

def firefox
  options = Selenium::WebDriver::Firefox::Options.new(profile: firefox_profile)
  options.headless! if config.headless

  options
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
  }
)


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

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.proxy.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)


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

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