---
title: Run Selenium tests in Ruby with Capybara
description: Test websites with Ruby, Selenium and Capybara. Covers Integrate with
  TestingBot, Specify Browsers & Devices and Testing Internal Websites.
source_url:
  html: https://testingbot.com/support/web-automate/selenium/ruby/capybara
  md: https://testingbot.com/support/web-automate/selenium/ruby/capybara.md
---

# Capybara Automated Testing

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

TestingBot supports Selenium testing using Capybara. Before we start with the example, please install Capybara and the TestingBot client gem. The example below drives Capybara with Cucumber and runs in parallel via `parallel_tests`, so install those too:

```bash
gem install capybara testingbot cucumber parallel_tests
```

Now let's start with a simple Capybara test case.

```yaml
# Google Feature
Feature: Google Search Functionality

Background:
  Given I am on https://www.google.com/ncr

Scenario: Can find search results
  When I fill in "q" found by "name" with "TestingBot"
  And I submit
  Then I should see title "TestingBot - Google Search"
```

```ruby
# Google Steps
Given /^I am on (.*)$/ do |url|
  visit url
end

When /^I fill in "([^\"]*)" found by "([^\"]*)" with "([^\"]*)"$/ do |value, type, keys|
  fill_in(value, :with => keys)
end

When /^I submit$/ do
  find_field('q').native.send_key(:enter)
end

Then /^I should see title "([^\"]*)"$/ do |title|
  expect(page).to have_title title
end
```

## Integrate with TestingBot

Please use this module to run Capybara tests on TestingBot:

```ruby
require "capybara/cucumber"
require "selenium/webdriver"
require "testingbot"

Capybara.default_max_wait_time = 10
Capybara.default_driver = :testingbot

Before do |scenario|
  jobname = scenario.name

  Capybara.register_driver :testingbot do |app|
    options = Selenium::WebDriver::Options.chrome
    options.browser_version = ENV['version']
    options.platform_name = ENV['platform']
    options.add_option('tb:options', {
      name: jobname
    })
    url = "https://#{ENV['TB_KEY']}:#{ENV['TB_SECRET']}@hub.testingbot.com/wd/hub"

    Capybara::Selenium::Driver.new(app,
      browser: :remote,
      url: url,
      options: options
    )
  end

  Capybara.session_name = "#{jobname} - #{ENV['platform']} - #{ENV['browserName']} - #{ENV['version']}"

  @driver = Capybara.current_session.driver
  @session_id = @driver.browser.session_id
  puts "TestingBotSessionId=#{@session_id} job-name=#{jobname}"
end

After do |scenario|
  @driver.quit
  api = TestingBot::Api.new(ENV['TB_KEY'], ENV['TB_SECRET'])
  api.update_test(@session_id, success: !scenario.exception)
end
```

This module uses a Rake file which contains the browsers we want to run the test on:

**Rakefile** : 

```ruby
def run_tests(platform, browser, version, junit_dir)
  system("platform=\"#{platform}\" browserName=\"#{browser}\" version=\"#{version}\" JUNIT_DIR=\"#{junit_dir}\" parallel_cucumber features -n 20")
end

task :win11_chrome_latest do
  run_tests('WIN11', 'chrome', 'latest', 'junit_reports/win11_chrome_latest')
end

task :sonoma_chrome_latest do
  run_tests('SONOMA', 'chrome', 'latest', 'junit_reports/sonoma_chrome_latest')
end

multitask :test_testingbot => [
  :win11_chrome_latest,
  :sonoma_chrome_latest,
] do
  puts 'Running automation'
end
```

To start the test, please run this command:

```bash
bundle exec rake test_testingbot
```

## 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, inside the `env.rb` file.

```ruby
options = Selenium::WebDriver::Options.chrome
options.browser_version = ENV['version']
options.platform_name = ENV['platform']
options.add_option('tb:options', {
  'name' => 'My Capybara Test'
})
```

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 Capybara 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:

**env.rb** : 

```ruby
Capybara.register_driver :testingbot do |app|
  options = Selenium::WebDriver::Options.chrome
  options.browser_version = 'latest'
  options.platform_name = 'WIN11'
  options.add_option('tb:options', { 'name' => 'Tunnel Test' })

  Capybara::Selenium::Driver.new(app,
    browser: :remote,
    url: 'http://localhost:4445/wd/hub',
    options: options
  )
end
Capybara.default_driver = :testingbot
```

## 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, add more browser configurations in the `Rakefile`:

**Rakefile** : 

```ruby
def run_tests(platform, browser, version, junit_dir)
  system("platform=\"#{platform}\" browserName=\"#{browser}\" version=\"#{version}\" JUNIT_DIR=\"#{junit_dir}\" parallel_cucumber features -n 20")
end

task :win11_chrome_latest do
  run_tests('WIN11', 'chrome', 'latest', 'junit_reports/win11_chrome_latest')
end

task :sonoma_chrome_latest do
  run_tests('SONOMA', 'chrome', 'latest', 'junit_reports/sonoma_chrome_latest')
end

multitask :test_testingbot => [
  :win11_chrome_latest,
  :sonoma_chrome_latest,
] do
  puts 'Running automation'
end
```

The `-n 20` in the example above specifies how many parallel sessions to run.  
Usually you would use the same number as the maximum parallel sessions allowed by your TestingBot plan.

To run your tests in parallel, run this command:

```bash
bundle exec rake test_testingbot
```

### 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.

```ruby
api = TestingBot::Api.new("API_KEY", "API_SECRET")
api.update_test(Capybara.current_session.driver.browser.session_id, name: 'My Test', success: true)
```

## 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)
