Features
< Back to Blog Overview

Ruby testing with Cucumber and Capybara

2012-02-19
  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn
  • Share on HackerNews

In this post you'll find a small tutorial on how to use Cucumber and Capybara to run Selenium tests on our Selenium grid.


Cucumber is Aslak Hellesøy’s rewrite of RSpec’s "Story runner". It uses plain text DSL (Gherkin), which we mentioned in a previous post (Behat and Mink), meaning you can write tests as stories.


Capybara fits nicely together with Cucumber when writing web application tests using Selenium.


To begin, let's create a directory called features which will hold all the files Cucumber requires: $ mkdir features


Now we'll add a configuration file so Cucumber knows what to do:

$ mkdir features/support
$ vim features/support/env.rb

require 'capybara'
require 'capybara/cucumber'
require 'rspec'
require 'selenium/webdriver'

caps = Selenium::WebDriver::Remote::Capabilities.firefox
caps.version = "8"
caps.platform = :WINDOWS

Capybara.default_driver = :selenium
Capybara.register_driver :selenium do |app|
  Capybara::Selenium::Driver.new(app,
    :browser => :remote,
    :url => "http://[KEY]:[SECRET]@hub.testingbot.com:4444/wd/hub",
    :desired_capabilities => caps)
end

This file tells Cucumber we will run the Selenium test on TestingBot.com's grid.


Now let's create a story (feature): $ vim features/youtube.feature

Feature: YouTube has a search function.

Background:
  Given I am on YouTube

Scenario: Search for a term
  When I fill in "search_query" with "text adventure"
  And I press "search-btn"
  Then I should see "GET LAMP: The Text Adventure Documentary"

If you would now run "cucumber", you would see the following:

1 scenario (1 undefined)
4 steps (4 undefined)

"You can implement step definitions for undefined steps with these snippets:"

You'll know need to specify the step definitions from your feature, create a file youtube_steps.rb:

$ vim features/youtube_steps.rb

Given /^I am on (.+)$/ do |url|
  visit "http://www.youtube.com"
end

When /^I fill in "([^"]*)" with "([^"]*)"$/ do |field, value|
  fill_in(field, :with => value)
end

When /^I press "([^"]*)"$/ do |button|
  click_button(button)
end

Then /^I should see "([^"]*)"$/ do |text|
  page.should have_content(text)
end

If you run cucumber again, your test should succeed and a video + screenshots should be available in our member area.

1 scenario (1 passed)
4 steps (4 passed)

We hope this simple tutorial demonstrates how easy it is to start testing your website in minutes. You can find these demo files in our GitHub repository.

TestingBot Logo

Sign up for a Free Trial

Start testing your apps with TestingBot.

No credit card required.

Other Articles

blank

If you haven't heard of Behat yet, it's a BDD framework which runs on PHP written by Konstantin Kudryashov. Behat is similar to Cucumber for Ruby, ...

Read more
Selenium cloud testing with Jenkins

Jenkins (formerly known as Hudson) is an open-sourced continuous integration (CI) system built in Java. With Jenkins you are able to run your Selen...

Read more
Browser statistics gathered from Selenium testing

This week we've reached the 35,000 tests milestone so we thought it might be interesting to do some research in the statistics we've gathered durin...

Read more
Video Record your Selenium Tests

When a Selenium test fails, you usually get a clear error message or trace to indicate where the problem is located.&nbsp;A video of your Seleniu...

Read more
Mobile Testing with Selenium and Android Ice Cream Sandwich

Today we've added Android to our Selenium grid, which means you can now use our grid for mobile testing as well. We've hooked up an Android Ice C...

Read more
Receive SMS alerts when a Selenium test fails

Today we've added SMS support to our alert options. If a test fails, we can now alert you via e-mail, push notification and SMS.

Read more