Some cucumber basics combined with selenium

My idea is to create human readable test scripts to test a web-based application. These tests are then run automated against a browser.

First I installed Cucumber and then created some example files to demonstrate its usage with Selenium on my Ubuntu box.

Install ruby, cucumber and other extensions:

apt-get install ruby
apt-get install rubygems1.8
apt-get install ruby1.8-dev
apt-get install libxml2-dev libxslt1-dev
gem install gherkin
gem install cucumber
gem install webrat
gem install rspec
gem install selenium-client
gem install selenium-webdriver

 

Then create a directory structure:

mkdir -p cucumber/features/
mkdir -p cucumber/features/step_definitions/
mkdir -p cucumber/features/support/

 

Create a file google.feature in the folder features containing:

Feature: Search Google

Scenario: Search Google
	Given I have opened "http://www.google.com/"
	When I search for "Niels van Reijmersdal"
	Then I should see "My personal blog"

 

Create a file google_feature.rb in the folder step_definitions containing:

Given /^I have opened "([^"]*)"$/ do |url|
        visit url
end
When /^I search for "([^"]*)"$/ do |search|
	fill_in "q", :with => search
	sleep 3
end
Then /^I should see "([^"]*)"$/ do |text|
        response.should contain(text)
end

 

Create a file env.rb in the folder support containing:

require 'webrat/selenium'
require 'webrat/core/matchers'
require 'rspec'

Webrat.configure do |config|
  config.mode = :selenium
  config.application_framework = :external
  config.selenium_server_address = '127.0.0.1'
  config.selenium_browser_startup_timeout = 60
end

World do
  session = Webrat::Session.new
  session.extend(Webrat::Methods)
  session.extend(Webrat::Selenium::Methods)
  session.extend(Webrat::Selenium::Matchers)
  session
end

 

Download the latest version of Selenium Server and run it:

java -jar selenium-server-standalone-2.8.0.jar

 

Start the cucumber from within the cucumber directory:

/var/lib/gems/1.8/bin/cucumber

 

This should start cucumber and run all its feature files, this should make the selenium server start firefox and execute the commands. The sleep command is pretty ugly, but i am just starting with this selenium, cucumber and ruby combination. When i figured out a good way to check if elements are present with this selenium interface i will update this howto. (Update: Its easier using Capybara, see this blog post for the same example.)

Next steps are to create a dedicated cucumber and selenium machine to keep the test running all the time.

Most stuff here I learned from this blog post (great post about using cucumber with webrat for webtesting). The selenium stuff was scattered around on the web. Also be sure to read the nicely written e-book “The secret ninja cucumber scrolls” for more info on cucumber, gherkin and automated testing. You should be able to read it within two-three hours.

Share and Enjoy:
  • Print
  • Twitter
  • Google Bookmarks
  • Facebook
  • Hyves

Written by Niels van Reijmersdal

October 19th, 2011 at 16:07

Posted under English,Software Testing

Comments: No comments yet!

Leave a Reply