Class: Quke::Configuration

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

Overview

Manages all configuration for Quke.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

When an instance is initialized it will automatically populate itself by calling a private method default_data().



59
60
61
62
63
64
65
# File 'lib/quke/configuration.rb', line 59

def initialize
  @data = default_data!(load_yml_data)
  # Order is important. @browserstack relies on @proxy being set
  @proxy = ::Quke::ProxyConfiguration.new(@data["proxy"] || {})
  @browserstack = ::Quke::BrowserstackConfiguration.new(self)
  @parallel = ::Quke::ParallelConfiguration.new(self)
end

Class Attribute Details

.file_locationObject

Returns the expected root location of where Quke expects to find the the config file.



47
48
49
# File 'lib/quke/configuration.rb', line 47

def self.file_location
  @file_location ||= "#{Dir.pwd}/#{file_name}"
end

Instance Attribute Details

#browserstackObject (readonly)

Instance of Quke::BrowserstackConfiguration which manages reading and returning the config for setting up Quke to use browserstack.



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

def browserstack
  @browserstack
end

#dataObject (readonly)

Access the loaded config data object directly.



15
16
17
# File 'lib/quke/configuration.rb', line 15

def data
  @data
end

#file_locationObject (readonly)

Access where the config file was loaded from for this instance of Quke::Configuration.



12
13
14
# File 'lib/quke/configuration.rb', line 12

def file_location
  @file_location
end

#parallelObject (readonly)

Instance of Quke::ParallelConfiguration which manages reading and returning the config for setting up Quke to use parallel tests.

The instance will be populated based on what was set in the config.yml merged with default values.

These values will then tell Quke whether to run tests in parallel, and if so how to setup the runs.



29
30
31
# File 'lib/quke/configuration.rb', line 29

def parallel
  @parallel
end

#proxyObject (readonly)

Instance of Quke::ProxyConfiguration which manages reading and returning the config for setting up Quke to use proxy server.



33
34
35
# File 'lib/quke/configuration.rb', line 33

def proxy
  @proxy
end

Class Method Details

.file_nameObject

Return the file name for the config file, either as set by the user in an environment variable called ‘QCONFIG` or the default of .config.yml.



53
54
55
# File 'lib/quke/configuration.rb', line 53

def self.file_name
  ENV["QUKE_CONFIG"] || ".config.yml"
end

Instance Method Details

#app_hostObject

Returns the value set for app_host.

Normally Capybara expects to be testing an in-process Rack application, but Quke is designed to talk to a remote host. Users of Quke can set what this will be by setting app_host in their .config.yml file. Capybara will then combine this with links you provide.

visit('/Main_Page')
visit('/')

This saves you from having to repeat the full url each time.



87
88
89
# File 'lib/quke/configuration.rb', line 87

def app_host
  @data["app_host"]
end

#cucumber_arg(additional_args) ⇒ Object

Returns a string representing the agruments that are passed to Cucumber by ParallelTests when it creates a new process and executes.

Specifically its the value for ParallelTests’ ‘–test-options` argument which will be used when generating the array of args to be passed to ParallelTests. It returns just a string rather than an array because ParallelTests needs to see this as a single argument.

