Cucumber Ruby

Applies to CrossBrowserTesting SaaS, last modified on January 19, 2022

Want a powerful and easy to use command line tool for running Selenium tests? Cucumber might be the option for you. Cucumber provides language-bindings for the powerful browser-driving tool Selenium. Its Gherkin language allows you to write your tests in a way that can be easily read by anyone on your team. Cucumber Ruby integrates easily with the CrossBrowserTesting platform, so you can perform tests on a wide variety of OS/Device/Browser combinations, all from one test.

Get set up

  • Installing Selenium

    First let's get Selenium installed. Its easiest to do so with Gem:

    gem install selenium-webdriver
  • Install Cucumber

    Then let's get Cucumber Ruby installed:

    gem install cucumber
  • Install Test-Unit

    We will also use test-unit to perform functional unit tests on our platform:

    gem install test-unit
  • Initialize project

    Lastly, initialize your Cucumber project using the command:

    cucumber --init

Write tests

We will first need to create a feature file where our test steps are defined in the Gherkin language. Save the following as features/todo.feature:

Feature: ToDo App
  Scenario: Archiving ToDos
    Given I go to my ToDo App
    When I archive all todos
    Then I should have no todos

Next we need to define the procedural code. This will be the Ruby that works with the Selenium language bindings to create the logic of our test. Save the following as features/step_definitions/stepdefs.rb:


require 'test/unit/assertions'
include Test::Unit::Assertions

Given("I go to my ToDo App") do"")

When("I archive all todos") do
    @driver.find_element(:name, "todo-1").click()
    @driver.find_element(:name, "todo-2").click()
    @driver.find_element(:name, "todo-3").click()
    @driver.find_element(:name, "todo-4").click()
    @driver.find_element(:name, "todo-5").click()
    @driver.find_element(:link_text, "archive").click()

Then("I should have no todos") do
    elems = @driver.find_elements(:class, "done-true")
    assert_equal(0, elems.length)

    if @driver!= nil

As you can probably make out from our test, we visit a small ToDo App example, interact with our page, and use assertions to verify that the changes we’ve made are actually reflected in our app.

Last, we will need to create a new file features/step_definitions/env.rb that defines our connection to the remote hub:

Note: You will need to use your Username and Authkey to run your tests on CrossBrowserTesting. To get yours, sign up for a free trial or purchase a plan.


require 'selenium/webdriver'

username = 'YOUR_USERNAME'
authkey = 'YOUR_AUTHKEY'

# Defining capabilities of the test session, such as browser, OS, and resolution
caps =
caps["name"] = "Cucumber Ruby" # A name for your test
caps["build"] = "1.0" #Versioning data for your site or application as you test
caps["browserName"] = "Chrome" #You can get a full list of browser, OS, and resolution combinations from our API
caps["platform"] = "Windows 10"
caps["screen_resolution"] = "1366x768"
caps["record_video"] = "true"

driver = Selenium::WebDriver.for(:remote, :url => "http://#{username}:#{authkey}", :desired_capabilities => caps)

Beforedo |scenario|
    @driver = driver

Now you are ready to run your test using the command:


Congratulations! You have successfully integrated CBT and Cucumber for Ruby. We kept it short and sweet for our purposes, but there is so much more you can do with Cucumber Ruby! Being built on top of Selenium means the sky is the limit as far as what you can do.

For examples and source code to this tutorial, check out our  Cucumber Ruby GitHub Repository

If you have any trouble, feel free to get in touch. We are always happy to help!

See Also

Test Frameworks and Tools
About Selenium Testing
Selenium and Ruby

Highlight search results