saucelabs/sauce_ruby — Issue #21: Automatically start/stop Sauce-Connect.jar
Author: fotioslindiakos
Created: 2011-10-13 18:58:56+00:00
State: open

I wrote this wrapper into my helper and test suite files and thought it might be of use to some other folks:

```ruby
# helper.rb
# Add this in addition to your normal helper code
require 'net/http'
require 'zipruby'

ready_file = 'sauce_ready'
sauce_file = 'Sauce-Connect.jar'

unless File.exists?(sauce_file)
puts "Fetching Sauce-Connect.jar"
url = "http://saucelabs.com/downloads/Sauce-Connect-latest.zip"

Zip::Archive.open_buffer(Net::HTTP.get(URI.parse(url))) do |zf|
zf.fopen(sauce_file) do |f|
open(sauce_file,'wb') do |file|
file.write(f.read)
end
end
end
end

# Get the Sauce configuration information
cfg = Sauce::Config.new()
# Create a command to spawn the jar file
cmd = "java -jar #{sauce_file} -f #{ready_file} #{cfg.opts[:username]} #{cfg.opts[:access_key]}"

# Run the command
`#{cmd} > /dev/null 2>&1 &`
@sauce_connect_pid = $?.pid
puts "Connect PID: #{@sauce_connect_pid}"

# Wait for Sauce to be ready
until File.exists?(ready_file)
puts "Waiting for Sauce Connect"
sleep 5
end
```

```ruby
#testsuite.rb
require "test/unit"
require "test/unit/testsuite"
require "test/unit/ui/console/testrunner"
require 'helper'
require 'tc_home'
require 'tc_ajax'

# Need to load the tests manualls so we can
# override at_exit
class TS_web
def self.suite
suite = Test::Unit::TestSuite.new('Sauce Tests')
suite << AJAX.suite
suite << Home.suite
return suite
end
end

# Need to do this for now in order to kill the
# process when we're done
Test::Unit::UI::Console::TestRunner.run(TS_web);

# HACK: pid is of the `cmd`, which spawns java
at_exit{ Process.kill('TERM',@sauce_connect_pid+1);}
```