Handling Exceptions and waits in selenium.pptx

MadhuriLonikar 26 views 26 slides May 06, 2025
Slide 1
Slide 1 of 26
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26

About This Presentation

Different types of exceptions occurred during execution and ways to handle these exceptions and waits in selenium


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

Hierarchy Of Exception

Common Selenium Exceptions WebDriverException TimeOutException ElementNotVisibleException NoSuchWindowException NoSuchElementException ElementNotInteractableException ConnectionClosedException ElementClickInterceptedException ElementNotSelectableException ErrorHandler.UnknownServerException ErrorInResponseException ImeActivationFailedException ImeNotAvailableException InsecureCertificateException InvalidArgumentException InvalidCookieDomainException InvalidCoordinatesException InvalidSessionIdException InvalidElementStateException JavascriptException

Continued.. InvalidSwitchToTargetException JsonException MoveTargetOutOfBoundsException NoAlertPresentException NoSuchAttributeException NoSuchContextException NoSuchCookieException NoSuchFrameException RemoteDriverServerException UnsupportedCommandException StaleElementReferenceException SessionNotFoundException SessionNotCreatedException

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 }

4 . Finally Syntax: try { // Code that might throw an exception } catch (ExceptionType1 e1) { // Handle ExceptionType1 } catch (ExceptionType2 e2) { // Handle ExceptionType2 } finally { // Code that always executes }

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.

Types of Selenium Wait Commands Implicit Wait Explicit Wait Fluent Wait

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);

Expected conditions alertIsPresent () elementSelectionStateToBe () elementToBeClickable () elementToBeSelected () frameToBeAvaliableAndSwitchToIt () invisibilityOfTheElementLocated () invisibilityOfElementWithText () presenceOfAllElementsLocatedBy () presenceOfElementLocated () textToBePresentInElement () textToBePresentInElementLocated () textToBePresentInElementValue () titleIs () titleContains () visibilityOf () visibilityOfAllElements () visibilityOfAllElementsLocatedBy () visibilityOfElementLocated ()

Example of Explicit wait

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 ")));

Example of fluent wait

Thank You!!
Tags