Rafael's Blog

Because manual testing is boring.

Selenium in the Cloud With Sauce Labs

Selenium is an awesome tool to run functional tests for web applications. One of the issues when creating browser tests is that some projects require a vast matrix of browsers/OS combinations and this means having a big infrastructure to support. That also means you have to maintain all the VMs by installing security patches and other required software updates. Sauce Labs provides over 400 browser/OS combinations in the cloud. All you have to do is specify which browser and OS version you want. The tests run the same way they would on a remote selenium server, except you have a wide variety of browsers and OSes to run your test against. If the tests fails or you just want to see what happened during the run, all you need to do is login to your Sauce Labs account and look at either the results log file or the video (yes, there is even a video!). Sauce Labs offers both free and paid plans. The paid plans basically work like a cell phone plan: you buy a certain number of minutes and use up those minutes to run your tests. All paid plans offer some level of concurrency, which can come in handy when you have a lot of browser tests to run. For instance, if you have 10 tests that take 1 minute each to run, you can execute the entire test suite in a minute by running 10 concurrent tests. Just like regular Selenium tests, you can code your tests in any of the supported languages. Their documentation and their customer service are awesome and you can find lots of examples of how to run your tests in the cloud. Let’s say you are testing the Wikipedia website and want to create a simple test to verify some content on a page. In Ruby, you would define a before block in your Rspec tests that launches a new browser window, using Sauce Labs as the remote server:

1
2
3
4
5
6
7
8
9
10
11
before :each do
    # launch the stuff
    caps = Selenium::WebDriver::Remote::Capabilities.firefox
    caps.platform = 'Linux'
    caps.version = '26'
    @driver = Selenium::WebDriver.for(
        :remote,
        :url =>  "http://#{SAUCE_USERNAME}:#{SAUCE_KEY}@ondemand.saucelabs.com:80/wd/hub",
        :desired_capabilities => caps)
    @driver.navigate.to 'http://www.wikipedia.org'
  end

Make sure you define the constants for your username and API key (you can get those from your account page on saucelabs.com). After that, you are ready to write your first test:

1
2
3
4
5
  it 'should find results for selenium' do
    homepage = WikipediaHome.new(@driver)
    homepage.search_for_something 'selenium'
    expect(homepage.text.include?('atomic number')).to eq(true)
  end

One important thing to keep in mind is that since ruby program is running on your local machine (sending commands to the remote webdriver in the cloud), you need to call Sauce Lab’s API after each test is done and update the test name and the result:

1
2
3
4
5
6
  after :each do |example|
    @driver.quit
    #update the sauce config settings after each test
    RestClient.put "https://#{SAUCE_USERNAME}:#{SAUCE_KEY}@saucelabs.com/rest/v1/#{SAUCE_USERNAME}/jobs/#{@driver.session_id}",
                   { name: example.full_description, passed: !example.exception }.to_json, :content_type => :json, :accept => :json
  end

Modify the caps object on the before block to specify what browser and OS you want.

Conclusion

If you have a large number of automated web tests you need to run, but don’t feel like spending a lot of time setting up a Selenium client infrastructure, checkout Sauce Labs to execute your tests in the cloud.

Comments