简体   繁体   English

错误:弃用警告:未处理的 promise 拒绝已弃用。 - 承诺在 Javascript 和 Selenium

[英]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.

相关问题 弃用警告:不推荐使用未处理的承诺拒绝 - DeprecationWarning: Unhandled promise rejections are deprecated 在Fuse.js的实现中获取“ DeprecationWarning:不建议使用的未处理承诺拒绝。”以在Node.js中从MongoDB中搜索数据 - Getting “DeprecationWarning: Unhandled promise rejections are deprecated.” in the implementation of Fuse.js to search data from MongoDB in Node.js (节点:93364)[DEP0018] DeprecationWarning:不推荐使用未处理的 promise 拒绝 - (node:93364) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated Discord.js: (node:4976) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated - Discord.js: (node:4976) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated async / await,弃用未处理的承诺拒绝 - async/await , unhandled promise rejections are deprecated Node / Express未处理的承诺拒绝已弃用 - Node/Express Unhandled promise rejections are deprecated 错误:未捕获(承诺中):未处理的承诺拒绝 - Error: Uncaught (in promise): Unhandled promise rejections 捕获所有未处理的 javascript 承诺拒绝 - Catch all unhandled javascript promise rejections 调试未处理的承诺拒绝 - Debug Unhandled Promise Rejections 面临来自 express 的“未处理的承诺拒绝”错误,问题是 catch() 语句 - facing an 'unhandled promise rejections' error from express, issue with catch() statement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM