Question

I am struggling writing a test; in which I want to check if an alert exists, check its text if it does and accept it.

I checked How to wait for an alert in Selenium webdriver ?, How to check if an alert exists using WebDriver? and selenium 2.4.0, how to check for presence of an alert, but I fail to adapt it using https://github.com/admc/wd

I have written something along

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

It works tremendously well when an alert is displayed, but alertText hangs when there is no alert window.

How can I check if the alert exists before issuing alertText?

Thanks for your help,

Was it helpful?

Solution

This may help - this is how I detect an alert using 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' is related to promises: then(success, failure)

and this may also help - it's how I ignore any alert that presents on a page:

function ignoreAlert(driver) {
  // detect and accept any alert
  driver.switchTo().alert().then(function() {
    driver.switchTo().alert().accept();}, 
    function(){});
}

OTHER TIPS

Make sure to set ignoreSynchronization if you're navigating to a non angular app:

browser.ignoreSynchronization = true;
browser.get(url);
browser.switchTo().alert().then(function() {
    browser.switchTo().alert().accept();
}, function(){});
browser.ignoreSynchronization = false;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top