/operawatir

Easily and automatically test your web pages just like a user would. OperaWatir simulates a user clicking links, entering text and submitting forms, reporting results back so you know that your website works.

Primary LanguageRubyBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

Readme

OperaWatir is a part of the Watir (pronounced water) family of free software Ruby libraries for automating web browsers. OperaWatir provides a querying engine and Ruby bindings for a backend Java library, OperaDriver, for driving the Opera web browser. It aims for full compliance with the Watir 2 and the Watir 3 specifications.

Install

Requirements

OperaWatir runs on GNU/Linux, Mac OS X and Windows operating systems. Required dependencies are Java >= 1.6.0, JRuby <= 1.5.5, RubyGems >= 1.3.5, RSpec >= 2.4, and a somewhat recent build of Opera (desktop, internal, mobile). For Windows you must also have the Microsoft Visual C++ 2008 Redistributable Package.

Installation procedure

Make sure that you have the right requirements for installing and running OperaWatir. Please note that OperaWatir will only run on the JRuby platform. For more detailed installation instructions, please see our Getting Started guide.

To install (leave out the “sudo” command if you're installing on Windows):

% sudo jruby -S gem install operawatir

Next, make sure that JRuby's bin directory is a part of your PATH environmental variable on GNU/Linux.

Examples

The Watir API allows you to write scripts that interact with any web page. Its primary purpose is to ease test automation for web applications. Your scripts can, for example, before you deploy automatically go through all the steps your users normally would and alert you of any regressions.

Let's take a closer look at how this works.

require 'rubygems'
require 'operawatir'

browser = OperaWatir::Browser.new

browser.goto 'http://en.wikipedia.org/'
browser.text_field(:id, 'searchInput').set 'Opera'
browser.button(:id, 'searchButton').click

The script above will tell the browser to load the front page of Wikipedia, write “Opera” in the search field and click the search button. If all goes as intended, the browser will end up at a relevant article.

To run the script, use the following command:

% jruby example.rb

As Watir scripts are run in a full-featured browser, all keypresses and clicks will be interpreted as if they were real, and invoke attached JavaScript event listeners. If we want to test the JavaScript-powered suggestion feature on the Wikipedia search box, we can do this:

require 'rubygems'
require 'operawatir'

browser = OperaWatir::Browser.new

browser.goto 'http://en.wikipedia.org/'
browser.text_field(:id, 'searchInput').click
browser.type 'Hello world'
browser.keys.send :down, :enter

The first suggested link will be selected by pressing arrow down ('Down') and navigated to ('Enter'). Using the same key events, you could even teach a script to play platform games:

require 'rubygems'
require 'operawatir'

browser.goto 'http://www.phoboslab.org/biolab/'
sleep 2
browser.keys.send 'X'
sleep 1
browser.keys.down :right
5.times { browser.keys.send 'X' }
2.times { browser.keys.send 'C' }
browser.keys.up :right

Sending commands to the browser is great, but sometimes we want to get something back too. The following lines of code will have the browser automatically look up the phone number of Opera Software in the yellow pages and write it to the console.

require 'rubygems'
require 'operawatir'

browser = OperaWatir::Browser.new

browser.goto 'http://gulesider.no/'
browser.text_field(:name, 'search_word').set 'Opera Software'
browser.button(:name, 'btn_cs').click
puts browser.li(:class, 'tel').text

When running proper functional tests on your web application with Watir, you might want a bit more structured output. To define assertions and get pretty test reports, you can use RSpec, a behaviour-driven testing framework.

When writing test suites you can use the OperaWatir::Helper class to help ease some tasks, such as constructing and tearing down the Browser object. A simple test suite looks like this:

describe 'Yellow Pages' do
  before :each do
    browser.goto 'http://gulesider.no/'
  end

  it 'finds the phone number to Opera Software' do
    browser.text_field(:name, 'search_word').set 'Opera Software'
    browser.button(:name, 'btn_cs').click
    browser.li(:class => 'tel').text.should == '24 16 40 00'
  end

  it 'finds the phone number to the Norwegian Opera and Ballet' do
    browser.text_field(:name, 'search_word').set 'Den Norske Opera'
    browser.button(:name, 'btn_cs').click
    browser.li(:class, 'tel').text.should == '21 42 21 00'
  end
end

Since this is an RSpec test you can run it using the operawatir binary, which is simply a shortcut for running tests:

% operawatir yellow_pages.rb

Each block of code corresponds to a single named test case returning PASS or FAIL. Whether you got back the expected and correct return value is evaluated with .should. If all tests pass, you will see the following output:

..

Finished in 2.04 seconds
2 examples, 0 failures

If anything fails, more information about each failure will be provided.

You can also see operawatir -h for more usage information:

Usage: operawatir [-m|--manual] [-l|--launcher=BINARY] [--binary=BINARY]
       [-a|--args=ARGUMENTS] [-q|--no-quit] [--opera-idle] [-b|--backtrace]
       [--no-color] [-t|--tag=TAG] [-f|--format=FORMAT] [-o|--out=FILE]
       [-h|--help] [-v|--version] FILES

Specific options:
    -m, --manual                     Wait for a manual connection from opera:debug
    -l, --launcher=BINARY            Path to launcher binary, will use environmental 
                                     variable OPERA_LAUNCHER if not specified
        --binary=BINARY              Browser to run the test with, will use guess the 
                                     path or use environmental variable OPERA_PATH if 
                                     not specified
    -a, --args=ARGUMENTS             Arguments passed to the binary, will override  
                                     environmental variable OPERA_ARGS
    -q, --no-quit                    Disable quitting of Opera at the end of a test run
        --opera-idle                 Enable OperaIdle
    -b, --backtrace                  Enable full backtrace
        --no-color                   Disable colorized output
    -t, --tag=TAG                    Specify tags to only run examples with the specified
                                     tag, to exclude examples, add ~ before the tag (e.g.
                                     `~slow')
    -o, --out=FILE                   Send output to a file instead of STDOUT
    -f, --format=FORMAT              Specify RSpec output formatter (documentation, 
                                     html, progress (default), textmate)

Common options:
    -h, --help                       Show this message
   -v, --version                    Show version

Third-Party Libraries

OperaWatir uses the following libraries: