[英]Error: DeprecationWarning: Unhandled promise rejections are deprecated. - Promises in Javascript with Selenium
I am trying to set up some test cases with Selenium in Javascript for a simple login page test.我正在尝试在 Javascript 中使用 Selenium 设置一些测试用例,以进行简单的登录页面测试。 I am at the point where I am trying to set up proper error catching by rejecting within a Promise. Here is my Promise code:我正试图通过在 Promise 中拒绝来设置正确的错误捕获。这是我的 Promise 代码:
//Test Case 1: Valid Email / Valid Password
async function tc1() {
announceTC();
//Create promise to handle execution of async test case
let promise = new Promise(async function (resolve, reject) {
//Create the webdriver (Chrome) and open an instance of the test page
const driver = createDriver();
//Enter valid email / valid password
await validEmail(driver, reject);
await validPassword(driver);
//Click login button
await clickLoginButton(driver);
//Test for a successful login
await testSuccessfulLogin(driver, resolve, reject);
//Exit the driver instance.
await driver.quit();
//Trigger the callback functions
promise
.then(value => console.log(value),
value => console.log(value))
.catch(value => console.log(value))
.finally(() => tc2());
});
};
Within my validEmail function, I am trying to reject if an error occurs.在我的 validEmail function 中,如果发生错误,我会尝试拒绝。 Here is my code for that function:这是我的 function 代码:
//Find and enter valid email on login page
async function validEmail(driver, reject) {
try{
//Click email input box.
let emailBox = await driver.wait(until.elementLocated(By.xpath("//*[@id='email']/iput")), 5000);
await emailBox.click();
//Input email.
driver.findElement(By.xpath("//*[@id='email']/input")).sendKeys("redacted@gmail.com");
} catch(error) {
console.log(error);
reject(tcErrorMsg());
}
}
My understanding of this error, is that it occurs when you do not provide a catch on the promise to handle a rejected case.我对这个错误的理解是,当您没有在 promise 上提供 catch 来处理被拒绝的案例时,就会发生这种情况。
My testSuccessfulLogin function uses resolve and the promise runs through.then properly, so I am unsure why I am getting an unhandled rejection error when I use reject in the same way.我的 testSuccessfulLogin function 使用 resolve 和 promise runs through.then 正确,所以我不确定为什么当我以同样的方式使用 reject 时我会收到未处理的拒绝错误。
below code should be moved outside the async function下面的代码应该移到异步 function 之外
//Trigger the callback functions
promise
.then(value => console.log(value),
value => console.log(value))
.catch(value => console.log(value))
.finally(() => tc2());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.