0

Is there any function available in Watir Webdriver to wait for a new window tab to load?

What happens right now is the tests are not waiting for the tab to load and my scripts are failing because of that.

I know that i can wait for some elements available in the web page. But i dont want to do that for each web page. If there is no method available, i would like to write a new method for the same as a library in my framework.

I am new to watir webdriver and ruby programming. So please help me to write a library function in case not available...

Sukuva
  • 812
  • 1
  • 7
  • 27
  • Is the problem (1) that Watir is looking for the tab before it is opened or (2) that Watir is looking for an element that is asynchronously loaded before it is created? – Justin Ko Jan 15 '15 at 16:14
  • @JustinKo What i am observing is : Tests are not waiting for page to load. Yes, watir is looking for an element even before the page load. Because of that tests were failing due to object not found or not visible or something like that. If i add some implicit wait there, it works fine. So definitely there is some issue. – Sukuva Jan 15 '15 at 16:31

1 Answers1

1

I may be misunderstanding, but your question seems to say two different things. On the one hand, you say that Watir doesn't wait for the page to load. But then you say you know that Watir can wait but you don't want to do that on every page.

In my experience, and again I may be misunderstanding, if Watir is going too fast and your page isn't loading, you have no choice but to have Watir wait on any page where this is a problem. What else did you have in mind?

If your page loads within 30 seconds you can use .wait_until_present or .when_present. (i.e., Watir will move on as soon as the element is available and will time out after half a minute).

For example, if your loaded page has the text "Hello" in a div, you can make Watir wait for up to 30 seconds for that text to appear by

browser.div(:text, 'Hello').wait_until_present

and then let your test pick up where it left off.

Or, if there is (as an example) a button with id "buttonId" that you want to click when the page loads, you can make Watir wait for up to 30 seconds until the button is loaded and clickable:

browser.button(:id, 'buttonId').when_present.click

I almost always use .wait_until_present or .when_present. for each new page load. Under normal circumstances 30 seconds is long enough for the page to be ready for Watir, and these two methods work very well.

See also http://watirwebdriver.com/waiting/

pjd
  • 1,123
  • 8
  • 17
  • I totally agree with the suggestions you provided. What i am looking for is something like this.. .wait_for_page_load() should wait for the complete page load instead of waiting for a button or an element... – Sukuva Jan 21 '15 at 17:18
  • Can we check the ready state and status of the page so that I can stop waiting once it becomes 4 & 200.. ? This is for the wait for Page Load function. – Sukuva Jan 23 '15 at 14:41
  • I don't think it is possible to check the page status with Watir-Webdriver. See http://stackoverflow.com/questions/6509628/webdriver-get-http-response-code/6512785#6512785 . You might investigate adding in the Page Objects gem. I have never used it, but that gem might be able to check if a page is loaded. See https://github.com/watir/watir-webdriver/wiki/Page-Objects . – pjd Jan 24 '15 at 06:28
  • If that doesn't work, I am out of suggestions and you may have to write your own method as you proposed at the start. – pjd Jan 24 '15 at 06:29