I recently encountered an issue while setting up test cases with Selenium in Javascript for a basic login page test. I am currently working on implementing proper error handling by rejecting within a Promise. Here is the code snippet in question:
//Test Case 1: Valid Email / Valid Password
async function tc1() {
announceTC();
//Creating a promise to handle the asynchronous test case execution
let promise = new Promise(async function (resolve, reject) {
//Setting up the webdriver (Chrome) and navigating to the test page
const driver = createDriver();
//Entering a valid email and password
await validEmail(driver, reject);
await validPassword(driver);
//Simulating a click on the login button
await clickLoginButton(driver);
//Verifying a successful login
await testSuccessfulLogin(driver, resolve, reject);
//Closing the driver instance
await driver.quit();
//Triggering callback functions
promise
.then(value => console.log(value),
value => console.log(value))
.catch(value => console.log(value))
.finally(() => tc2());
});
};
While working on the validEmail function, I came across an issue where I need to reject if an error occurs. Here is how I handled it:
//Locate and input valid email on the login page
async function validEmail(driver, reject) {
try{
//Locating the email input box
let emailBox = await driver.wait(until.elementLocated(By.xpath("//*[@id='email']/iput")), 5000);
await emailBox.click();
//Inputting the email address
driver.findElement(By.xpath("//*[@id='email']/input")).sendKeys("<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a0d2c5c4c1c3d4c5c4e0c7cdc1c9cc8ec3cfcd">[email protected]</a>");
} catch(error) {
console.log(error);
reject(tcErrorMsg());
}
}
From my understanding, this error arises when there's no catch statement in the promise to address a rejected case.
Oddly enough, my testSuccessfulLogin function utilizing resolve works fine with .then, but encountering an unhandled rejection error when using reject in a similar manner has left me puzzled.