External Cucumber Testing

by Brian Farr — May 14, 2012

This blog isn't using Rails or any other off the shelf framework, but I still wanted to be able to test it to make sure I don't inadvertantly break anything when I'm updating it. I came across Cucumber, an interesting take on testing that they call “behavior driven development”.


  • a computer with ruby
  • the cucumber gem
  • a site you want to test

Getting Started

You can download or view the files I created on github if you want to use them as a base for yours.

Cucumber behaviors live in a features directory. You create features, and then describe the steps needed to check them.


Here's the feature I used to describe what I want out of my home page.

Feature: Home
  As a user
  I want to visit the home page
  so that I can navigate to the rest of the site
  Scenario: Visit home page
	Given I try to visit the home page
	When the page loads
	Then the page should have navigation
	And the page should have content
	And the page should have a footer
	And the page should say "hire me"

You can see I give a rationale for the feature, and then describe some things I want to happen. I make sure the page loads, that it has the basic elements of my site, and that it says "hire me" somewhere, because I'm currently looking for a good fulltime gig.

Now we need the steps for this feature. In features/step_definitions, I made home_steps.rb:

Given /^I try to visit the home page$/ do

When /^the page loads$/ do

Then /^the page should have navigation$/ do
  response_body.should have_xpath(%\//nav\)

Then /^the page should have content$/ do
  response_body.should have_xpath(%\//div[@class='content']\)

Then /^the page should have a footer$/ do
  response_body.should have_xpath(%\//footer\)

Then /^the page should say "([^"]*)"$/ do |arg1|  #"
 response_body.should contain(arg1)

One cool feature of cucumber: when you run a feature with no steps yet, it will helpfully make and print a skeleton of the step definition you need, so you can get started and type less.

You can see the page structure checks use xpath to look at the html output of the page, and that "should say" check is using a variable argument. This allows us to re-use the "page should say" in other features.

I'm using a wrapper around the visit function, myvisit, so that I can set a base url to use in the configuration.

There are a few other nifty things that I have in the github repository, like checking for error codes, and gems to download and process the pages that work well on a variety of platforms.


The upside of all this is that whenever I am working on my site, I can go into its main directory and type cucumber, and be greeted shortly by a list of all things that are working or not working on my site, even the dusty corners that I sometimes forget about.

If you have any questions about the stuff I did in this file, just drop me an email and I'll do what I can to help. Thanks for reading!