---
title: Run Selenium tests in Ruby with Cucumber
description: Test websites with Ruby and Cucumber. Run automated tests with Cucumber
  on 6100+ real browsers and devices in the TestingBot cloud.
source_url:
  html: https://testingbot.com/support/web-automate/selenium/ruby/cucumber
  md: https://testingbot.com/support/web-automate/selenium/ruby/cucumber.md
---

# Cucumber Automated Testing

See our [Cucumber example repository](https://github.com/testingbot/ruby-cucumber-example).

To get started, please make sure you have installed the necessary gems:

```ruby
source 'https://rubygems.org'
gem 'cucumber'
gem 'selenium-webdriver'
gem 'rspec'
gem 'rake'
gem 'parallel'
gem 'testingbot'
```

Save the block above as a `Gemfile` and run `bundle install`. Cucumber expects a specific project layout: feature files in `features/`, step definitions in `features/step_definitions/`, and the driver setup/hooks in `features/support/env.rb`.

## Example

Next, we'll create a `features` and a `steps` file. Combined, they're your test.

### Example feature (features/google.feature)

```yaml
Feature: Google can search

Background:
  Given I am on Google

Scenario: Search for a term
  When I fill in "q" found by "name" with "TestingBot"
  And I submit
  Then I should see title "TestingBot - Google Search"
```

### Example steps (features/step\_definitions/steps.rb)

```ruby
Given /^I am on (.+)$/ do |url|
  @browser.navigate.to "https://www.google.com"
end

When /^I fill in "([^"]*)" found by "([^"]*)" with "([^"]*)"$/ do |value, type, keys|
  @element = @browser.find_element(type, value)
  @element.send_keys keys
end

When /^I submit$/ do
  @element.submit
end

Then /^I should see title "([^"]*)"$/ do |title|
  raise "Fail" if @browser.title != title
end
```

### Example env.rb (features/support/env.rb)

```ruby
require 'selenium-webdriver'
require 'testingbot'

Before do
  options = Selenium::WebDriver::Options.send(ENV['browserName'] || 'chrome')
  options.browser_version = ENV['version'] || 'latest'
  options.platform_name = ENV['platform'] || 'WIN11'
  options.add_option('tb:options', {
    key: ENV['TB_KEY'],
    secret: ENV['TB_SECRET']
  })

  url = 'https://hub.testingbot.com/wd/hub'

  client = Selenium::WebDriver::Remote::Http::Default.new
  client.open_timeout = 180
  @browser = Selenium::WebDriver.for(:remote, url: url, options: options, http_client: client)
end

After do |test_case|
  sessionid = @browser.session_id
  jobname = test_case.name

  puts "TestingBotSessionId=#{sessionid} job-name=#{jobname}"

  @browser.quit
end
```

Now we can run the test on TestingBot, to start, please run this command:

```bash
cucumber TB_KEY=api_key TB_SECRET=api_secret platform=WIN11 browserName=chrome
```

### Configuring capabilities

To run your existing tests on TestingBot, your tests will need to be configured to use the TestingBot remote machines. If the test was running on your local machine or network, you can simply change your existing test like this:

**Before:**

```ruby
driver = Selenium::WebDriver.for :chrome
```

**After:**

```ruby
driver = Selenium::WebDriver.for(:remote,
  url: 'https://hub.testingbot.com/wd/hub',
  options: options)
```

## Specify Browsers & Devices

To let TestingBot know on which browser/platform/device you want to run your test on, you need to specify the browsername, version, OS and other optional options in the capabilities field.

```ruby
options = Selenium::WebDriver::Options.chrome
options.browser_version = 'latest'
options.platform_name = 'WIN11'
options.add_option('tb:options', {
  key: 'api_key',
  secret: 'api_secret'
})

driver = Selenium::WebDriver.for(:remote,
  url: 'https://hub.testingbot.com/wd/hub',
  options: options)
```

To see how to do this, please select a combination of browser, version and platform in the drop-down menus below.

  ![OS selected](https://testingbot.com/assets/environments/svg/windows11-0e1b28bc0fdd5034d3e4d3dc8d346c500a8c6522facf4b45d0da56537c1f1c6d.svg) Windows 11 › ![Browser Selected](https://testingbot.com/assets/environments/svg/chrome-c4081ff447d2d898d4afcb8f074a907c960e6f007716c1a1d119eee6803c4042.svg) Chrome 139 

Loading environments...

Please wait while we load the available browsers and platforms.

## Testing Internal Websites

We've built [TestingBot Tunnel](https://testingbot.com/support/tunnel), to provide you with a secure way to run tests against your staged/internal webapps.  
Please see our [TestingBot Tunnel documentation](https://testingbot.com/support/tunnel) for more information about this easy to use tunneling solution.

The example below shows how to easily run a Cucumber test with our Tunnel:

1. [Download our tunnel](https://testingbot.com/support/tunnel) and start the tunnel:

```bash
java -jar testingbot-tunnel.jar key secret
```

2. Adjust your test: instead of pointing to `'hub.testingbot.com/wd/hub'` like the example above - change it to point to your tunnel's IP address.   
 Assuming you run the tunnel on the same machine you run your tests, change to `'localhost:4445/wd/hub'`. localhost is the machine running the tunnel, 4445 is the default port of the tunnel.

This way your test will go securely through the tunnel to TestingBot and back:

### Example env.rb

```ruby
require 'selenium-webdriver'

options = Selenium::WebDriver::Options.send(ENV['SELENIUM_BROWSER'] || 'chrome')
options.browser_version = ENV['SELENIUM_VERSION'] || 'latest'
options.platform_name = ENV['SELENIUM_PLATFORM'] || 'WIN11'
options.add_option('tb:options', {
  name: 'My first Test',
  key: ENV['TB_KEY'],
  secret: ENV['TB_SECRET']
})

url = 'http://localhost:4445/wd/hub'
browser = Selenium::WebDriver.for(:remote, url: url, options: options)

Before do |scenario|
  @browser = browser
end

at_exit do
  browser.quit
end
```

## Run tests in Parallel

Parallel Testing means running the same test, or multiple tests, simultaneously. This greatly reduces your total testing time.

You can run the same tests on all different browser configurations or run different tests all on the same browser configuration.   
 TestingBot has a large grid of machines and browsers, which means you can use our service to do efficient parallel testing. It is one of the key features we provide to greatly cut down on your total testing time.

To start running tests in parallel, first we'll define a `browsers.json` file which contains all the browser combinations:

```json
[{
  "browserName": "firefox",
  "browserVersion": "latest",
  "platformName": "SONOMA"
},
{
  "browserName": "chrome",
  "browserVersion": "latest",
  "platformName": "WIN11"
}]
```

Now, we'll create a single test ( **features** and **steps** file) which we'll use to run on the browser combinations we defined above.

```yaml
Feature: Google can search

Background:
  Given I am on Google

Scenario: Search for a term
  When I fill in "q" found by "name" with "TestingBot"
  And I submit
  Then I should see title "TestingBot - Google Search"
```

```ruby
Given /^I am on (.+)$/ do |url|
  @browser.navigate.to "https://www.google.com"
end

When /^I fill in "([^"]*)" found by "([^"]*)" with "([^"]*)"$/ do |value, type, keys|
  @element = @browser.find_element(type, value)
  @element.send_keys keys
end

When /^I submit$/ do
  @element.submit
end

Then /^I should see title "([^"]*)"$/ do |title|
  raise "Fail" if @browser.title != title
end
```

Now we need to create a Ruby script that will run the test above, and use the environment variables to determine browser/OS combination.

```ruby
require 'selenium-webdriver'

options = Selenium::WebDriver::Options.send(ENV['SELENIUM_BROWSER'])
options.browser_version = ENV['SELENIUM_VERSION']
options.platform_name = ENV['SELENIUM_PLATFORM']
options.add_option('tb:options', {
  key: ENV['TB_KEY'],
  secret: ENV['TB_SECRET'],
  build: ENV['TB_AUTOMATE_BUILD']
})

url = 'https://hub.testingbot.com/wd/hub'
browser = Selenium::WebDriver.for(:remote, url: url, options: options)

Before do |scenario|
  @browser = browser
end

at_exit do
  browser.quit
end
```

Finally, we create a `Rakefile` which handles the running of the tests in parallel.

```ruby
require 'rubygems'
require 'cucumber'
require 'cucumber/rake/task'
require 'parallel'
require 'json'

@browsers = JSON.load(open('browsers.json'))
@parallel_limit = ENV["nodes"] || 1
@parallel_limit = @parallel_limit.to_i

task :cucumber do
  Parallel.each(@browsers, :in_processes => @parallel_limit) do |browser|
    begin
      puts "Running with: #{browser.inspect}"
      ENV['SELENIUM_BROWSER'] = browser['browserName']
      ENV['SELENIUM_VERSION'] = browser['browserVersion']
      ENV['SELENIUM_PLATFORM'] = browser['platformName']

      Rake::Task[:run_features].execute()
    rescue StandardError => e
      puts "Error while running task: #{e.message}"
    end
  end
end

Cucumber::Rake::Task.new(:run_features)
task :default => [:cucumber]
```

To run your tests in parallel, run this command:

```bash
rake TB_KEY=api_key TB_SECRET=api_secret nodes=3
```

### Queuing

Every plan we provide comes with a limit of parallel tests.   
 If you exceed the number of parallel tests assigned to your account, TestingBot will queue the additional tests (for up to 6 minutes) and run the tests as soon as slots become available.

## Mark tests as passed/failed

As TestingBot has no way to determine whether your test passed or failed (it is determined by your business logic), we offer a way to send the test status back to TestingBot. This is useful if you want to see if a test succeeded or failed from the TestingBot member area.

You can use our [Ruby API client](https://github.com/testingbot/testingbot_ruby) to report back test results. Call this from an `After` hook in `features/support/env.rb`, where the driver is available as `@browser` (and read the result via `test_case.failed?`):

```ruby
After do |test_case|
  api = TestingBot::Api.new("key", "secret")
  api.update_test(@browser.session_id, { name: test_case.name, success: !test_case.failed? })
  @browser.quit
end
```

## Other Ruby Framework examples

- [Capybara](https://testingbot.com/support/web-automate/selenium/ruby/capybara)

Capybara is an integration testing tool for rack based web applications.

- [Cucumber](https://testingbot.com/support/web-automate/selenium/ruby/cucumber)

Cucumber is a Ruby based test tool for BDD.

- [RSpec](https://testingbot.com/support/web-automate/selenium/ruby/rspec)

RSpec is a behavior-driven development (BDD) framework, inspired by JBehave.

- [Test::Unit](https://testingbot.com/support/web-automate/selenium/ruby/testunit)

Test-Unit is a xUnit family unit testing framework for Ruby.

- [Minitest](https://testingbot.com/support/web-automate/selenium/ruby/minitest)

Minimal (mostly drop-in) replacement for test-unit.

- [Watir](https://testingbot.com/support/web-automate/selenium/ruby/watir)

Watir, pronounced water, is an open-source (BSD) family of Ruby libraries for automating web browsers.

### Looking for more help?

Have questions or need more information? Reach out via email or Slack.

[Email us](https://testingbot.com/contact/new) [Join our Slack](https://join.slack.com/t/testingb0t/shared_invite/zt-3bcw9xch-jk19~6XPs_xBrsAgAedkCw)
