# Question on watir

**URL:** <https://www.ruby-forum.com/t/question-on-watir/224437>\
**Category:** Ruby\
**Created:** [November 12, 2012, 5:45am UTC](https://www.ruby-forum.com/t/question-on-watir/224437 "2012-11-12T05:45:39Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [November 20, 2012, 12:39pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/21 "2012-11-20T12:39:00Z")

</div>

Firstly, please start any new question as a new thread so others can  
find answers more easily in future.

Anyways, you want to look into this kind of thing:

> **[Ruby - Exceptions](http://www.tutorialspoint.com/ruby/ruby_exceptions.htm)**
>
> Ruby Exceptions - The execution and the exception always go together. If you are opening a file, which does not exist, then if you did not handle this situation properly, then your program is considered to be of bad quality.

If you’re receiving a Timeout exception from Watir but your program  
keeps running then you’re probably handling the exception. What you need  
to do is terminate when you hit that kind of exception. If you’re  
calling a method and the exception is handled inside that method, you’ll  
need to return a value which will be recognised by the parent and used  
in a decision as to whether to continue or terminate execution.

For example (caveat lector, untested code):

* * *

def mymethod  
b = Watir::Browser.new  
b.goto ‘[www.purple.com](http://www.purple.com)’  
b.close  
return true  
rescue Timeout::Error  
puts “Page load timed out”  
return false  
rescue =\> e  
puts e  
return false  
ensure  
b.close rescue nil  
end

10.times do |iteration|  
unless mymethod  
puts “Error occurred, terminating process”  
exit  
else  
puts “Succeeded: #{iteration}”  
end  
end

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [January 21, 2013, 1:34pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/22 "2013-01-21T13:34:09Z")

</div>

Without a more descriptive response than “i couldn’t succeed” ,it’s  
impossible to suggest anything relevant to combat your problem.  
Other than “USE RUBY 1.9.3!”, of course.

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [January 22, 2013, 6:58pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/23 "2013-01-22T18:58:08Z")

</div>

hi,

we usually run ruby program by giving the command

> ruby prog.rb

But here our source code is needed. But Is there any way we could  
convert this file into .exe and then run the program directly by double  
clicking that file or directly typing name of the file in the command  
prompt?

Raj

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [January 21, 2013, 1:38pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/24 "2013-01-21T13:38:04Z")

</div>

I used ‘gem install watir -v 3.0.0’ ,But i failed to install this. Error  
occurred suggesting me to install watir 1.9.0.

Raj

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [January 23, 2013, 2:30pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/25 "2013-01-23T14:30:53Z")

</div>

Ok thank you.

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [January 22, 2013, 9:28pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/26 "2013-01-22T21:28:51Z")

</div>

If you want to just double click the file and run it then you can  
associate “.rb” files with the Ruby interpreter.

If you want an executable, look up [http://rubyforge.org/projects/ocra/](http://rubyforge.org/projects/ocra/)

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [January 23, 2013, 7:43pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/27 "2013-01-23T19:43:27Z")

</div>

Rather than tell you how to fix your specific problem, I’ll give you a  
link on how to handle Exceptions; so you can broaden your knowledge and  
apply it appropriately.

> **[Ruby Exceptions And Exception Handling](https://skorks.com/2009/09/ruby-exceptions-and-exception-handling/)**
>
> Ruby exceptions and error handling is not the most remarkable feature of the Ruby language. Infact, the way Ruby deals with exceptions is strikingly similar to other languages (such as Java etc.). But, I think all the stuff I’ve been writing about...

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [January 24, 2013, 8:05am UTC](https://www.ruby-forum.com/t/question-on-watir/224437/28 "2013-01-24T08:05:43Z")

</div>

Ok Thank you.

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [January 28, 2013, 8:34am UTC](https://www.ruby-forum.com/t/question-on-watir/224437/29 "2013-01-28T08:34:17Z")

</div>

hi,

They have explained how can we handle the exception. Now, my question  
is, I am working in a big project, there are many screens, we have  
written unique script for each and every screen. So the server may fails  
to respond at any time or at any script while execution. So how can i  
write the exception common for all screen? My ultimate goal is, i need  
to inform the user that server has been down at this test case. how  
would i do this?

Raj.

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [January 23, 2013, 3:20pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/30 "2013-01-23T15:20:13Z")

</div>

Hi,

I am running 500 test cases. I am facing some problem here,that is, if  
server has been down for sometime,we couldn’t access the web page,So  
watir fails to access the web page, and it stops it’s running. and then  
again i need to start the process manually as soon as server has been  
up,Is there any way we can avoid this manual interference,what i meant  
to say is,Can we make our program for continual checking until server  
has been up and then it has to start the program automatically?

Raj

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [January 28, 2013, 3:37pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/31 "2013-01-28T15:37:26Z")

</div>

hi Joel P.

Thank you, I am running in a single machine, For each screen I have  
defined corresponding function,when one particular screen has to be  
automated, corresponding function will be invoked. Basically, from one  
function all other function are getting called. And I haven’t thought  
class usage anyway. I will try put it in class as packed one. Thank you  
for you suggestions.

Raj

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [January 28, 2013, 11:21am UTC](https://www.ruby-forum.com/t/question-on-watir/224437/32 "2013-01-28T11:21:07Z")

</div>

It depends what you mean by “screens”. If you’re referring to the  
different pages your script is accessing and the scripts are all running  
on the same machine, you can join your tests into a single environment  
so that the error handler can encapsulate all of them. Look into using a  
Class or Module.

If you mean you’re running individual scripts on different machines,  
then you have the option of placing a log file in a shared location  
which any failing routine can append to, so that you can trace errors  
after the tests have been run, or check the file before running the  
tests to see whether this iteration has already failed and can be  
skipped.

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [January 28, 2013, 3:59pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/33 "2013-01-28T15:59:15Z")

</div>

Hi ,

could you please write the small code about how to write the exception  
handling inside the class common to all function.?

Raj

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [January 28, 2013, 4:55pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/34 "2013-01-28T16:55:03Z")

</div>

Hi Thank you so much, what do you mean by “don’t repeat yourself”? I  
think class form would be much more easy. Any way I will try it out.

Raj

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [January 28, 2013, 4:48pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/35 "2013-01-28T16:48:19Z")

</div>

You basically have 2 options…

You can handle the exception only at parent level and not bother with  
local error trapping:

* * *

begin

def method1  
#Content  
end

def method2  
#Content  
end

method1  
method2

rescue  
#Handle exception  
end

* * *

Or you could handle each exception at a local level and then hand it up  
to the next handler:

* * *

begin

def method1  
#Content  
rescue  
#Do something here  
raise #Pass the error up to the next level  
end

def method2  
#Content  
rescue  
#Do something here  
raise #Pass the error up to the next level  
end

method1  
method2

rescue  
#Handle exception  
end

* * *

You don’t strictly need a class, a parent method would do but building a  
class system allows you to avoid rewriting the same code repeatedly by  
dealing with variables as arguments and leaving the surrounding  
structure as part of your methods. For more info on this, look up DRY  
(Don’t Repeat Yourself).

FYI, this is what it would look like as a class

* * *

class MyTestClass

method1  
#Do stuff  
end

method2  
#Do stuff  
end

rescue  
#Do stuff here  
end

* * *

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [January 28, 2013, 7:23pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/36 "2013-01-28T19:23:00Z")

</div>

Regarding DRY,  
I know that when running Watir tests from different scripts you’ll be  
rewriting a lot of the same code, like starting and closing the browser,  
for example. By using a single running environment (or requiring tools),  
you can set up the repetitive tasks into methods, meaning you need only  
write the code once.  
Unless of course you’re ahead of the game and you’re only writing the  
unique steps into each of your scripts while requiring the common  
methods from elsewhere, in which case you can ignore me 🙂

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [January 29, 2013, 5:46am UTC](https://www.ruby-forum.com/t/question-on-watir/224437/37 "2013-01-29T05:46:16Z")

</div>

OK Thank you.

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [January 29, 2013, 7:03am UTC](https://www.ruby-forum.com/t/question-on-watir/224437/38 "2013-01-29T07:03:21Z")

</div>

hi,

please consider this code below

these three function has been written in separate page.  
def fun2  
end

def fun3  
end

def fun4  
end

* * *

def fun1(argument)  
begin  
if(argument == this)  
fun2  
end  
end  
begin  
if(argument == some)  
fun3  
end  
end  
begin  
if(argument == something)  
fun4  
end  
end  
rescue  
#some statement  
end

* * *

class fun  
for loop  
fun1(argument)  
end  
end

I hope you can understand above simple structure of my program.when the  
first time “fun1” called,assume inside the fun1, fun3 is getting called,  
and now if server has been down, execution should not continue, but it  
has to be terminated. But regardless of server state, program continues  
to execute.could you give me suggestion where to put this exception to  
avoid this execution?

Another problem is, if server has not been down, and there is some error  
in function, it has to terminate the funtion, but next execution has to  
continue. So how could I handle this situation?

Raj

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [January 29, 2013, 10:01am UTC](https://www.ruby-forum.com/t/question-on-watir/224437/39 "2013-01-29T10:01:48Z")

</div>

class MyTestClass

method1  
#Do stuff  
end

method2  
#Do stuff  
end

rescue  
#Do stuff here  
end

you gave this example for writing exception inside the class, but it is  
not working for me, consider the program below,

class Raj  
def hello  
puts ‘hello’  
1/0  
end  
def hi  
puts ‘hi’  
end

rescue Exception =\> e  
puts ‘the exception has been raised’  
end

a=Raj.new  
a.hello  
a.hi

Here exception error of division by 0 is not caught by exception, why it  
is so?

Raj

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [January 29, 2013, 10:35am UTC](https://www.ruby-forum.com/t/question-on-watir/224437/40 "2013-01-29T10:35:43Z")

</div>

It’s probably best to put the errorhandler in the script which is using  
the class. Apparently it doesn’t like rescuing inside a class like that.  
This approach works:

irb(main):001:0\> class Raj  
irb(main):002:1\> def hello  
irb(main):003:2\> puts ‘hello’  
irb(main):004:2\> 1/0  
irb(main):005:2\> end  
irb(main):006:1\> def hi  
irb(main):007:2\> puts ‘hi’  
irb(main):008:2\> end  
irb(main):009:1\>  
irb(main):010:1\* rescue Exception =\> e  
irb(main):011:1\> puts ‘the exception has been raised’  
irb(main):012:1\> end  
=\> nil  
irb(main):013:0\> begin  
irb(main):014:1\* Raj.new.hello  
irb(main):015:1\> rescue  
irb(main):016:1\> puts ‘the exception has been raised’  
irb(main):017:1\> end  
hello  
the exception has been raised  
=\> nil

[Previous page](https://www.ruby-forum.com/t/question-on-watir/224437.md?page=1)

[Next page](https://www.ruby-forum.com/t/question-on-watir/224437.md?page=3)
