Question on watir

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
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


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.

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?

Ok thank you ,it’s very helpful.

Raj

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

Yep, thank you very much, I exactly understood.

Raj

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

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.

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

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

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.

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

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:

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

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.

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

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

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

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.

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
"