Advanced Topics

Handling web sites that automatically refresh

Many sites use javascript to automatically refresh the user's browser while it processes the request in the background. Once the refresh is complete, script execution may continue because IE thinks the page has finished reloading.

To ensure that the page has completely loaded, it is sometimes necessary to add wait statements to wait for a specific object before continuing to execute the script.

Watir has a wait_until statement that is effective in handling these situations. To use the wait_until statement, we do the following:

wait_until(time_to_wait_in_seconds) { condition }

Example:
wait_until (60) { $IE0.contains_text('some text') }


In this example, we will wait for a maximum of 60 seconds for 'some text' to appear in browser $IE0 before continuing with script execution.

Handling AJAX heavy web sites

Websites that use AJAX usually make XMLHttp requests when some user action triggers it. When this request is complete, the text on the page will most likely update as well. We can use the same method as above and wait until some text appears before continuing.