Class: Sauce::JSUnit::SeleniumConfig

Inherits:
Object
  • Object
show all
Includes:
Utilities
Defined in:
lib/sauce/jsunit/selenium_config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utilities

#debug, #diagnostics_prefix, #find_unused_port, #kill_mongrel_if_needed, #raise_with_message, #say, #setup_tunnel, #start_mongrel, #teardown_tunnel

Constructor Details

#initialize(configuration_name = nil, selenium_yml_path = nil) ⇒ SeleniumConfig

Returns a new instance of SeleniumConfig.



9
10
11
12
13
# File 'lib/sauce/jsunit/selenium_config.rb', line 9

def initialize(configuration_name = nil, selenium_yml_path = nil)
  selenium_yml_path = selenium_yml_path || File.join(ENV['RAILS_ROOT'] || RAILS_ROOT, 'config', 'selenium.yml')
  SeleniumConfig.parse_yaml(selenium_yml_path)
  build_configuration(configuration_name)
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



7
8
9
# File 'lib/sauce/jsunit/selenium_config.rb', line 7

def configuration
  @configuration
end

Class Method Details

.parse_yaml(selenium_yml_path) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/sauce/jsunit/selenium_config.rb', line 111

def self.parse_yaml(selenium_yml_path)
  raise "[saucelabs-adapter] could not open #{selenium_yml_path}" unless File.exist?(selenium_yml_path)
  file_contents = File.open(selenium_yml_path).read
  erb_parsed_file_contents = ERB.new(%{#{file_contents}}).result
    configs = YAML.load(erb_parsed_file_contents)
    @@selenium_configs ||= configs
end

Instance Method Details

#[]=(attribute, value) ⇒ Object



15
16
17
# File 'lib/sauce/jsunit/selenium_config.rb', line 15

def []=(attribute, value)
  @configuration[attribute.to_s] = value
end

#application_addressObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sauce/jsunit/selenium_config.rb', line 49

def application_address
  if start_tunnel? &&
    [:sauceconnecttunnel, :saucetunnel].include?(@configuration['tunnel_method'].to_sym)
    # We are using Sauce Labs and Sauce Connect Tunnel or Sauce Tunnel.
    # We need to use a masquerade hostname on the EC2/Sauce end of the tunnel that will be unique within the scope of
    # this account (e.g. pivotallabs).  Therefore we mint a fairly unique hostname here.
    hostname = Socket.gethostname.split(".").first
    "#{hostname}-#{Process.pid}.com"
  else
    @configuration['application_address']
  end

end

#configure_polonium(polonium_configuration_object) ⇒ Object

Takes a Polonium::Configuration object and configures it by calling methods on it



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sauce/jsunit/selenium_config.rb', line 81

def configure_polonium(polonium_configuration_object)
  {
    'selenium_server_host'      => :selenium_server_address,
    'selenium_server_port'      => :selenium_server_port,
    'browser'                   => :selenium_browser_key,
    'external_app_server_host'  => :application_address,
    'external_app_server_port'  => :application_port
  }.each do |polonium_configuration_method, our_accessor|
    polonium_configuration_object.send("#{polonium_configuration_method}=", self.send(our_accessor).to_s)
  end
end

#configure_webrat(webrat_configuration_object) ⇒ Object

Takes a Webrat::Configuration object and configures it by calling methods on it



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/sauce/jsunit/selenium_config.rb', line 64

def configure_webrat(webrat_configuration_object)
  # NOTE: application_port_for_selenium requires version > 0.7.3 of webrat
  # Prior versions only have application_address, and don't have a concept of
  # starting a rails server at one port, and hitting it at selenium via another
  {
    'selenium_server_address' => :selenium_server_address,
    'selenium_server_port'    => :selenium_server_port,
    'selenium_browser_key'    => :selenium_browser_key,
    'application_address'     => :application_address,
    'application_port_for_selenium' => start_tunnel? ? :tunnel_to_localhost_port : :application_port,
    'application_port'        => :application_port
  }.each do |webrat_configuration_method, our_accessor|
    webrat_configuration_object.send("#{webrat_configuration_method}=", self.send(our_accessor).to_s)
  end
end

#create_driver(selenium_args = {}) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/sauce/jsunit/selenium_config.rb', line 93

def create_driver(selenium_args = {})
  args = selenium_client_driver_args.merge(selenium_args)
  say "Connecting to Selenium RC server at #{args[:host]}:#{args[:port]} (testing app at #{args[:url]})" if ENV['SAUCELABS_ADAPTER_DEBUG']
  say "args = #{display_safely(args)}" if ENV['SAUCELABS_ADAPTER_DEBUG']
  driver = ::Selenium::Client::Driver.new(args)
  debug "done"
  driver
end

#kill_mongrel_after_suite?Boolean

Returns:

  • (Boolean)


106
107
108
109
# File 'lib/sauce/jsunit/selenium_config.rb', line 106

def kill_mongrel_after_suite?
  return true if kill_mongrel_after_suite.nil?
  kill_mongrel_after_suite.to_s == 'true'
end

#selenium_browser_keyObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sauce/jsunit/selenium_config.rb', line 33

def selenium_browser_key
  if selenium_server_address == 'saucelabs.com'
    # Create the JSON string that Saucelabs needs:
    { 'username' => saucelabs_username,
      'access-key' => saucelabs_access_key,
      'os' => saucelabs_browser_os,
      'browser' => saucelabs_browser,
      'browser-version' => saucelabs_browser_version,
      'max-duration' => saucelabs_max_duration_seconds.to_i,
      'job-name' => ENV['SAUCELABS_JOB_NAME'] || Socket.gethostname
    }.to_json
  else
    @configuration['selenium_browser_key']
  end
end

#start_tunnel?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/sauce/jsunit/selenium_config.rb', line 102

def start_tunnel?
  !tunnel_method.nil? && tunnel_method.to_sym != :othertunnel
end