Tools for running Selenium 2.x (i.e. Selenium::Webdriver) in Ruby
Ruby Shell
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
bin
lib
tests
yaml
README.md

README.md

Tools For Ruby Selenium 2.x (AKA Selenium::Webdriver)

This project has some wrapper scripts to help drive complex testing, using rspec, with Selenium::Webdriver ( http://rubygems.org/gems/selenium-webdriver ).

It also has some helper functions to simplify driving actual tests, such as functions to test large amounts of elements on a age, driven by yaml data files.

It also has some programs to harvest data about many elements all at once to fill those yaml data files.

Requirements

You really want the latest of the Selenium parts, or things won't work well (and even then there are a decent amount of bugs still as of Jan 2012).

Example Run

This project comes with tests against www.google.com, which is much more interesting than you'd expect; it's mostly javascript these days.

Basic usage is:

bin/runtest.rb

Here's how the run looks:

wrapper

    set up google
      should load the remote driver
      should load the setup page
      should change to no instant predictions
-      should save the new setting

    run a basic search on Google
      should load the main page
      should take the text input
---      should click elsewhere to close the javascripty bits
--      should click on search and load the new page
..........................      should have the same item titles as last time (FAILED - 1)
...........      should have the same item urls as last time (FAILED - 2)

    run a feeling lucky search on Google
      should load the main page
      should take the text input
---      should click elsewhere to close the javascripty bits
--      should click on "I'm Feeling Lucky" and load the new page

    go to the google translate page via the main site
      should load the main page
      should click on the More link
--      should click on the translate link and load the new page

WARNING: It'll probably fail, as you see above. See the next section for why.

I use ssh port forwarding to run my Selenium server on a different system than the system where I run runtest. The script has a bunch of options (use -h to show them), and in particular you can use -p to give the port number of your Selenium server.

The BasicSearch section of the test can take a bit; you can skip it like so:

bin/runtest.rb -S BasicSearch

The browser option can be used to pick another browser; see http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions#Q:_Which_browsers_does_WebDriver_support? for a list.

Most of the rest of the options are of no use with tests as simple as the example.

YAML Data Generation

Part of the point here is to have your tests be driven by data files as much as possible, so that you can chance information about the elements on your pages without having to rip out a bunch of code.

You will very likely find that the tests fail because they expect the list of results that google returns to be exactly the results that were there when I last checked in the code.

Fortunately, I've written code to help regenerate the YAML files from current data.

First, run in debug mode:

bin/runtest.rb -D

This will spout a bunch of crap, but it will also write copies of the HTML, as Selenium sees it (which is as modified by javascript at the time), to /tmp/, and then you can use scripts to snarf the data.

To actually regenerate the yaml:

bin/yaml_regenerate.sh yaml/BasicSearch_000_basic.yaml

If you look in yaml/BasicSearch_000_basic.yaml, you can see the actual yaml generation parameters:

#################################################################################
# BEGIN AUTOGENERATED BY bin/yaml_generate.rb with arguments:
# "-f" "/tmp/goog4" "-x" "//div[@id='ires']" "-a" "text" "-H" "basic_search_em_data" "-R" "/em[^/]*$"
#################################################################################

This means that you can regenerate that portion of the file, assuming that /tmp/goog4 has the right data, with this command:

bin/yaml_generate.rb "-f" "/tmp/goog4" "-x" "//div[@id='ires']" "-a" "text" "-H" "basic_search_em_data" "-R" "/em[^/]*$"

Altering Tests From The Command Line

If you have a complicated test matrix, writing seperate tests for each possible case can get very annoying, thus it's possible to change the main data set (called $yaml_data in the code) from the command line:

bin/runtest.rb -c 'base_url=google.co.jp'

This will run the tests with a different server_url in $yaml_data, which will make things break pretty badly. :) You can use as many -c options as you like, and the format is just x=y

To support this sort of variable replacement, all of the library functions that take only a $yaml_data key will also perform erb replacement just before actual use; look for "base_url" in the yaml files to see examples.