Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an alert is open using nodejs webdriver (wd)

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,

like image 931
lkenneth Avatar asked Mar 21 '23 20:03

lkenneth


1 Answers

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(){});
}
like image 70
Dion Avatar answered Apr 02 '23 15:04

Dion