Module: SaucelabsAdapter::Utilities

Included in:
JsunitSeleniumSupport, SauceConnectTunnel, SauceTunnel, SeleniumConfig, SshTunnel, Test::Unit::UI::Console::TestRunner
Defined in:
lib/saucelabs_adapter/utilities.rb

Instance Method Summary collapse

Instance Method Details

#debug(what, print_if_level_ge = 0) ⇒ Object



12
13
14
15
16
17
# File 'lib/saucelabs_adapter/utilities.rb', line 12

def debug(what, print_if_level_ge = 0)
  if ENV['SAUCELABS_ADAPTER_DEBUG']
    actual_level = ENV['SAUCELABS_ADAPTER_DEBUG'].to_i
    STDOUT.puts "#{diagnostics_prefix}   #{what}" if print_if_level_ge >= actual_level
  end
end

#diagnostics_prefixObject



4
5
6
# File 'lib/saucelabs_adapter/utilities.rb', line 4

def diagnostics_prefix
  @diagnostics_prefix ||= '[saucelabs-adapter]'
end

#find_unused_port(hostname, range = (3000..5000)) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/saucelabs_adapter/utilities.rb', line 23

def find_unused_port(hostname, range = (3000..5000))
  debug 'searching for unused port', 2
  range.each do |port|
    debug "trying #{hostname}:#{port}", 2
    begin
      socket = TCPSocket.new(hostname, port)
    rescue Errno::ECONNREFUSED
      debug "it's good, returning #{port}", 2
      return port
    ensure
      socket.close if socket
    end
  end
end

#kill_mongrel_if_needed(suite_name = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/saucelabs_adapter/utilities.rb', line 46

def kill_mongrel_if_needed(suite_name = {})
  mongrel_pid_file = File.join(RAILS_ROOT, "tmp", "pids", "mongrel_selenium.pid")
  if File.exists?(mongrel_pid_file)
    pid = File.read(mongrel_pid_file).to_i
    say "Killing mongrel at #{pid}"
    Process.kill("KILL", pid)
  end
  if File.exists?(mongrel_pid_file)
    FileUtils.rm(mongrel_pid_file)
  end
end

#raise_with_message(message) ⇒ Object



19
20
21
# File 'lib/saucelabs_adapter/utilities.rb', line 19

def raise_with_message(message)
  raise "#{diagnostics_prefix} #{message}"
end

#say(what) ⇒ Object



8
9
10
# File 'lib/saucelabs_adapter/utilities.rb', line 8

def say(what)
  STDOUT.puts "#{diagnostics_prefix} #{what}"
end

#setup_tunnel(suite_name = {}) ⇒ Object



58
59
60
61
# File 'lib/saucelabs_adapter/utilities.rb', line 58

def setup_tunnel(suite_name = {})
  @tunnel = SaucelabsAdapter::Tunnel.factory(@selenium_config)
  @tunnel.start_tunnel
end

#start_mongrel(suite_name = {}) ⇒ Object

parameters required when invoked by test_unit



39
40
41
42
43
44
# File 'lib/saucelabs_adapter/utilities.rb', line 39

def start_mongrel(suite_name = {})
  pid_file = File.join(RAILS_ROOT, "tmp", "pids", "mongrel_selenium.pid")
  port = suite_name[:port] rescue @selenium_config.application_port
  say "Starting mongrel at #{pid_file}, port #{port}"
  system "mongrel_rails start -d --chdir='#{RAILS_ROOT}' --port=#{port} --environment=test --pid #{pid_file} %"
end

#teardown_tunnel(suite_name = {}) ⇒ Object



63
64
65
# File 'lib/saucelabs_adapter/utilities.rb', line 63

def teardown_tunnel(suite_name = {})
  @tunnel.shutdown
end