1

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("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.

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.

  • shouldn't below code move outside the async function?? promise .then(value => console.log(value), value => console.log(value)) .catch(value => console.log(value)) .finally(() => tc2()); – gftea Mar 16 '22 at 20:28
  • This may have fixed it. I can't believe I overlooked that one, but promises and async functions have been confusing and it's been a whirlwind of information coming at me. I can possibly do more testing tomorrow to see if it is all working correctly. – Brandon Pulera Mar 16 '22 at 21:08
  • is it working now, if yes, hope you can accept the answer – gftea Mar 17 '22 at 13:18
  • Yes it is working. I'm new to stackoverflow and I don't see an option to accept your answer. Is it because it is in comment form? – Brandon Pulera Mar 18 '22 at 16:29
  • @BrandonPulera Yes, comments cannot be accepted as answers, I wouldn't worry about it – Nick is tired Mar 18 '22 at 16:30
  • @BrandonPulera, now I post it as answer, thanks for feedback – gftea Mar 18 '22 at 17:16
  • It doesn't make sense to call new Promise() with an async function. You should choose one or the other. – pguardiario Mar 19 '22 at 03:11
  • @pguardiario I think I've gotten myself into this mess with this being the only way I could figure out how to get async functions to not run on top of each other. I need to run many tests, as different async functions, one after the other. – Brandon Pulera Mar 19 '22 at 20:46

1 Answers1

0

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());
gftea
  • 558
  • 3
  • 7