使用 watir-webdriver 等待 js 警报
我正在使用 watir-webdriver 来驱动 Web 应用程序的测试运行。
相关页面通过确认来响应输入更改,然后是页面切换,然后是警报。
** 更新 **
我已尝试以下操作,但我收到一个在确认块中启动的 Selenium::WebDriver::Error::UnhandledAlertError
。
require "watir-webdriver/extensions/alerts"
b.alert do
b.confirm(true) do
b.checkbox(:index => 1).set
end
end
** 结束更新 **
但是添加以下内容(在输入更改后):
browser.driver.switch_to.alert.accept
将忽略第一个确认没有问题...
不幸的是,由于页面搅动尝试使用同一行来忽略以下警报,因此会引发 a ,
Selenium::WebDriver::Error::NoAlertPresentError
因为警报尚未“事情还没有发生。
我的解决方法是将以下方法添加到 Watir::Browser
def get_alert (timeout = 300)
Watir::Wait.until(timeout) {
begin
driver.switch_to.alert
rescue Selenium::WebDriver::Error::NoAlertPresentError
end
}
end
并通过
browser.get_alert
是否有更好/内置的方法来运行所有警报处理?
** 更新 **
我的解决方法的一个好处是它允许我按程序处理警报,而不是使用 b.alert/b.confirm 中更难读取(或生成)的块
** 结束更新 **
提前致谢, 阿尔斯
I am using watir-webdriver to drive a test run of a web application.
The page in question responds to an input change with a confirm, followed by a page-churn, followed by an alert.
** UPDATE **
I have tried the following but I get a Selenium::WebDriver::Error::UnhandledAlertError
that starts in the confirm block.
require "watir-webdriver/extensions/alerts"
b.alert do
b.confirm(true) do
b.checkbox(:index => 1).set
end
end
** END UPDATE **
However adding the following (after the input change):
browser.driver.switch_to.alert.accept
will dismiss the first confirm no problem...
Unfortunately due to the page-churn trying the same line to dismiss the following alert raises a
Selenium::WebDriver::Error::NoAlertPresentError
as the alert hasn't happened yet.
My work-around is to add the following method to Watir::Browser
def get_alert (timeout = 300)
Watir::Wait.until(timeout) {
begin
driver.switch_to.alert
rescue Selenium::WebDriver::Error::NoAlertPresentError
end
}
end
and run all alert handling through
browser.get_alert
Is there a better/built-in way of doing this?
** UPDATE **
One benefit of my work-around is it allows me to handle alerts procedurally rather than with the harder to read (or generate) blocks within b.alert/b.confirm
** END UPDATE **
Thanks in advance,
Arth
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据 watir-webdriver 网站上的 dialogs 页面 ->
因此,在您的具体情况下,您应该能够调用上述内容,而不是调用
browser.driver.switch_to.alert.accept
或browser.get_alert
。希望这有帮助。
As per the dialogs page on the watir-webdriver site ->
So, in your specific case instead of calling
browser.driver.switch_to.alert.accept
orbrowser.get_alert
you should be able to call the above.Hope this helps.
如果没有访问您的页面进行尝试,很难知道这是否有效,但是您是否考虑过尝试这样的事情?
hard to know if this will work without having access to your page to try it, but have you considered trying something like this?
较新的 Alerts API 具有标准的等待方法,因此您现在可以执行以下操作: :
The newer Alerts API has the standard waiting methods, so you can now do: