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

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

8 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)

    • Is Oracle ERP a web interface? if so then you automate it much like any other web app. If it uses an interace native to the OS, then Watir is not the right tool (although there are ruby gems (libraries) you might be able to use for easy DB access if you just want to do it in ruby

  2. Pingback: WatirGrid 1.1.3 Released « 90kts

  3. Pingback: Digging for rubies, but found watir! | Where does your road lead?

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 )

Google+ photo

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

Connecting to %s