I am using Cucumber.js and Selenium Webdriver JS in a Node.js environment, using Firefox as my browser.
I am trying to test a page that shows an alert on load. I have created a Cucumber step to dismiss the alert box which appears to work (visibly, the alert disappears) but I still get the error:
UnexpectedAlertOpenError: Dismissed user prompt dialog: Some alert text...
I have tried many different ways of getting around this but have had no success.
I have tried:
Setting an option when I build the browser:
let options = new firefox.Options();
options.setAlertBehavior(UserPromptHandeler, 'dismiss');
this.driver = new Builder()
.forBrowser('firefox')
.setFirefoxOptions(options)
.build();
Dismissing it in a Cucumber step:
When('I dismiss the alert', async function() {
this.driver.switchTo().alert().then((alert) => alert.dismiss());
});
And this way (as per Selenium docs):
When('I dismiss the alert', async function() {
await this.driver.wait(until.alertIsPresent());
let alert = await this.driver.switchTo().alert();
await alert.dismiss();
});
I've also tried waiting before dismissing:
When('I dismiss the alert', async function() {
await this.driver.manage().setTimeouts({ implicit: 3000 });
this.driver.switchTo().alert().then((alert) => alert.dismiss());
});
And
When('I dismiss the alert', async function() {
await this.driver.sleep(3000);
this.driver.switchTo().alert().then((alert) => alert.dismiss());
});
But no matter what I do I always get same error as above on the "When I dismiss the alert" step.
Would anyone know what I need to do to solve this error?
My full code:
const { When, Then, Given } = require('@cucumber/cucumber');
const { Builder, By, until, Key } = require('selenium-webdriver');
const { Options } = require('selenium-webdriver/firefox');
const firefox = require('selenium-webdriver/firefox');
const { UserPromptHandeler } = require('selenium-webdriver/lib/capabilities');
Given('I am on the branch locator page', async function () {
let options = new firefox.Options();
//options.setAlertBehavior(UserPromptHandeler, 'dismiss');
this.driver = new Builder()
.forBrowser('firefox')
.setFirefoxOptions(options)
.build();
this.driver.wait(until.elementLocated(By.tagName('h1')));
await this.driver.get('https://www.awebsite.com');
});
When('I dismiss the alert', async function() {
await this.driver.sleep(3000);
//await this.driver.manage().setTimeouts({ implicit: 3000 });
this.driver.switchTo().alert().then((alert) => alert.dismiss());
// await this.driver.wait(until.alertIsPresent());
// let alert = await this.driver.switchTo().alert();
// await alert.dismiss();
});
Then('I click the radio button', async function() {
await this.driver.findElement(By.id('option2')).click();
});
I got this working by looking through the capabilities.js
file in the selenium-webdriver
folder within node-modules
.
I can set it to dismiss alerts automatically but setting the option:
options.setAlertBehavior('dismiss');
Eg:
let options = new firefox.Options();
options.setPreference('geo.enabled', false);
options.setAlertBehavior('dismiss');
const driver = new Builder()
.forBrowser('firefox')
.setFirefoxOptions(options)
.build();