Friday, September 25, 2009

Watir Tutorial - How to check for web component rendering

For any web page testing, almost very test engineer has the dilemma of how much wait is optimal for the page/certain component to render. How much time will it take to render the component before you can check that components or the enclosed in the components depends on so may parameters and your guess is as good as mine. If you wait for less, you get to many false failures. If you wait more, you test execution get sluggish.
You can always use this trick, few lines if extra code, but make you test execution much efficient - sleep for small duration, check for component in a loop and break out soon it is available.
Here is the test automation scenario. You have a web page with the form and you have to automate filling the form fields and form submission. So if you try to check for components using assert() or try to edit the form fields before the form is rendered on the page, you test will fail just because the page took longer to load. If you take the worst case scenario and put 30 sec of wait time, it will be annoying for person who will execute those test cases, as he/she have to wait for that duration everytime.
Following approach will address this issue, just use any looping structure you are comfortable with, sleep for small duration and check if the form is rendered, break out of the loop soon form is available and continue with rest of the test case.
Sample Code:
tries = 0
until ($browser.form(:id, "service-form").exists? or tries > 5)
sleep 2
$browser.logger.info( "waiting...")
tries = tries + 1
end

1 comment:

  1. Hi Shrikant,

    Do you prefer your above methodology over -
    Watir::Waiter.wait_until(10) ($browser.form(:id, "service-form").exists?

    And if so, why?

    Thanks,
    Darryl

    ReplyDelete