How do I use Watir::Waiter::wait_until to force Chrome to wait?
Asked Answered
V

3

17

I'm trying to tell my watir script to wait for an ajax-injected login box to open up. I am using watir-webdriver, and testing in Chrome. I cannot get wait_until to work, as commented in the below (simplified) script.

require "rubygems"
require "watir-webdriver"
b = Watir::Browser.new(:chrome)
site = "www.example.com"
b.goto site

puts "Click on Sign In button"
b.link(:id, 'btnLogin').click

puts "Waiting for the username/password dialog to show up"

# Below line does *not* work
# Throws this error: "uninitialized constant Watir::Waiter (NameError)" 
Watir::Waiter::wait_until { b.text_field(:id, 'username').exists? }

# Below line does *not* work
# Throws this error: "undefined method `wait_until' for main:Object (NoMethodError)" 
wait_until { b.text_field(:id, 'username').exists? }

# Below line *does* work, but I don't want to use it.
sleep 1 until b.text_field(:id, 'username').exists?

Is Watir::Waiter an IE-only class? Or what am I doing wrong, the sleep 1 wait method works just fine. I am new to Ruby and watir, I literally just picked this up yesterday so I'm half expecting this to be a result of my noobaciousness.

In case it is relevant, I am working on a mac (OSX v. 10.6.5).

Vandiver answered 4/12, 2010 at 22:54 Comment(0)
B
28

Do this first:

require "watir-webdriver/wait"

Then try these:

1

Watir::Wait.until { ... }

2

browser.text_field(:id => 'username').when_present.set("name")

3

browser.text_field(:id => 'username').wait_until_present

Note that "present" here means "the element both exists and is visible".

Backfield answered 5/12, 2010 at 1:54 Comment(1)
Thank you. That solved it, and thanks also for pointing out the wait_until_present and when_present methods. Is there an online source for what you have showed me? My googling has been pretty useless, apparently...Vandiver
D
2

You can also set timeout with browser.it will wait for it 700 seconds, like this.

client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 700 # seconds � default is 60 second
ie=Watir::Browser.new:firefox, :http_client => client
Dread answered 8/10, 2013 at 13:54 Comment(0)
E
1

I run into the same issue few weeks ago. The point is that Watir::Wait.until{} waits ONLY for the main page to load (tested mainly on Firefox). If you have some JavaScript code loading other components, these are not waited for.

So, the only solution is to pick and element and explicitly wait for it to appear (using methods 2 & 3).

Evolution answered 24/6, 2013 at 13:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.