Hi Nathan, thanks for the follow-up.  I do have it working, as per my last
message in this thread, but I'll offer some additional info here since you
asked.

The $something_went_wrong variable is a legacy variable from when I first
wrote these scripts using Watir and I was trying to tell the script that a
particular 'test' method did not successfully complete.  It isn't really
used in the write_status() method.  That variable is used by a
"complete_walkabout()" method that runs at the end of the script -- which
cleans up the environment (e.g. closes files) and makes a decision based on
the successful completion of the test (i.e. whether or not "something went
wrong").

I'm in the process of refactoring the logic and how the code works, and am
taking it one step at a time.  (I may even end up getting rid of that
variable.)  Basically what the script does is this:  Open IE, walk the web
app's site map, capture some performance metrics and close the browser.

What I *want* the script to do is recover from any page or link that it
fails to connect to.  The script *used* to do this in a very neat way, but
that way wasn't transferable to the new Ruby script structure that I'm
using.

So how do you get the script to recover from an error and make it through
right to the very end? (I asked myself)

I put all the "walk the site map pages and capture metrics" code into
individual methods and I needed to find a way to capture any exceptions
raised.  I'm not doing any explicit [test] assertions in these scripts, but
the effect is the same.  i.e. if it fails to connect to a page, the script
fails on the next line and it stops cold.

So I tried to wrap it in a rescue block like this:
----
begin
  call_page_walking_method()
rescue => e
 #do something
end
#keep going
----

However, as soon as I realised that I needed to do this for *every* page set
I was going to call, I put that (above) code into a method on its own.

The problem is that the above rescue block *doesn't* work.  Ruby didn't get
what I was trying to do.  If the called method failed then the script just
stopped, it didn't return and get rescued like I thought it would.

That's when I discovered that you can put the rescue block *within* the body
of a method.  So I moved it to the method that does the actual walking, and
now it recovers nicely.  Voila!

So I learned one way how a regular Ruby method is different from the
Test::Unit::TestCase class' 'test' method.  The TestCase class will still
proceed to a 'teardown' method if any individual test method terminates
unexpectedly, but a regular Ruby method just stops where it failed.

Now I know how to get my non-TestCase (Ruby/Watir) scripts to recover
gracefully from exceptions.  Cool.

Does that help clarify things?  If not, mail me off-list and we'll chat.

Cheers.  Paul C.
_______________________________________________
Wtr-general mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/wtr-general

Reply via email to