简体   繁体   English

如何使用Watir :: Waiter :: wait_until强制Chrome等待?

[英]How do I use Watir::Waiter::wait_until to force Chrome to wait?

I'm trying to tell my watir script to wait for an ajax-injected login box to open up. 我试图告诉我的watir脚本等待ajax注入的登录框打开。 I am using watir-webdriver, and testing in Chrome. 我正在使用watir-webdriver,并在Chrome中进行测试。 I cannot get wait_until to work, as commented in the below (simplified) script. 我无法让wait_until工作,如下面(简化)脚本中所评论的那样。

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? Watir::Waiter是IE专用课吗? Or what am I doing wrong, the sleep 1 wait method works just fine. 或者我做错了什么, sleep 1等待方法工作得很好。 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. 我是Ruby和watir的新手,我昨天刚刚接受了这个,所以我有一半期待这是我不忠的结果。

In case it is relevant, I am working on a mac (OSX v. 10.6.5). 如果它是相关的,我正在使用mac(OSX v.10.6.5)。

Do this first: 首先这样做:

require "watir-webdriver/wait"

Then try these: 然后试试这些:

1 1

Watir::Wait.until { ... }

2 2

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

3 3

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

Note that "present" here means "the element both exists and is visible". 请注意,“present”在这里表示“元素既存在又可见”。

You can also set timeout with browser.it will wait for it 700 seconds, like this. 您也可以使用browser.it设置超时,等待700秒,就像这样。

client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 700 # seconds � default is 60 second
ie=Watir::Browser.new:firefox, :http_client => client

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). 关键是Watir :: Wait.until {}仅等待加载主页面(主要在Firefox上测试)。 If you have some JavaScript code loading other components, these are not waited for. 如果您有一些JavaScript代码加载其他组件,则不会等待这些代码。

So, the only solution is to pick and element and explicitly wait for it to appear (using methods 2 & 3). 因此,唯一的解决方案是pick和element并明确等待它出现(使用方法2和3)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM