Watir setting up a test case to run

August 6, 2008

Ok we’ve got code that works! Now how do we run the test case?

If you have the set of commands all in one .rb file, simply double click it and it should work. However, if you’re setting up a framework and want to be able to use repeating code from different scenarios, it would be a good idea to break the code apart into modules (or functions if you want to call them that).

Here’s one way I set it up and it works so far. Still in the process of investigating other ways to setup the Watir framework and test script runs.

Setup a test case run for Watir:

1. Set up a module that contains the test definitions (methods/actions)
Example for logging in and logging out.
File: login_logout_defs.rb

module LoginLogoutMod

def go_to_login
$ie.link(:href, /login/).click

if $ie.text.include? “Login”
puts “- Login page found, so login!”
else
puts “- Security Alert must be in the way. Click to bypass”
$ie.link(:id, “overridelink”).click
puts “- Clicked the continue link in IE 7″
sleep 5
end
end

def login
$ie.text_field(:name, “login_username”).set($user)
$ie.text_field(:name, “login_password”).set($password)
$ie.button(:id,”login-text”).click
puts “- Logged in with user <#{$user}>”
end

def logout
$ie.link(:href, “http://#{$url}/logout”).click
end

end

2. Now set up the test case file to run which calls the definitions from the modules.
File: login_logout_test.rb

dir = File.dirname(__FILE__)
require “#{dir}\/loginLogout_defs”

require ‘watir’
require ‘watir/testcase’

$url = “www.newsqa.com”
$user = “user1″
$password = “password”

$ie = Watir::IE.new
$ie.set_fast_speed

class TC_Login

Previous post: Watir Examples continued…

Next post: Running a Watir test script file