Skip to main content

Automation Testing Use Cases for Exception Handling

Exception handling plays a critical role in automation testing by ensuring that tests remain stable, provide meaningful feedback, and handle unexpected issues gracefully. In this section, we’ll explore practical use cases of exception handling in automation testing frameworks like Selenium WebDriver, REST Assured (API Testing), and JDBC (Database Testing).


1. Handling NoSuchElementException in Selenium WebDriver​

In Selenium WebDriver, exceptions like NoSuchElementException occur when an element is not found on the page. Proper exception handling ensures that tests do not crash and provide clear error messages.

Example: Handling NoSuchElementException​

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class SeleniumExceptionHandling {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
try {
driver.get("https://example.com");
// Attempt to find an element that may not exist
driver.findElement(By.id("nonExistentElement")).click();
} catch (NoSuchElementException e) {
System.out.println("Error: Element not found on the page.");
} finally {
// Ensure the browser is closed regardless of success or failure
driver.quit();
}
}
}

Explanation:

  • The try block attempts to find and interact with an element.
  • If the element is not found, a NoSuchElementException is caught and logged.
  • The finally block ensures the browser is closed, even if an exception occurs.

2. Managing TimeoutException in Explicit Waits​

Explicit waits in Selenium are used to wait for specific conditions. If the condition is not met within the specified timeout, a TimeoutException is thrown.

Example: Handling TimeoutException​

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class TimeoutExceptionExample {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
try {
driver.get("https://example.com");
WebDriverWait wait = new WebDriverWait(driver, 10); // Wait up to 10 seconds
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dynamicElement")));
element.click();
} catch (org.openqa.selenium.TimeoutException e) {
System.out.println("Error: Element did not appear within the timeout period.");
} finally {
driver.quit();
}
}
}

Explanation:

  • The WebDriverWait waits for an element to become visible.
  • If the element does not appear within the timeout, a TimeoutException is caught and logged.

3. Dealing with IOException in API Testing​

When testing APIs, exceptions like IOException can occur due to network issues, invalid URLs, or server errors. Proper exception handling ensures that these issues are logged and tests continue running.

Example: Handling IOException in REST Assured​

import io.restassured.RestAssured;
import io.restassured.response.Response;

public class ApiTestingExample {
public static void main(String[] args) {
try {
Response response = RestAssured.get("https://api.example.com/data");
if (response.getStatusCode() == 200) {
System.out.println("API call successful.");
} else {
System.out.println("API call failed with status code: " + response.getStatusCode());
}
} catch (Exception e) {
System.out.println("Error making API call: " + e.getMessage());
}
}
}

Explanation:

  • The try block makes an API call using REST Assured.
  • If the API call fails (e.g., due to a timeout or invalid URL), an exception is caught and logged.

4. Handling SQLException in Database Testing​

In database testing, exceptions like SQLException occur when there are issues with the database connection or query execution. Proper exception handling ensures that these issues are logged and resources are cleaned up.

Example: Handling SQLException in JDBC​

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DatabaseTestingExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/mydatabase";
String username = "root";
String password = "password";

try (Connection connection = DriverManager.getConnection(url, username, password)) {
System.out.println("Database connected successfully.");
} catch (SQLException e) {
System.out.println("Error connecting to database: " + e.getMessage());
}
}
}

Explanation:

  • The try-with-resources statement ensures that the database connection is closed automatically.
  • If the connection fails, an SQLException is caught and logged.

5. Using Custom Exceptions for Test Validation​

Custom exceptions are useful in test automation for signaling specific validation failures.

Example: Using a Custom Exception for Validation​

// Custom exception for test validation
public class TestValidationException extends Exception {
public TestValidationException(String message) {
super(message);
}
}

// Using the custom exception in a test
public class TestAutomationExample {
public static void validateLoginButtonVisibility(boolean isVisible) throws TestValidationException {
if (!isVisible) {
throw new TestValidationException("Login button is not visible.");
} else {
System.out.println("Login button is visible.");
}
}

public static void main(String[] args) {
try {
boolean loginButtonVisible = false; // Simulate a hidden login button
validateLoginButtonVisibility(loginButtonVisible);
} catch (TestValidationException e) {
System.out.println("Test failed: " + e.getMessage());
}
}
}

Explanation:

  • The TestValidationException class is used to signal a test failure due to a missing login button.
  • The validateLoginButtonVisibility method throws the custom exception if the login button is not visible.

Key Takeaways​

  • Exception handling is crucial for building stable and reliable automation test scripts.
  • Common exceptions in automation testing include:
    • NoSuchElementException in Selenium WebDriver.
    • TimeoutException in explicit waits.
    • IOException in API testing.
    • SQLException in database testing.
  • Custom exceptions can be used to signal specific validation failures in test automation.
  • Proper exception handling ensures graceful recovery, meaningful feedback, and resource cleanup.