使用 watir-webdriver 等待 js 警报

发布于 2025-01-06 03:24:17 字数 1073 浏览 4 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彼岸花似海 2025-01-13 03:24:17

根据 watir-webdriver 网站上的 dialogs 页面 ->

require "watir-webdriver/extensions/alerts"

browser.confirm(true) do
  browser.button(:value => 'Confirm').click
end #=> 'the confirm message'

因此,在您的具体情况下,您应该能够调用上述内容,而不是调用 browser.driver.switch_to.alert.acceptbrowser.get_alert

希望这有帮助。

As per the dialogs page on the watir-webdriver site ->

require "watir-webdriver/extensions/alerts"

browser.confirm(true) do
  browser.button(:value => 'Confirm').click
end #=> 'the confirm message'

So, in your specific case instead of calling browser.driver.switch_to.alert.accept or browser.get_alert you should be able to call the above.

Hope this helps.

雾里花 2025-01-13 03:24:17

如果没有访问您的页面进行尝试,很难知道这是否有效,但是您是否考虑过尝试这样的事情?

require "watir-webdriver/extensions/alerts"

b.alert do
  b.checkbox(:index => 1).set
  browser.driver.switch_to.alert.accept
  #possibly a wait here...
end

hard to know if this will work without having access to your page to try it, but have you considered trying something like this?

require "watir-webdriver/extensions/alerts"

b.alert do
  b.checkbox(:index => 1).set
  browser.driver.switch_to.alert.accept
  #possibly a wait here...
end
深巷少女 2025-01-13 03:24:17

较新的 Alerts API 具有标准的等待方法,因此您现在可以执行以下操作: :

browser.alert.when_present.ok

The newer Alerts API has the standard waiting methods, so you can now do:

browser.alert.when_present.ok
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文