There are times, we hope rare, when we need to attach an Internet Explorer window where not all of the ready states are complete. Here is an example where the window contains several frames and one of the frames never gets beyond ready state ‘loading’.

Frame in loading state

Watir‘s normal #attach method waits for the overall document to get to ready state ‘complete’ and it also waits for all frames in the page to get to ready state ‘complete’. In this example the frame that stays in ‘loading’ hangs the script.

Nearly always we’ll want to wait until everything in the window is complete. Instances like this should be rare and are either due to errors in the application code or some condition that prevents the particular frame from completing that is unavoidable or ‘normal’.

In this case, however, we need to keep going and do not need to access the frame that is still ‘loading’.

One solution is to use Watir::IE::find(how, what). This does what Watir::IE::attach does but skips the wait process. That does mean we have to use some sleep or wait_until commands to let the window stabilize before trying to manipulate it. Some sample code:

Watir::IE#close also calls the wait process that wants to see everything in ready state ‘complete’. For this we need to step outside of Watir proper and use AutoIt (included in the Watir gem).

Using attach is always preferred but this alternate works in a pinch.


Enhanced by Zemanta