我正在尝试测试一个在加载时显示警报的页面。我创建了一个 Cucumber 步骤来关闭似乎有效的警报框(显然,警报消失了),但我仍然收到错误:
UnexpectedAlertOpenError: Dismissed user prompt dialog: Some alert text...
我尝试了许多不同的方法来解决这个问题,但没有成功。我已经尝试过:
构建浏览器时设置选项:
let options = new firefox.Options();
options.setAlertBehavior(UserPromptHandeler, 'dismiss');
this.driver = new Builder()
.forBrowser('firefox')
.setFirefoxOptions(options)
.build();
通过黄瓜步骤将其关闭:
When('I dismiss the alert', async function() {
this.driver.switchTo().alert().then((alert) => alert.dismiss());
});
这样(根据 Selenium 文档):
When('I dismiss the alert', async function() {
await this.driver.wait(until.alertIsPresent());
let alert = await this.driver.switchTo().alert();
await alert.dismiss();
});
我也尝试过在解雇前等待:
When('I dismiss the alert', async function() {
await this.driver.manage().setTimeouts({ implicit: 3000 });
this.driver.switchTo().alert().then((alert) => alert.dismiss());
});
还有
When('I dismiss the alert', async function() {
await this.driver.sleep(3000);
this.driver.switchTo().alert().then((alert) => alert.dismiss());
});
但无论我做什么,我总是在“当我解除警报时”步骤中遇到与上面相同的错误。有人知道我需要做什么来解决这个错误吗?
我的完整代码:
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();
});
capabilities.js
中
selenium-webdriver
文件夹中的
node-modules
文件来完成此操作。我可以将其设置为自动消除警报,但设置选项:
options.setAlertBehavior('dismiss');
例如:
let options = new firefox.Options();
options.setPreference('geo.enabled', false);
options.setAlertBehavior('dismiss');
const driver = new Builder()
.forBrowser('firefox')
.setFirefoxOptions(options)
.build();