4. Exception Handling & Defensive Automation

Exception Handling in Test Automation

try {
    page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Accept")).click(
        new Locator.ClickOptions().setTimeout(2000)
    );
} catch (PlaywrightException e) {
    System.out.println("Optional modal did not appear; continuing.");
}

Task

  • Implement safeParseTimeout:
  • 1. Accept String timeoutStr.
  • 2. Parse to integer, or return 5000 if NumberFormatException is thrown.
πŸ’‘Common doubts
Did you like the problem?
Lesson 4 of 18
Java
Loading code editor…
Next