sivaguru perumal
sivaguru perumal

Reputation: 103

Watir Webdriver wait for page to load

How to check whether a page is loaded or not using watir webdriver?

When i googled, I found many suggesting to check for a particular element which will be present only after the page is loaded. This doesn't help my requirement because the page load happens when I change the value of a list box and it doesn't take me to a new page. The elements and most of the values remain same after this page load. Therefore i need something like waitForPage in selenium RC.

My Scenario 1. There are three list boxes(A,B,C) in a page 2. When I change the value in List A, a page load happens (this will update the values for list box C. These values are dynamic.) 3. Then I have to change a value in List Box B.

In the above scenario if i have a List B element check after step 2, it says the element is present but while changing the value it fails saying the element doesn't exist

$browser.frame.select_list(:id,"listA").select("aaa")
while !$browser.frame.select_list(:id,"listA").exists? && i<=20
    sleep 1
     i+=1
end
raise "Element not found" if i > 20
i=1
while !$browser.frame.select_list(:id,"listA").include?("bbb") && i<=20
    sleep 1
    i+=1
end
raise "Option not found" if i > 20
$browser.frame.select_list(:id,"listB").select("bbb")

The script fails with element not found exception either while checking for presence of option in list B or while changing the value for list B

Upvotes: 1

Views: 2000

Answers (2)

Chuck van der Linden
Chuck van der Linden

Reputation: 6660

If you are truely looking at a full page reload, and there is no AJAX going on (rare these days, where the trend is client side rendering) then the automatic waiting for page load that watir does after clicking links or submitting forms ought to work. Using developer tools to watch net traffic will tell you if the page is actually doing a true reload, or it just looks visually like that is the case when client side js re-renders the page.

If you are faced with AJAX requests, where the submit is handled by a javascript request to some service, typically a REST Service, and a portion of the page updated, there are a few things you can do.

  1. Decorate the code that calls on the element you are waiting for with the .when_present method. For example:

    $browser.frame.select_list(:id,"listB").when_present.select("bbb")

  2. If your client side code uses jQuery, you can construct a helper method that uses Watir::Wait.until to wait for browser.execute_script('return jQuery.active == 0') to be true and stay that way for some small period of time. (.active is the count of active jQuery reqests waiting for a response)

  3. Similar to other suggestions wait for a paticular page element to be present.

  4. Combine the above, do #2 followed by one of the others.

Upvotes: 1

Smooyk
Smooyk

Reputation: 409

Try to select all elements with id equals to "listB", iterate through them and check whether particular element is visible or not. I don't particular remember how it should looks like in watir, something likes this I think:

$browser.frame.select_lists(:id,"listB").each do |list|
    if list.visible?
        list.select("bbb")
        break
    end
end

Upvotes: 0

Related Questions