Home > Enterprise >  Error: DeprecationWarning: Unhandled promise rejections are deprecated. - Promises in Javascript wit
Error: DeprecationWarning: Unhandled promise rejections are deprecated. - Promises in Javascript wit

Time:03-19

I am trying to set up some test cases with Selenium in Javascript for a simple login page test. 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:

//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. Here is my code for that 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("[email protected]");
    } 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.

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.

CodePudding user response:

below code should be moved outside the async function

 //Trigger the callback functions
        promise
            .then(value => console.log(value),
                value => console.log(value))
            .catch(value => console.log(value))
            .finally(() => tc2());
  • Related