The additional args are whatever a user enters after the ‘bundle exec quke` command.



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/quke/configuration.rb', line 221

def cucumber_arg(additional_args)
  # Because cucumber is called in the context of the executing project and
  # not Quke it will take its arguments in the context of that location, and
  # not from where the Quke currently sits. This means to Cucumber
  # 'lib/features' doesn't exist, which means our env.rb never gets loaded.
  # Instead we first have to determine where this file is running from when
  # called, then we simply replace the last part of that result (which we
  # know will be lib/quke) with lib/features. For example __dir__ returns
  # '/Users/acruikshanks/projects/defra/quke/lib/quke' but we need Cucumber
  # to load '/Users/acruikshanks/projects/defra/quke/lib/features'
  # We then pass this full path to Cucumber so it can correctly find the
  # folder holding our predefined env.rb file.
  env_folder = __dir__.sub!("lib/quke", "lib/features")
  fail_fast = "--fail-fast" if stop_on_error
  print_format = print_progress ? "progress" : "pretty"
  "#{fail_fast} --format #{print_format} -r #{env_folder} -r #{features_folder} #{additional_args.join(' ')}".strip
end

#customObject

Return the hash of custom server settings

This returns a hash of all the key/values in the custom section of your .config.yml file. You can then access it in your steps/page objects with

Quke::Quke.config.custom["default_org_name"] # = "Testy Ltd"
Quke::Quke.config.custom["accounts"]["account2"]["username"] # = "[email protected]"
Quke::Quke.config.custom["urls"]["front_office"] # = "http://myservice.service.gov.uk"

As long as what you add is valid YAML (check with a tool like www.yamllint.com/) Quke will be able to pick it up and make it available in your tests.



207
208
209
# File 'lib/quke/configuration.rb', line 207

def custom
  @data["custom"]
end

#display_failuresObject

Returns the value set for display_failures.

Tells Quke not to display the html for the last page when a failure happens. Quke uses Capybara to save a copy of the page as html and uses launchy to display it in the default browser.



134
135
136
# File 'lib/quke/configuration.rb', line 134

def display_failures
  @data["display_failures"]
end

#display_failures?Boolean

Returns whether failures should be displayed.

If the browser is headless then we never display failures, even if the setting has been set to true. Else whether we display a failure is based on the value set for display_failures.

Returns:

  • (Boolean)


143
144
145
146
147
# File 'lib/quke/configuration.rb', line 143

def display_failures?
  return false if headless

  display_failures
end

#driverObject

Returns the value set for driver.

Tells Quke which browser to use for testing. Choices are firefox, chrome browserstack and phantomjs, with the default being phantomjs.



95
96
97
# File 'lib/quke/configuration.rb', line 95

def driver
  @data["driver"]
end

#features_folderObject

Returns the value set for features_folder.

This will be passed to Cucumber by Quke when it executes the tests. It tells Cucumber where the main features folder which contains the tests is located. If not set in the .config.yml file it defaults to ‘features’.



72
73
74
# File 'lib/quke/configuration.rb', line 72

def features_folder
  @data["features_folder"]
end

#headlessObject

Returns the value set for headless.

Tells Quke whether to drive the browser in headless mode. Only applicable if driver is set to ‘chrome’ or ‘firefox’.



103
104
105
# File 'lib/quke/configuration.rb', line 103

def headless
  @data["headless"]
end

#javascript_errorsObject

Return the value set for javascript_errors.

Currently only supported when using the phantomjs driver (ignored by the others). In phantomjs if a site has a javascript error we can configure it to throw an error which will cause the test to fail. Quke by default sets this to true, however you can override it by setting this flag to false. For example you may be dealing with a legacy site and JavaScript errors are out of your scope. You still want to test other aspects of the site but not let these errors prevent you from using phantomjs.



182
183
184
# File 'lib/quke/configuration.rb', line 182

def javascript_errors
  @data["javascript_errors"]
end

#max_wait_timeObject

Return the value for max_wait_time

max_wait_time is the time Capybara will spend waiting for an element to appear. It’s default is normally 2 seconds but you may want to increase this is you are having to deal with a site that is not performant or prone to delays.

If the value is not set in config file, it will default to whatever is the current Capybara value for default_max_wait_time.



158
159
160
# File 'lib/quke/configuration.rb', line 158

def max_wait_time
  @data["max_wait_time"]
end

#pauseObject

Return the value set for pause.

Add a pause (in seconds) between steps so you can visually track how the browser is responding. Only useful if using a non-headless browser. The default is 0.



112
113
114
# File 'lib/quke/configuration.rb', line 112

def pause
  @data["pause"]
end

Returns the value set for print_progress.

If set Quke will tell Cucumber to output to the console using its ‘progress’ formatter, rather than the default ‘pretty’ which displays each scenario in detail.



191
192
193
# File 'lib/quke/configuration.rb', line 191

def print_progress
  @data["print_progress"]
end

#stop_on_errorObject

Return the value set for stop_on_error.

Specify whether Quke should stop all tests once an error occurs. Useful in Continuous Integration (CI) environments where a quick Yes/No is preferable to a detailed response.



121
122
123
124
125
126
127
# File 'lib/quke/configuration.rb', line 121

def stop_on_error
  # This use of Yaml.load to convert a string to a boolean comes from
  # http://stackoverflow.com/a/21804027/6117745
  # rubocop:disable Security/YAMLLoad
  YAML.load(@data["stop_on_error"])
  # rubocop:enable Security/YAMLLoad
end

#user_agentObject

Return the value set for user_agent.

Useful if you want the underlying driver to spoof what kind of browser the request is coming from. For example you may want to pretend to be a mobile browser so you can check what you get back versus the desktop version. Or you want to pretend to be another kind of browser, because the one you have is not supported by the site.



169
170
171
# File 'lib/quke/configuration.rb', line 169

def user_agent
  @data["user_agent"]
end