chaitanya
chaitanya

Reputation: 1601

intercepting the onload event fired by the browser in watir

I have a unique situation over here. I have a button on a form which produces a popup if there are some errors in the form. [I know this is not good practice, but the developers of the product would not be changing that behavior!] The form navigates to a different page if all the required fields are correctly populated. Now, I need to write a script in order to click the "Submit" button on the form which either might produce a popup or navigate to the next page.

I have the used the click_no_wait on the "Submit" button and handled the popup using AutoIt as per Javascript Popups in Watir. Now, if all the information is valid and the form navigates to the next page, I use a delay in the script by following some of the techniques described in How to wait with Watir. I am using a Watir::wait_until() to wait in the script.

Now sometimes because of some network issues, it takes time to go to the next page (report-generation) page when the form is submitted and thus the script fails because of the timeout value specified in the wait_until.

I was wondering whether there is a way to intercept the onload event of the HTML page in Watir, since the onload event isn't fired until the entire page is loaded. By that way I could have an accurate estimate of the timeout value and not experiment with it. Thus, my script will pass 100% rather than say 98% right now.

Thanks for any help on this topic.

Upvotes: 0

Views: 251

Answers (1)

anonygoose
anonygoose

Reputation: 739

You could try setting up a rescue for the time out, then looping a reasonable amount of times (2 or 3?) if it encounters a timeout.

E.g.

# All your button clicking and autoit stuff here
i = 0
begin
  b.wait_until{ # the thing you're waiting to happen }
  rescue TheSpecificTimeOutException 
  # Sorry I can't remember it, the command prompt will tell you exactly 
  # which one
   if i < 3
     i += 1
     retry
   else
     raise
   end
 end

I'm sure i'll have messed something up in the above, or there'll be more concise ways of doing it, but you get the idea. When it times out, give it another few tries before giving up.

Upvotes: 1

Related Questions