Different types of exceptions occurred during execution and ways to handle these exceptions and waits in selenium
Size: 734.27 KB
Language: en
Added: May 06, 2025
Slides: 26 pages
Slide Content
Handling Exceptions And Waits In Selenium
What Is Exception? An exception is an incident that disturbs the normal flow of a program’s execution. When an unexpected occurrence occurs during the execution of a program, an exception object is created to reflect the specific error or unexpected state. Factors causing exception are: Logical errors Runtime errors System Errors
What is Exception Handling in Selenium? Exception handling in Selenium involves the process of detecting and managing errors that occur during the execution of test scripts. When an unforeseen event or error disrupts the normal flow of a Selenium script, exception handling mechanisms allow developers to manage these disruptions gracefully. This ensures that the test execution does not abruptly stop, and appropriate actions can be taken to address or log the issues. Proper exception handling helps in creating resilient and reliable automation tests by enabling the scripts to handle unexpected situations effectively and continue running or provide meaningful feedback.
Process Of Handling Exception
What Happens When You Don’t Use Exception Handling In Selenium? Uncaught Errors Halt Execution Lack of Error Reporting Inconsistent Test Results Inability to Handle Dynamic Content Reduced Maintainability
Different Methods to Handle Exception In Selenium Try-Catch Multiple Catch Blocks Throw/Throws Finally Exception Information Methods
1. Try-catch Syntax: try { // Code that may throw an exception } catch ( ExceptionType e) { // Exception handling code }
2 . Multiple Catch blocks Syntax: try { // Code that may throw an exception } catch ( IOException e) { // Handle IOException } catch ( NumberFormatException e) { // Handle NumberFormatException } catch (Exception e) { // Handle any other exceptions }
3. Throw/Throws Syntax: public static void anyFunction () throws Exception { try { // Code that might throw an exception } catch (Exception e) { // Handle the exception throw e; // Re-throw the exception } }
3. Throw/Throws Syntax: (throw) public static void anyFunction () throws Exception { try { // Code that might throw an exception } catch (Exception e) { // Handle the exception throw e; // Re-throw the exception } } Syntax: (throws) public static void anyFunction () throws ExceptionType1, ExceptionType2 { // Method code }
5 .Exception Information Methods: printStackTrace () toString () getMessage ()
What is Selenium Wait? Selenium Wait is a mechanism in Selenium WebDriver that makes it wait until certain conditions are met before executing further commands of the test script. These typically contain a wait for one of the web page elements to either be loaded or interactable. Without waits, test scripts will fail in trying to perform activities with elements that have not appeared yet, or their loading is not complete; this makes tests unreliable and inconsistent.
Implicit Wait In Selenium Implicit Wait is one of the simple yet very useful features available in Selenium, which will instruct the WebDriver to wait for a certain period of time before throwing a no such element exception ( NoSuchElementException ) if it cannot find an element on the page. Once set, it applies globally to all element searches performed by WebDriver for the duration of that WebDriver instance. Syntax to Implicit Wait in Selenium: // Setting the implicit wait for 10 seconds driver.manage ().timeouts(). implicitlyWait ( Duration.ofSeconds (10));
Example of implicit wait
Explicit wait Explicit wait in Selenium is a synchronization mechanism that allows the WebDriver to wait for a specific condition to occur before proceeding with the next step in the code. Unlike Implicit waits, which apply globally, explicit waits are applied only to specific elements or conditions, making them more flexible and precise. Setting Explicit Wait is important in cases where there are certain elements that naturally take more time to load. Explicit Wait Syntax: WebDriverWait wait = new WebDriverWait (driver,30);
Fluent Wait Fluent Wait in Selenium marks the maximum amount of time for Selenium WebDriver to wait for a certain condition (web element) becomes visible. It also defines how frequently WebDriver will check if the condition appears before throwing the “ ElementNotVisibleException ”. Fluent Wait looks for a web element repeatedly at regular intervals until timeout happens or until the object is found. Fluent Wait commands are most useful when interacting with web elements that can take longer durations to load. Fluent Wait allows to specify: Polling Frequency Timeout Ignoring Exceptions
Syntax of fluent wait Wait<WebDriver> wait = new FluentWait <>(driver) . withTimeout ( Duration.ofSeconds (30)) // Maximum time to wait . pollingEvery ( Duration.ofMillis (500)) // Interval between each poll .ignoring( NoSuchElementException.class ); // Exceptions to ignore WebElement element = wait.until ( ExpectedConditions.visibilityOfElementLocated (By.id(" elementID ")));