Question on watir

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:

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

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.

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

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

Ok thank you.

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/

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.

Ok Thank you.

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.

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

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

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.

Hi ,

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

Raj

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

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


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

OK Thank you.

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

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

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