# 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:** 3

<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:23am UTC](https://www.ruby-forum.com/t/question-on-watir/224437/41 "2013-01-29T10:23:20Z")

</div>

You’re rescuing the exceptions inside fun1, but your loop inside class  
fun will continue because you’re not passing the exception any higher.

Catch specific types of exceptions.  
[http://wtr.rubyforge.org/rdoc/1.6.5/classes/Watir/Exception.html](http://wtr.rubyforge.org/rdoc/1.6.5/classes/Watir/Exception.html)  
This one should cover server outages: Watir::Exception::TimeOutException

You’re kind of missing the point with the class, it’s intended to  
encapsulate your methods.

Also this:

* * *

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

* * *

is equivalent to this:

* * *

def fun1(argument)

if argument == this  
fun2  
elsif argument == some  
fun3  
elsif argument == something  
fun4  
end

rescue  
#some statement  
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 29, 2013, 1:23pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/42 "2013-01-29T13:23:34Z")

</div>

If you want the Exception to be passed up to the next level…  
Instead of this:

1/0  
rescue Exception =\>e  
puts e

do this:

1/0  
rescue Exception =\>e  
puts e  
raise

A local rescue would be used if you wanted to do something specific to  
that method, otherwise just don’t rescue it there, it’ll be picked up by  
the error handler you’re using at the top level.

---

<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, 1:18pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/43 "2013-01-29T13:18:28Z")

</div>

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

Yes I understood now.

I have another here,

## please have a look at this code

class Raj  
def hi  
raja  
gopal  
hello  
end  
def raja  
puts ‘raja’  
end  
def gopal  
begin  
puts ‘gopal’  
1/0  
rescue Exception =\>e  
puts e  
end  
end  
def hello  
puts ‘hello’  
end  
end

## begin Raj.new.hi rescue Exception =\> e puts e end

here, the exception has been caught at the gopal function,So the  
exception which I have written for entire class hasn’t been invoked. So  
next hello also was called. But hello should not be invoked in my case,  
meanwhile I couldn’t take exception handling which is within gopal  
function because it has some other need. what can I do to solve this  
problem?

---

<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, 1:33pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/44 "2013-01-29T13:33:12Z")

</div>

Ok thank you ,it’s very helpful.

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, 1:34pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/45 "2013-01-29T13:34:34Z")

</div>

Raj pal wrote in post #1094182:

> meanwhile I couldn’t take exception handling which is within gopal  
> function because it has some other need. what can I do to solve this  
> problem?

To rescue something specific, rescue is like a case statement:

begin  
1/0  
rescue ZeroDivisionError =\> err  
puts ‘Rescued expected error’  
p err  
rescue =\> err  
puts ‘Rescued unexpected error’  
p err  
raise  
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 29, 2013, 4:24pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/46 "2013-01-29T16:24:21Z")

</div>

Yep, thank you very much, I exactly understood.

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 30, 2013, 7:52am UTC](https://www.ruby-forum.com/t/question-on-watir/224437/47 "2013-01-30T07:52:06Z")

</div>

hi,

I have some confusion with define method

class Raja\<Watir::testcase

define\_method(testName[i]) do  
puts ‘raja’  
end

end  
how this define\_method is automatically executed when we run the script?  
(the above program will not execute),I haven’t called this function by  
creating object, but when i run this script this method gets  
automatically executed. could you please explain me how it is working?

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 30, 2013, 10:28am UTC](https://www.ruby-forum.com/t/question-on-watir/224437/48 "2013-01-30T10:28:42Z")

</div>

I’ve never tried this before, but after a little digging I got this to  
work:

irb(main):001:0\> class Raja  
irb(main):002:1\> def initialize  
irb(main):003:2\> create\_method(:raja) {puts ‘raja’}  
irb(main):004:2\> end  
irb(main):005:1\> def create\_method(name, &block)  
irb(main):006:2\> self.class.send(:define\_method, name, &block)  
irb(main):007:2\> end  
irb(main):008:1\> end  
=\> nil  
irb(main):009:0\> Raja.new.raja  
raja

I used the “create\_method” example from the Module documentation, and  
then used initialize to trigger it when the class is created via “new”.  
Of course you could iterate through a hash or array to populate your  
class with multiple methods.

---

<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 30, 2013, 10:39am UTC](https://www.ruby-forum.com/t/question-on-watir/224437/49 "2013-01-30T10:39:11Z")

</div>

I am not sure whether you have answered my question. Actually my  
question was the define\_method was getting called when I execute the  
file, But there was no object creation is in my file like you did(  
Raja.new.raja). “define\_method” is getting called when I execute the  
file, So my question was who calls this define method. Actually it was  
declared inside Module.rb.

like

def define\_method(\*args)  
end

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 30, 2013, 11:31am UTC](https://www.ruby-forum.com/t/question-on-watir/224437/50 "2013-01-30T11:31:52Z")

</div>

I seem to be missing something. If I don’t have any .js files in my  
input  
directory, shouldn’t this fail and fall into rescue?

begin  
jsfile = Dir.glob("\*.js") # Get the javascript file that  
contains the  
real hiearachy.  
jsfile.each do |f|  
js\_contents = File.open(f, “rb”) {|io| io.read }  
process\_js(js\_contents)  
end  
rescue  
puts “The x7\_top.js file must be in the input directory.”  
exit  
end

- Wayne

---

<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 30, 2013, 11:47am UTC](https://www.ruby-forum.com/t/question-on-watir/224437/51 "2013-01-30T11:47:29Z")

</div>

Raj pal wrote in post #1094296:

> I am not sure whether you have answered my question. Actually my  
> question was the define\_method was getting called when I execute the  
> file, But there was no object creation is in my file like you did(  
> Raja.new.raja). “define\_method” is getting called when I execute the  
> file, So my question was who calls this define method. Actually it was  
> declared inside Module.rb.
> 
> like
> 
> def define\_method(\*args)  
> end
> 
> RAJ

Because you haven’t contained it within a method, it’ll execute when you  
run the file.

---

<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 30, 2013, 12:02pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/52 "2013-01-30T12:02:23Z")

