Tuesday, August 9, 2011

Watir Tutorial: Error Recovery

So your script is happily humming along, testing myriad little tests, and then despite all your best efforts, it hangs!  Tragedy!  Sometimes despite our best efforts, it will still crash.  But it doesn't have to be like that.  No sir.

Ruby comes with error handling and with just a few lines of code, you can get your script to recover itself when it hangs, as it sometimes will.

If you need a primer on exceptions and error handling, I recommend Alan Skorkin's post about it.  As he covers a great deal of what you need to know, we will just be talking about the Watir code you will use.

And here it is:

  1. while !(b.frame(:id => "e1menuAppIframe").text.include? "Welcome!")
  2.   autoit.Send("^!l")
  3. end
  4. retry

What this will do, is hit cancel until you are back at the welcome screen.  So by placing this in the 'rescue' portion of your error handling and the begin statement after where you would initially be seeing the welcome screen in the first place, it will work like a dream.

I understand this won't work for all cases, but for some it will, and for those that it will, it makes the testing go that much smoother!  Enjoy~

No comments:

Post a Comment