Skip to main content

_ruby

Example

NOTE: You'll need an account to use Sauce Labs. You can sign up for a free trial account (no credit-card required).

First we'll include our dependent libraries (to drive the browser and do assertions), and wire up some simple setup, teardown, and run methods. We recommend exporting your Sauce Labs SAUCE_USERNAME and SAUCE_ACCESS_KEY as environment variables.

Here are instructions for setting environment variables on each Operating System:

# filename: cloud.rb

require 'selenium-webdriver'
require 'rspec/expectations'
include RSpec::Matchers

def setup
options = Selenium::WebDriver::Options.chrome
options.browser_version = '109'
options.platform_name = 'macOS 13'
sauce_options = {}
sauce_options[:name] = 'Elemental Selenium - Tip 26 - Hello!'
options.add_option('sauce:options', sauce_options)

url = "https://#{ENV['SAUCE_USERNAME']}:#{ENV['SAUCE_ACCESS_KEY']}@ondemand.us-west-1.saucelabs.com:443/wd/hub"
@driver = Selenium::WebDriver.for(:remote, :url => url, :capabilities => options)
end

def teardown
@driver.quit
end

def run
setup
yield
teardown
end

Our setup method is where the magic is happening. We declare the browser options for Chrome and store it in a variable for immediate reuse. We then set the browser version (e.g. options.browser_version = '109', but you can also use latest to use the most recent version), specify the operating system we would like to use (e.g. options.platform_name = 'macOS 13'), and set the test name (e.g., sauce_options[:name] = 'Elemental Selenium - Tip 26 - Hello!').

We then connect to Sauce Labs with Selenium WebDriver and feed in our browser options object.

NOTE: The URL contains the URI for the Sauce Labs cl service as well as our Sauce user and access credentials. In this case, they are specified through environment variables. But you can just as easily hard-code your info here. We recommend exporting your Sauce Labs SAUCE_USERNAME and SAUCE_ACCESS_KEY as environment variables.

Here are instructions for setting environment variables on each Operating System:

Now we're ready to add a test to run in Sauce Labs.

run do
@driver.get 'http://the-internet.herokuapp.com'
expect(@driver.title.include?('The Internet')).to eql true
end

If we save this and run it (e.g., ruby cloud.rb from the command-line) it will execute the test on an Chrome 109 browser instance running on macOS 13 (Ventura) in Sauce Labs.

Another Browser

If you want to change up the browser, you would just need to alter the options:

  # For Chrome 110
options = Selenium::WebDriver::Options.chrome
options.browser_version = '110'

Or...

  # For Safari 16
options = Selenium::WebDriver::Options.safari
options.browser_version = '16'

Or...

  # For Edge 109
options = Selenium::WebDriver::Options.edge
options.browser_version = '109'

NOTE: You can find a full list of configuration options along with example code for all of Sauce supported languages at their Platform Configurator.

Expected Behavior

  • Open a specified browser in Sauce Labs
  • Test runs
  • Browser closes
  • Test results (along with a video recording, screenshots, and other debugging information) are available on the test results dashboard.

Summary

Hopefully this tip has helped you get your tests up and running against numerous browsers.

There are some other things to consider when using Sauce Labs (e.g., testing your secure apps through Sauce Connect, setting the pass/fail status for your tests, and dynamically setting the test name). You can find more information about these things in the Sauce Labs docs.

Happy Testing!