1
votes

The page I'm using watir-webdriver on will often not load completely for maybe like a minute because of the many external scripts I'm loading/using. This of course means that I have to wait needlessly for a minute when I could have performed the checks I wanted to in 10 seconds after the browser I'm controlling (Firefox) started loading the page.

I noticed that even though my target elements become immediately available in the page, the browser.div(:id => 'some_id').present? method hangs until the page is fully loaded. I noticed that if I press on the stop button in the browser window, watir will immediately continue successfully with my tests (the present? method un-hangs). Is there a way to avoid this behavior of the present? method and be able to tell that my target elements are present without having to wait for the page to fully load?

2

2 Answers

1
votes

I've experienced this issue before as well. The method I employed, which rides on the idea you mentioned about hitting the stop button, is as follows:

begin
  browser.wait_until{browser.div(:id => 'some_id').present?}
rescue
  browser.send_keys :escape
end

expect(browser.div(:id=> 'some_id').present?).to be true

This, by default, gives that div 30 seconds to appear (you can insert a smaller value then 30 if you prefer), otherwise it causes watir to hit 'escape' which will stop any remaining background page loading and resume the test.

0
votes
.present? 

As I understand it checks to see if an element exists AND is visible.

What I would suggest you try, is

.exists? 

This will simply check that the element exists, and doesnt care if its visible or not.

Just for reference, there is also a

.visible?

Which just makes sure the item is visible, though I dont ever use that one.