Class: Sauce::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/sauce/config.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
    :host => "ondemand.saucelabs.com",
    :port => 80,
    :browser_url => "http://saucelabs.com",
    :job_name => "Unnamed Ruby job",
    :start_tunnel => true,
    :start_local_application => true,
    :warn_on_skipped_integration => true,
    :skip_connection_test => false,
    :suppress_session_quit_failures => false
}
DEFAULT_BROWSERS =
{
    :browsers => [
      ["Windows 8", "Internet Explorer", "10"],
      ["Windows 7", "Firefox", "20"],
      ["OS X 10.8", "Safari", "6"],
      ["Linux", "Chrome", nil]
    ]
}
POTENTIAL_PORTS =
[
    3000, 3001, 3030, 3210, 3333, 4000, 4001, 4040, 4321, 4502, 4503, 5000,
    5001, 5050, 5555, 5432, 6000, 6001, 6060, 6666, 6543, 7000, 7070, 7774,
    7777, 8000, 8001, 8003, 8031, 8080, 8081, 8765, 8888, 9000, 9001, 9080,
    9090, 9876, 9999, 49221, 55001, 80, 443, 888, 2000, 2001, 2020, 2109,
    2222, 2310
]
ENVIRONMENT_VARIABLES =
%w{SAUCE_HOST SAUCE_PORT SAUCE_BROWSER_URL SAUCE_USERNAME
SAUCE_ACCESS_KEY SAUCE_OS SAUCE_BROWSER SAUCE_BROWSER_VERSION SAUCE_JOB_NAME
SAUCE_FIREFOX_PROFILE_URL SAUCE_USER_EXTENSIONS_URL
SAUCE_ONDEMAND_BROWSERS SAUCE_USER_NAME SAUCE_API_KEY}
PLATFORMS =
{
  "Windows 2003" => "WINDOWS",
  "Windows 2008" => "VISTA",
  "Linux" => "LINUX"
}
BROWSERS =
{
  "iexplore" => "internet explorer",
  "ie" => "internet explorer"
}
SAUCE_OPTIONS =
%w{record-video record-screenshots capture-html tags
    sauce-advisor single-window user-extensions-url firefox-profile-url
    max-duration idle-timeout build custom-data tunnel-identifier
    selenium-version command-timeout prerun prerun-args screen-resolution
    disable-popup-handler avoid-proxy public name iedriver-version parent-tunnel
    time-zone device-orientation record-logs webdriver-remote-quiet-exceptions
    chromedriver-version chromeOptions
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Sauce::Config

Creates a new instance of Sauce::Config

Parameters:

  • opts (Hash, Boolean) (defaults to: {})

    Any value you’d set with [:option], as a hash. If false, skip loading default options

Options Hash (opts):

  • :without_defaults (Boolean)

    Set true to skip loading default values



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/sauce/config.rb', line 98

def initialize(opts={})
  @opts = {}
  @undefaulted_opts = {}
  if opts != false
    if (!opts[:without_defaults]) 
      @opts.merge! DEFAULT_OPTIONS
      @opts.merge! DEFAULT_BROWSERS
      @opts.merge!({:application_port => Sauce::Config.get_application_port})

      @undefaulted_opts.merge! load_options_from_yaml
      @undefaulted_opts.merge! load_options_from_environment
      @undefaulted_opts.merge! load_options_from_heroku unless ENV["SAUCE_DISABLE_HEROKU_CONFIG"]
      
      global_config = Sauce.get_config
      @undefaulted_opts.merge! global_config.opts if global_config.opts
      @whitelisted_capabilities = global_config.whitelisted_capabilities
    end

    @undefaulted_opts.merge! opts
    @opts.merge! @undefaulted_opts
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/sauce/config.rb', line 141

def method_missing(meth, *args)
  unless self.silence_warnings
    warn "[DEPRECATED] This method (#{meth}) is deprecated, please use the [] and []= accessors instead"
  end
  if meth.to_s =~ /(.*)=$/
    self[$1.to_sym] = args[0]
    return args[0]
  elsif meth.to_s =~ /(.*)\?$/
    return self[$1.to_sym]
  else
    return self[meth]
  end
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



21
22
23
# File 'lib/sauce/config.rb', line 21

def opts
  @opts
end

Class Method Details

.called_from_integrationsObject



87
88
89
90
# File 'lib/sauce/config.rb', line 87

def self.called_from_integrations
  Sauce.logger.debug "Sauce config accessed from an integration"
  @called_from_integrations = true
end

.called_from_integrations?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/sauce/config.rb', line 83

def self.called_from_integrations?
  @called_from_integrations || false
end

.get_application_portObject



77
78
79
80
81
# File 'lib/sauce/config.rb', line 77

def self.get_application_port
  port_index = ENV["TEST_ENV_NUMBER"].to_i
  port_to_use = POTENTIAL_PORTS[port_index]
  return port_to_use
end

Instance Method Details

#[](key) ⇒ Object



121
122
123
# File 'lib/sauce/config.rb', line 121

def [](key)
  @opts[key]
end

#[]=(key, value) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/sauce/config.rb', line 125

def []=(key, value)
  if(key == :browsers)
    value = [value] unless value.first.instance_of?(Array)
  end
  @undefaulted_opts.merge!({key => value})
  @opts[key] = value
end

#access_keyObject



287
288
289
# File 'lib/sauce/config.rb', line 287

def access_key
  @opts[:access_key]
end

#after_job(hook, &block) ⇒ Object



299
300
301
302
303
# File 'lib/sauce/config.rb', line 299

def after_job(hook, &block)
  hooks = @opts[:after_job_hooks] || {}
  hooks[hook] = block unless hooks[hook]
  @opts[:after_job_hooks] = hooks
end

#browserObject



241
242
243
244
245
246
247
248
249
250
# File 'lib/sauce/config.rb', line 241

def browser
  if single_browser_set?
    return @undefaulted_opts[:browser]
  end
  if !ENV["TEST_ENV_NUMBER"] && @opts[:browsers]
    @opts[:browsers][0][1]
  else
    raise StandardError, no_browser_message
  end
end

#browser_versionObject



263
264
265
266
267
268
269
270
271
272
# File 'lib/sauce/config.rb', line 263

def browser_version
  if single_browser_set?
    return @undefaulted_opts[:browser_version] || @undefaulted_opts[:version]
  end
  if !ENV["TEST_ENV_NUMBER"] && @opts[:browsers]
    @opts[:browsers][0][2]
  else
    @opts[:browser_version]
  end
end

#browsersObject



207
208
209
210
211
212
213
214
215
216
217
# File 'lib/sauce/config.rb', line 207

def browsers
  if @undefaulted_opts[:browser]
    # If a specific browser was requested, ignore :browsers and
    # use that one. This allows a setup with :browsers to launch
    # sub-processes pointed just at each browser in the list.
    return [[os, browser, browser_version]]
  end

  return @opts[:browsers] if @opts.include? :browsers
  return [[os, browser, browser_version]]
end

#caps_for_location(file, linenumber = nil) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/sauce/config.rb', line 219

def caps_for_location(file, linenumber=nil)
  Sauce.logger.debug "Finding capabilities for #{file}"
  Sauce.logger.debug "  Line #{linenumber}" unless linenumber.nil?
  Sauce::Config.called_from_integrations
  perfile_browsers = @opts[:perfile_browsers]
  
  if perfile_browsers
    platforms = []
    test_location = "#{file}:#{linenumber}"
    if linenumber && (perfile_browsers.include? test_location)
      platforms =  perfile_browsers[test_location]
    else
      platforms = perfile_browsers[file]
    end
    Sauce.logger.debug "  Found - #{platforms}"
    platforms.map { |p| [p['os'], p['browser'], p['version'], (p['caps'] || {})] }
  else
    Sauce.logger.debug "No perfile browsers; Using browsers array instead."
    browsers
  end
end

#domainObject



274
275
276
277
# File 'lib/sauce/config.rb', line 274

def domain
  return @opts[:domain] if @opts.include? :domain
  return URI.parse(@opts[:browser_url]).host
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/sauce/config.rb', line 133

def has_key?(key)
  @opts.has_key? key
end

#hostObject



291
292
293
# File 'lib/sauce/config.rb', line 291

def host
  @opts[:host]
end

#is_defined?(top_mod, sub_mod = nil) ⇒ Boolean

Only here to be stubbed for testing. Gross.

Returns:

  • (Boolean)


325
326
327
328
329
330
331
332
# File 'lib/sauce/config.rb', line 325

def is_defined? (top_mod, sub_mod = nil)
  return_value = Object.const_defined? top_mod
  unless !return_value || sub_mod.nil?
    return_value = Object.const_get(top_mod).const_defined? sub_mod
  end

  return_value
end

#local?Boolean

Returns:

  • (Boolean)


279
280
281
# File 'lib/sauce/config.rb', line 279

def local?
  return ENV['LOCAL_SELENIUM']
end

#osObject



252
253
254
255
256
257
258
259
260
261
# File 'lib/sauce/config.rb', line 252

def os
  if single_browser_set?
    return @undefaulted_opts[:os]
  end
  if !ENV["TEST_ENV_NUMBER"] && @opts[:browsers]
    @opts[:browsers][0][0]
  else
    @opts[:os]
  end
end

#portObject



295
296
297
# File 'lib/sauce/config.rb', line 295

def port
  @opts[:port]
end

#run_post_job_hooks(job_id, platform, job_name, job_success) ⇒ Object



305
306
307
308
309
310
311
312
313
# File 'lib/sauce/config.rb', line 305

def run_post_job_hooks(job_id, platform, job_name, job_success)
  Sauce.logger.debug "Running post job hook for #{job_id}"
  Sauce.logger.debug " - Job name #{job_name}"
  Sauce.logger.debug " - Successful? #{job_success}"
  @opts[:after_job_hooks].each do |key, hook|
    Sauce.logger.debug "Running #{hook}"
    hook.call job_id, platform, job_name, job_success
  end
end

#silence_warningsObject



137
138
139
# File 'lib/sauce/config.rb', line 137

def silence_warnings
  false
end

#to_browser_stringObject



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/sauce/config.rb', line 166

def to_browser_string
  browser_options = {
    'username' => @opts[:username],
    'access-key' => @opts[:access_key],
    'os' => os,
    'browser' => browser,
    'browser-version' => browser_version,
    'name' => @opts[:name] || @opts[:job_name]}

  SAUCE_OPTIONS.each do |opt|
    [opt, opt.gsub("-", "_")].map(&:to_sym).each do |sym|
      browser_options[opt] = @opts[sym] if @opts.include? sym
    end
  end
  return browser_options.to_json
end

#to_desired_capabilitiesObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/sauce/config.rb', line 183

def to_desired_capabilities
  desired_capabilities = {
    :browserName => BROWSERS[browser] || browser,
    :version => browser_version,
    :platform => PLATFORMS[os] || os,
    :name =>@opts[:job_name],
    :client_version => client_version
  }

  allowed_options = whitelisted_capabilities + SAUCE_OPTIONS

  allowed_options.each do |opt|
    [opt, opt.gsub("-", "_")].map(&:to_sym).each do |sym|
      if @opts.include? sym
        desired_capabilities[opt.to_sym] = @opts[sym]
      elsif @opts.include? sym.to_s
        desired_capabilities[opt.to_sym] = @opts[sym.to_s]
      end
    end
  end

  desired_capabilities
end

#toolsObject



315
316
317
318
319
320
321
322
# File 'lib/sauce/config.rb', line 315

def tools
  tools = []
  tools << "Rspec" if is_defined? "RSpec"
  tools << "Capybara" if is_defined? "Capybara"
  tools << "Cucumber" if is_defined? "Cucumber"
  tools << "Test::Unit" if is_defined?("Test", "Unit")
  tools
end

#usernameObject



283
284
285
# File 'lib/sauce/config.rb', line 283

def username
  @opts[:username]
end

#whitelist(capability) ⇒ Object



159
160
161
162
163
164
# File 'lib/sauce/config.rb', line 159

def whitelist capability
  Sauce.logger.info "Whitelisting #{capability}"
  cap = capability.to_s
  wl = whitelisted_capabilities || Set.new
  @whitelisted_capabilities = wl.add cap
end

#whitelisted_capabilitiesObject



155
156
157
# File 'lib/sauce/config.rb', line 155

def whitelisted_capabilities
  @whitelisted_capabilities ||= Set.new 
end