Skip to content
This repository has been archived by the owner on Feb 13, 2020. It is now read-only.

Cucumber Parallel Tutorial

Greg Gauthier edited this page Apr 8, 2014 · 4 revisions

Using Sauce Labs with Ruby

The sauce gem makes it easy to run Selenium or Capybara tests against a wide range of browsers on Windows (XP, 7, 8), OS X, Linux, iOS and Android.

This example uses Capybara and Cucumber with Rails 3 and Ruby 1.9, but Sauce Labs also works great against any Ruby web stack, and with Test::Unit, [RSpec], and most other testing frameworks... right down to vanilla WebDriver.

Your tests are run in real browsers on a real operating system, in a dedicated, single-use VM. Once they're complete, screenshots, video, Selenium log and a log of passes and failures can be seen and shared.

We're working on making this tutorial as clear, simple, and relevant as possible. If you run into any problems, or have questions or suggestions, please don't hesitate to email [email protected]!

What You'll Need

In your Gemfile:

group :test, :development do
  # These are the target gems of this tutorial
  gem 'sauce', '~> 3.1.1'
  gem 'sauce-connect'
  gem 'parallel_tests'
  gem "capybara", "~> 2.0.3"
  gem "cucumber-rails", :require => false
  gem "sauce-cucumber", :require => false
  gem "rake", "10.1.0"
end

Setting up Cucumber

From your $RAILS_ROOT, generate a ./features directory tree, a ./features/support/env.rb file, and a warm, fuzzy feeling of productivity by executing:

    $ rails generate cucumber:install

Create a template sauce_helper, by running:

    $ rake sauce:install:features

Now, open the new ./features/support/sauce_helper.rb file, and configure your desired test platforms. Here's an example:

# Use Capybara integration
require "sauce"
require "sauce/capybara"

# Set up configuration
Sauce.config do |c|
  c[:browsers] = [ 
    ["Windows 8", "Internet Explorer", "10"],             
    ["Windows 7", "Firefox", "20"],
    ["OS X 10.8", "Safari", "6"],                         
    ["Linux", "Chrome", nil]          
  ]
end

Check out our platforms page for available platforms (130+ and counting!).

Inside the newly created ./features/support/sauce_helper.rb, just under the other require statements, we'll add requires for sauce/cucumber and tell Capybara to use the Sauce Labs driver:

# Use Capybara integration
require "sauce"
require 'sauce/capybara'
require "sauce/cucumber"

Capybara.default_driver = :sauce

# Set up configuration

Setting up the Sauce Gem

Keep your Sauce Labs credentials out of your repositories and available to all your Sauce Labs tools using projects by adding them as environment variables.

Open ~/.bash_profile and add the following lines:

export SAUCE_USERNAME=SAUCE:USERNAME
export SAUCE_ACCESS_KEY=SAUCE:ACCESS_KEY

You'll then need to re-load that profile with source ~/.bash_profile

Writing your tests

Phew! That's all your setup done. You're ready to write your features.

Turn on the Sauce voodoo by tagging each feature block with @selenium like this:

$ vim ./features/ramen_feature.rb
  Feature: Finding ramen

  @selenium
  Scenario: I should be able to find delicious Ramen on Wikipedia 
    When I go to Wikipedia.com
    # SNIP #

And that's everything!

Running your tests

$ bundle exec rake sauce:features[features,4]

It's that simple (Thanks in part to the excellent parallel_tests gem.)

That command will run everything in the 'features' directory, across 4 simultaneous Sauce sessions. Your features will run on those sessions, once for each platform you specified.

You should see output much like the following:

20 processes for 8 specs, ~ 0 specs per process
[Connecting to Sauce Labs...]
Sauce Connect 3.0-r25, build 38

[snip]A LOT OF TEST DATA[/snip]

1 scenario (1 passed)
2 steps (2 passed)
1m47.467s

1 scenario (1 passed)
2 steps (2 passed)

Took 112.497433 seconds

The multiple 1 scenario (1 passed) lines mean your features are running against each platform, and passing. Congratulations!

Running in parallel makes your build faster, so you can run more tests in more browsers in less total time.

Check out the results, including a command log, screenshots, and video of the browser executing the test, on your account page.

Help, it didn't work!

Make sure your features groups are tagged with @selenium. Without this tag, the Cucumber integration won't be used, and your tests will only run with the default Sauce configuration.

If you still can't get things going, drop us a line at [email protected] and we're happy to assist you. Don't forget to include your Gemfile.lock!

What's Next?

Capybara Resources

Now that you have an example to work with, it's time to write a test for your web app! For more info on how to write Capybara tests, we recommend the excellent Capybara README.

Testing against local servers with Sauce Connect

If you need to test a staged site behind your firewall, that's no problem: you're already set up to use Sauce Connect, which means local servers are available to your Sauce Labs tests.

If you don't need access to local servers, you can turn Sauce Connect off by adding this to your Sauce.config block:

  config[:start_tunnel] = false

parallel_tests considerations

If you find that you run into problems with all your tests using the same db at once, or otherwise need some careful setup, the parallel_tests instructions have useful info.

The Sauce Gem

The Sauce gem has its own github repo and a constantly evolving wiki you should check out!