Skip to end of metadata
Go to start of metadata

Sauce Labs is a cloud platform for executing automated and manual mobile and web tests. Sauce Labs supports running automated tests with Selenium WebDriver (for web applications) and Appium (for native and mobile web applications). This topic will provide you with basic information and code examples to get your Ruby tests up and running on Sauce.

Example Only

The code in this topic is presented as an example only, since your tests and testing environments may require specialized scripting. This information should be taken only as an illustration of how you could set up your tests with Sauce Labs, and is not directly supported by Sauce.

Prerequisites

Before getting started, you should read the Best Practices for Running Tests with Sauce.
  • Make sure you have the selenium-webdriver gem installed
  • If you want to run tests in parallel you will need the parallel_tests gem
  • You will also need to install the sauce_whisk gem to report the results of your test to your Sauce Labs dashboard

Quick Start

It's very easy to get your existing Ruby tests up and running on Sauce. 

If you wanted to run a test on Selenium locally, you would initiate a driver for the browser you want to test against like this.
Local Ruby Test Example
driver = Selenium::WebDriver.for :firefox 

To run an existing test on Sauce, all you need to change is your driver definition, and make sure that the sauce_endpoint variable includes your Sauce USERNAME and ACCESSKEY.

Remote Ruby Test on Sauce Example
driver = Selenium::WebDriver.for :remote, :url => sauce_endpoint, :desired_capabilities => caps
sauce_endpoint = "http://YOUR_SAUCE_USERNAME:YOUR_SAUCE_ACCESSKEY@ondemand.saucelabs.com:80/wd/hub"

Once you have your tests set up to run in the Sauce cloud, you need to define the platform, browser, and version you want the test to run against, which is where :desired_capabilites and caps come into play. 

caps = { :platform => "Mac OS X 10.9", :browserName => "Chrome", :version => "31" }

You can manually enter the values you want for :platform:browsername, and :version, or you can use the handy Platform Configurator to generate the caps values for any combination of platform, browser, and browser version. 


Code Sample

This code examples shows a very simple test to see if the title of Google's home page has changed, but it includes everything you need to set up and run an automated test on Sauce. 

You can also clone this script directly from our GitHub repo

require "selenium/webdriver"

caps = {
  :platform => "Windows 7",
  :browserName => "Chrome",
  :version => "45"
}

driver = Selenium::WebDriver.for(:remote,
	:url => "https://YOUR_SAUCE_USERNAME:YOUR_SAUCE_ACCESS_KEY@ondemand.saucelabs.com:443/wd/hub",
	:desired_capabilities => caps)

driver.get('http://saucelabs.com/test/guinea-pig')

puts "title of webpage is: #{driver.title()}"

driver.quit()

Running Local Tests

Developing websites/apps within your local network is secure and efficient. The drawback to this method is that local assets are not publicly-accessible on the Internet, so the browsers/devices in the Sauce Labs cloud can't load and test your app. The solution is to use Sauce ConnectSauce Connect is a proxy server that creates a secure tunnel connection between the Sauce Labs virtual machine that runs your test and your local  network. You can also use Sauce Connect to test applications and websites that are located within your corporate firewall. Sauce Connect is not required to run tests on Sauce Labs, only in situations where the application or website you want to test is not publicly accessible. 

Running Tests in Parallel

Now that you’re running tests on Sauce, you may wonder how you can run your tests faster. One way to increase speed is by running tests in parallel across multiple virtual machines.

You can run your tests in parallel at two levels, and you can run your tests in parallel across multiple browsers. For example, if you have 10 tests and want to run on five browsers, this would be parallelism of five. You can also run tests across browsers and each test in parallel. Using our previous example this would be more like 50 parallel tests. Doing this requires that your tests are written in a way that they do not collide with one another, as described in our Best Practice topics avoiding external test dependencies and avoiding dependencies between tests.

Match Thread Count to Concurrency Limit

You should match your thread count to your concurrency limit, which is shown in the My Account section of your user profile information on the Sauce Labs dashboard.  

For more information, check out the example scripts in our GitHub repo

Reporting on Test Results

Selenium is a protocol focused on automating the actions of a browser, and as such, it doesn't encompass the notions of a "test" that "passes" or "fails." Sauce Labs lets you notify us of test status using our REST API. All you need is the ID Sauce Labs gave the job, and the status your test ended with. Then, you can use the Update Job method on the REST API to set the job's status.

The Job ID is the simplest part of the process. The ID assigned to each job by Sauce Labs is the same as the Session ID for the corresponding Selenium session, which you can pull off the driver like so:

job_id = driver.session_id 

Using the REST API is best done with the sauce_whisk gem, which assumes you've set your Sauce username and access key as the SAUCE_USERNAME and SAUCE_ACCESS_KEY environment variables.

This example shows how you would modify the example RSpec spec_helper.rb file to check the status of the test, and pass that to the REST API.

Modifying spec_helper.rb to Report Test Results to the Dashboard Using the sauce-whisk Gem
require "sauce_whisk"

RSpec.configure do |config|
  config.around(:example, :run_on_sauce => true) do |example|
    @driver = SauceDriver.new_driver
    @job_id = @driver.session_id
    begin
      example.run
    ensure
      SauceWhisk::Jobs.change_status job_id, !example.exception.nil?
      @driver.quit
    end
  end
end 

That's it! Your tests will now be reporting their status to the Sauce Labs REST API.

1 Comment

  1. Anonymous

    Why aren't HTTPS endpoints used here?