4

我正在努力编写测试;我想在其中检查警报是否存在,如果存在则检查其文本并接受它。

我检查了如何在 Selenium webdriver 中等待警报?,如何使用 WebDriver 检查是否存在警报?selenium 2.4.0,如何检查是否存在警报,但我无法使用https://github.com/admc/wd对其进行调整

我已经写了一些东西

browser.alertText(function (err, text) {
  if (text) {
    browser.acceptAlert(function () {
      // ...
    });
  }
});

它在显示警报时效果非常好,但alertText在没有警报窗口时会挂起。

如何在发布前检查警报是否存在alertText

谢谢你的帮助,

4

2 回答 2

7

这可能会有所帮助 - 这就是我使用 Selenium Webdriver for node.js 检测警报的方式:

var webdriver = require('selenium-webdriver');

var url = "http://web-page-to-test-for-alert";

driver = new webdriver.Builder().
    withCapabilities(webdriver.Capabilities.chrome()).
    build();

driver.get(url);

driver.switchTo().alert().then(
  function() {
    console.log("alert detected");
  },
  function() {
    console.log("no alert detected");
  }
);

driver.quit(); 

'then' 与承诺有关:then(success, failure)

这也可能有所帮助 - 这就是我忽略页面上出现的任何警报的方式:

function ignoreAlert(driver) {
  // detect and accept any alert
  driver.switchTo().alert().then(function() {
    driver.switchTo().alert().accept();}, 
    function(){});
}
于 2014-02-02T22:52:01.463 回答
0

如果您要导航到非 Angular 应用程序,请确保设置 ignoreSynchronization:

browser.ignoreSynchronization = true;
browser.get(url);
browser.switchTo().alert().then(function() {
    browser.switchTo().alert().accept();
}, function(){});
browser.ignoreSynchronization = false;
于 2015-12-02T03:44:01.797 回答