9. Try, Catch, Finally & Custom Automation Errors

Error Handling in Test Automation

export class LocatorTimeoutError extends Error {
  constructor(locatorName: string, timeoutMs: number) {
    super(Element '${locatorName}' did not appear within ${timeoutMs}ms);
    this.name = "LocatorTimeoutError";
  }
}

async function safeClick(action: () => Promise<void>) {
try {
await action();
} catch (error) {
console.error("Action failed:", (error as Error).message);
throw error; // Re-throw to fail the test properly
} finally {
console.log("Cleanup executed");
}
}

Task

  • Wrap the async operation in a `try/catch` block. In `catch`, print the error message and re-throw the error.
πŸ’‘Common doubts
Did you like the problem?
Lesson 9 of 36
TypeScript
Loading code editor…
Next