Fetching contributors…
Cannot retrieve contributors at this time
150 lines (126 sloc) 3.82 KB

Testing Web Apps with Selenium, Capybara, and Cucumber

by Craig Buchek

St. Louis GNU/Linux Users Group


Automated Testing

  • Regression Testing
  • Acceptance Testing
    • Know when you're done writing code
  • Smoke Testing
  • TDD
    • BDD
    • ATDD
  • Does not completely replace manual testing

Selenium (WebDriver)


  • Supported languages:

    • Java
    • Ruby
    • C#
    • Python
    • Perl (3rd party)
    • PHP (3rd party)
  • Supported browsers:

    • Firefox 3.6+
    • Chrome 12+ (requires extra download)
    • IE 6+ (requires extra download for better support)
    • Safari 5+ (beta)
    • Opera 11.5+
    • Android 2.3+ (requires extra download and Android SDK)
    • iOS 3.2+ (requires extra download, XCode, iOS SDK, and developer license)
  • Selenium IDE (Firefox plugin to create scripts)

  • Selenium Server (remote control)

  • Selenium RC (old remote control)

  • Selenium Grid (distribute over multiple machines or VMs)

  • Commercial support/users

    • Sauce Labs
    • Gomez RealityCheck
    • BrowserMob (load testing)
  • Java Example: Selenium2Example at

  • Ruby Example: Ruby code at

  • Live Example: IRB

      #gem install selenium-webdriver
      require 'selenium-webdriver'
      driver = Selenium::WebDriver.for :firefox
      driver.get ''
      inputElement = driver.find_element :name => "q"
      inputElement.send_keys "Cheese!"
      #inputElement.send_keys :enter
      buttonElement = driver.find_element :name => "btnG"



  • Simple front end to Selenium

    • Can also front end directly to Webkit or Rack (Rails)
  • My cheatsheet:

  • Live Example: IRB

      #gem install capybara
      #gem install rspec
      require 'capybara/dsl'
      include Capybara::DSL
      Capybara.default_driver = :selenium
      require 'rspec'
      include RSpec::Matchers
      fill_in 'q', with: 'Craig Buchek'
      click_button 'gbqfb' # Or click_on 'gbqfb'
      page.should have_content('STLLUG')
      page.should_not have_content('John Doe')
      page.should have_content('kgkjhgkjhgjk')



  • Describe behavior in plain text.

    • Supports many different human languages.
    • Given, When, Then, And, But (case sensitive)
  • Scenario

  • Scenario Outlines / Examples

  • Feature Introduction

      As a <role>,
      I want to <perform some action>
      In order to <achieve some goal>
  • Goal should be one of:

    • Increase Revenue, Reduce Costs, Protect Revenue, Increase Goodwill
    • You might have to pop the "why" stack 5 times to get to the true business value
  • Step definitions convert English to Ruby (or Java)

    • Cucumber compares English strings to Ruby regular expressions
  • Cucumber-JVM

  • Tags

  • Background

  • Higher-level steps

    • Descriptive versus imperative
  • Imperative Example:

      Given I go to ""
      When I enter "Craig Buchek" in "q"
      And I hit the search button
      Then I should see ""
  • Descriptive Example:

      Given I am on the Google search page
      When I search for "Craig Buchek"
      Then I should see "" in the search results

Advanced Ideas

  • Headless drivers
    • Rack
    • HtmlUnit
    • Mechanize
    • Capybara-Webkit
    • Poltergeist (PhantomJS)
  • Parallel test runs
  • WaterBuffalo (Java port of Capybara)
  • Testing command-line programs (Aruba)
  • Relish
  • JBehave


  • Books
    • RSpec Book
    • Cucumber Book