Running a Watir test script file

August 6, 2008

Here’s a simple way to write a ruby file that calls other DEFINITIONS from MODULES that you create.

1. Create the basic methods (actions) you need to log in and log out. Name this file loginLogout_defs.rb

module LoginLogout #class

def go_to_login #methods/actions
$ie.link(:href, /lgn\/login/).click
### Security Alert box ###
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″
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}/lgn/logout”).click
end

end

2. Now create a ruby file that uses these methods. This way, your other test cases can call the same code and if it needs to be fixed, you only have to fix it in one location :)
loginTest.rb. Simply double click to run this file.
Note: f.puts opens a file and writes data to it

dir = File.dirname(__FILE__)
require “#{dir}\/loginLogout_defs” #Definition file
require ‘watir’

include LoginLogout #Module

$url = “www.newsqa.com”
$user = “user”
$password = “password”
$time = Time.now.strftime(”%m/%d/%y-%H:%M:%S”) #$time = Time.now.to_s
$fileDate = Time.now.strftime(”%m%d%y_%H%M%S”)

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

File.open(’troubleshoot’+$fileDate+’.txt’, ‘a’) do |f|
f.puts “Script started on: ” + $time
$ie.goto($url)

go_to_login #call the DEF from the MODULE

login #call the DEF from the MODULE

logout #call the DEF from the MODULE

$end = Time.now.strftime(”%m/%d/%y-%H:%M:%S”)
f.puts “Script ended at: ” + $end
end

Previous post: Watir setting up a test case to run

Next post: Ruby cheat sheet, Watir cheat sheet