</div>

hi,  
I think you have misunderstood,

## For an example consider the code below

class Raja \< Watir::TestCase

def test\_2\_Event  
puts ‘raja’  
end

def test\_3\_Followup  
puts ‘gopal’  
end

def test\_4\_hi  
puts ‘hi’  
end

def testhello  
puts ‘hiiii’  
end

## end

Now for the above class you no need to create the object, instead if you  
execute the file which contains this class, it will be executed  
successfully. So Watir::TestCase contains some thing which invoke this  
function name starts with “test”. Now what if we have 1000 files which  
starts with test, So we have a method “define\_method” which generate the  
function dynamically. This takes the name as an argument.for an example  
define\_method(test\_3\_Followup). Now my question how these function are  
getting invoked without any calling to these functions?

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 30, 2013, 12:15pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/53 "2013-01-30T12:15:08Z")

</div>

It looks like anything starting with “test” gets executed automatically  
in alphabetical order. It’s a convenience thing so you don’t have to  
manually make the list of all your methods to be run, you just name them  
in order: “test\_01\_DoThis”, “test\_02\_ThenDoThis”.

This looks useful:

> **[How to start writing web test scripts with Ruby and Watir](https://jimhmatthews.wordpress.com/2007/10/28/how-to-start-writing-web-test-scripts-with-ruby-and-watir/)**
>
> This blog entry is about how to start writing web test scripts with Ruby and Watir.  Watir is basically a library that lets you interface Ruby to Microsoft Internet Explorer (IE).  I originally pre…

---

<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 30, 2013, 12:19pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/54 "2013-01-30T12:19:36Z")

</div>

Yes this is what I meant, but now the problem is, how do I write the  
exception handling for such a class?

If I were to call the function ,then i would write the exception  
handling like,

begin  
Raja.new.hi  
rescue Exception =\> e  
end  
But now, there is no method calling. If so, how would I write the  
exception handling?

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 30, 2013, 12:25pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/55 "2013-01-30T12:25:23Z")

</div>

What do you want the handler to do? Skip to the next test, quit the  
program, begin the next test loop?  
I don’t have TestCase installed, so I can’t experiment with this, but  
experimentation is often the way forward. Try placing handlers in  
different places and see what they pick up.

---

<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 30, 2013, 12:38pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/56 "2013-01-30T12:38:26Z")

</div>

when the control in a particular method and server has been failed,  
entire program has to terminate, it should not call the next method.  
Handling with exception is the problem here, because it allows me to run  
the next test case.

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 30, 2013, 12:31pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/57 "2013-01-30T12:31:30Z")

</div>

There is nothing wrong with iterating over an empty list. This will not  
produce and error, you will need to check the list manually

---

<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 30, 2013, 12:44pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/58 "2013-01-30T12:44:46Z")

</div>

Yes, you are right , I need to report to the user that how many failures  
and how many successes, But however I need to tell to the user that It  
was failed since server was failed to responded.

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 30, 2013, 12:41pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/59 "2013-01-30T12:41:54Z")

</div>

I’m pretty sure the whole point of using testcase is so that you can  
report back the number of failures and successes rather than terminate  
at the first failure. If you really want to terminate on failure you  
could just do this without using testcase.

---

<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 30, 2013, 1:17pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/60 "2013-01-30T13:17:47Z")

</div>

I think you’ll need to do something specific around each page navigation  
which rescues the Timeout error specifically and then calls “exit” after  
outputting the appropriate failure message.  
I’m not sure you’re supposed to be able to rescue outside of individual  
testcases. perhaps you could run your methods through something which  
appends a handler?

modified\_method = "#{current\_method.reverse.sub(/\bdne\b/, ‘’).reverse}  
rescue Watir::Exception::TimeOutException  
puts ‘Server failed to respond’  
exit  
end  
"

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

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