# Newbie ruby watir exception handling

**URL:** <https://www.ruby-forum.com/t/newbie-ruby-watir-exception-handling/187083>\
**Category:** Ruby\
**Created:** [April 16, 2010, 5:38pm UTC](https://www.ruby-forum.com/t/newbie-ruby-watir-exception-handling/187083 "2010-04-16T17:38:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![viavr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/4bbf92/32.png) [@viavr](https://www.ruby-forum.com/u/viavr)\
**Post date:** [April 16, 2010, 5:38pm UTC](https://www.ruby-forum.com/t/newbie-ruby-watir-exception-handling/187083/1 "2010-04-16T17:38:20Z")

</div>

We’re testing. This script below will go through the list of url’s, and  
on each url, write out the results of the script to a file. These  
results are a) the last 3 digits of the url string  
b) the 1st href on the page  
c) the 3rd href on the page  
a,b,c are seperated by a tab. each url result array is on a separate  
line.

We run the following ruby-watir test script  
require ‘rubygems’  
require ‘hpricot’  
require ‘watir’  
browser = Watir::Browser.new  
[  
('[http://www.example.com/add1.htm?prod=123’),(‘http://www.example.com/add1.htm?prod=124’),('http://www.example.com/add1.htm?prod=125](http://www.example.com/add1.htm?prod=123'),('http://www.example.com/add1.htm?prod=124'),('http://www.example.com/add1.htm?prod=125))].each  
{|url|  
browser.goto url  
sleep 4  
browser.link(:index, 1).href  
browser.link(:index, 3).href  
target=url.slice(-3…-1)+“\t”+browser.link(:index,  
1).href+“\t”+browser.link(:index, 3).href  
outfile = File.new(“test.txt”, “a”)  
outfile.puts target  
}  
browser.close

################################  
if the javascript on the targeted url page loads slowly, or not at all,  
ruby throws an exception  
Watir::Exception::UnknownObjectException, since the 1st or 3rd link  
aren’t there.

we used “sleep 4” to slow down the script, but would rather have the  
test run as fast as it can.

since I am a newbie to ruby, my exception handling block code attempts  
aren’t working - so my syntax is off. I’ve gone through more than a few  
hours of trying various ways of handling the exception with ‘rescue  
Exception =\> e’

The exception handling should simply put an “unknown” as the values for  
the missing links, and then go to the next url in the array.

This is our first ruby attempt. On another note, we landed on  
ruby/watir/hpricot because we didn’t locate any testing tools that will  
run in FreeBSD’s command line. This test script requires the browser to  
process the javascript on the targeted url.

Does anyone know of a nix solution to testing instead?
