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