Frameworks

Put simply, Watir is a browser driver. Lightweight and simple to use, Watir excels at automating modern browsers. Built in Ruby, it also works well with other testing frameworks and solutions in the Ruby ecosystem. This section explores frameworks which are compatible with Watir including:

1. RSpec – Behaviour driven development
2. Cucumber – Business readable behaviour driven development
3. Test/Unit – Basic unit testing framework
4. WatirGrid – Distributed testing on a grid network

RSpec

RSpec is a Behaviour-Driven Development tool for Ruby programmers. BDD is an approach to software development that combines Test-Driven Development, Domain Driven Design, and Acceptance Test-Driven Planning. RSpec helps you do the TDD part of that equation, focusing on the documentation and design aspects of TDD. The following code demonstrates using Watir and a basic RSpec example.
More info …

Install

gem install rspec

Example

rspec test_spec.rb

will execute the following rspec example …

test_spec.rb


require "rubygems"
require "rspec"
require "watir-webdriver"

describe "google.com" do
  let(:browser)       { @browser ||= Watir::Browser.new :firefox }

  before { browser.goto "http://google.com" }
  after  { browser.close }

  it "should search for watir" do
    browser.text_field(:name => "q").set "watir"
    browser.button.click
    browser.div(:id => "resultStats").wait_until_present
    browser.title.should == "watir - Google Search"
  end
end

Cucumber

Cucumber lets software development teams describe how software should behave in plain text. The text is written in a business-readable domain-specific language and serves as documentation, automated tests and development-aid – all rolled into one format.
More info …

Install

gem install cucumber

Example

cucumber features

will execute the following cucumber feature …

features/example.feature

Feature: Search
  In order to use Google users must
  be able to search for content

  Scenario: Search for a term
    Given I have entered "watir" into the query
    When I click "search"
    Then I should see some results

features/step_definitions/example_steps.rb

require "watir-webdriver"
require "rspec/expectations"

Given /^I have entered "([^"]*)" into the query$/ do |term|
  @browser ||= Watir::Browser.new :firefox
  @browser.goto "google.com"
  @browser.text_field(:name => "q").set term
end

When /^I click "([^"]*)"$/ do |button_name|
  @browser.button.click
end

Then /^I should see some results$/ do
  @browser.div(:id => "resultStats").wait_until_present
  @browser.div(:id => "resultStats").should exist
  @browser.close
end

Test::Unit

Test::Unit is a framework for unit testing in Ruby, helping you to design, debug and evaluate your code by making it easy to write and have tests for it. The following code demonstrates using Watir and a basic Test::Unit example.
More info …

Install

Test::Unit already comes bundled with most ruby installations.

Example

ruby example_test.rb

will execute the following test case …

example_test.rb

require "rubygems"
require "test/unit"
require "watir-webdriver"

class GoogleSearch < Test::Unit::TestCase
   def setup
     @browser ||= Watir::Browser.new :firefox
   end
   def teardown
     @browser.close
   end
   def test_search
     @browser.goto "google.com"
     @browser.text_field(:name => "q").set "watir"
     @browser.button.click
     @browser.div(:id => "resultStats").wait_until_present
     assert @browser.title == "watir - Google Search"
  end
end

WatirGrid

WatirGrid allows multiple parallel processes to provide remote Watir objects in the form of tuple spaces (providers) across a grid network. This grid network is controlled by a ring server (controller). The following code demonstrates using Watir on a grid network.
More info …

Install

gem install watirgrid

Example

From a command line, start a controller on a central machine e.g.

controller
INFO -- : Controller started on : druby://10.0.1.3:11235

From a command line, start providers on remote machines e.g.

provider -b webdriver -c druby://10.0.1.3:11235
INFO -- : Provider started on   : druby://10.0.1.3:11236
INFO -- : Controller found on   : druby://10.0.1.3:11235
INFO -- : Provider registered   : druby://10.0.1.3:11235

Execute the following watirgrid example …

example.rb

require "watirgrid"
require "rspec/expectations"

Watir::Grid.control(:controller_uri => "druby://10.0.1.3:11235") do |browser, index|
  browser.goto "google.com"
  browser.text_field(:name => "q").set "watir"
  browser.button.click
  browser.div(:id => "resultStats").wait_until_present
  browser.title.should == "watir - Google Search"
end

4 thoughts on “Frameworks

  1. The watirgrid example would probably be a lot more useful if it showed starting up at least two providers (each on its own remote system) and examples of how to either distribute the tests across two systems, or run the same tests in parallel against different browsers.

    Better yet (if such is possible) show how to combine watirgrid with another framework and (as an example) run the same cucumber feature against three different boxes each with its own browser (e.g. Chrome on one, FF on another, IE on the third, etc)

    (presuming I’m correct that you could even use it in those ways)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s