Recently, I've delved into the world of Typescript while exploring Playwright for Test Automation. There's a scenario where two elements - Validated and Failed - can appear after some loading. In Selenium, my go-to method is to use WebDriverWait and stop the loop as soon as either element appears. In Typescript, I came across Promise.race() but couldn't quite get it to work. Here's the snippet from my code:
public waitForFinalStatus = async (): Promise<Boolean> => {
const [failed, validated] = await Promise.race([
this.locFailed().waitFor({ timeout: 120000 }).then(() => "Failed"),
this.locValidated().waitFor({ timeout: 120000 }).then(() => "Passed")
]);
return validated === "Passed";
}
The values of 'failed' and 'validated' statuses are coming out as "E" and "C", respectively, which I find puzzling. I expected that when 'Passed', the value would be "Passed" and the other one would be undefined
.
I also have another query here. Suppose the 'Validated' element appears. After the 120-second wait time, the waitFor
on locFailed would fail, triggering an error, right? Without proper try-catch handling, could this disrupt the program flow? In Selenium Java, this issue is dealt with internally by WebDriverWait, which ignores NotFoundException so that exceptions thrown due to element unavailability are managed. From what I understand, if the element isn't found within the specified conditions, waitFor
simply throws an error. Can you shed some light on this concept?