Exception Test in Selenium

Share

Exception Test in Selenium

In Selenium, exception handling is crucial for robust test automation. Selenium WebDriver may throw various exceptions during test execution due to various reasons like element not found, timeout, or invalid input. Handling these exceptions appropriately helps ensure that your tests are stable and can provide meaningful feedback.

Here’s an example of how to handle exceptions in a Selenium test using Java:

java
import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class ExceptionHandlingExample { public static void main(String[] args) { // Set the path to the ChromeDriver executable System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe"); // Initialize the WebDriver WebDriver driver = new ChromeDriver(); try { // Navigate to a webpage driver.get("https://www.example.com"); // Attempt to locate an element WebElement element = driver.findElement(By.id("nonExistentElement")); // Perform an action on the element (this will throw a NoSuchElementException) element.click(); } catch (NoSuchElementException e) { // Handle the NoSuchElementException (element not found) System.out.println("Element not found: " + e.getMessage()); } catch (Exception e) { // Handle other exceptions that might occur during the test System.out.println("An error occurred: " + e.getMessage()); } finally { // Close the WebDriver instance driver.quit(); } } }

In this example:

  1. We set the path to the ChromeDriver executable and initialize the WebDriver with Chrome.

  2. We navigate to a webpage (https://www.example.com) and attempt to locate an element with an ID that does not exist on the page (nonExistentElement). This will trigger a NoSuchElementException.

  3. We use a try-catch block to catch the NoSuchElementException and handle it appropriately. In this case, we print a message indicating that the element was not found.

  4. We also have a catch block for a general Exception to handle any other unexpected exceptions that might occur during the test.

  5. In the finally block, we ensure that the WebDriver instance is properly closed using driver.quit() to release resources.

Demo Day 1 Video:

 
You can find more information about Selenium in this Selenium Link

 

Conclusion:

Unogeeks is the No.1 IT Training Institute for Selenium Training. Anyone Disagree? Please drop in a comment

You can check out our other latest blogs on  Selenium here – Selenium Blogs

You can check out our Best In Class Selenium Training Details here – Selenium Training

💬 Follow & Connect with us:

———————————-

For Training inquiries:

Call/Whatsapp: +91 73960 33555

Mail us at: info@unogeeks.com

Our Website ➜ https://unogeeks.com

Follow us:

Instagram: https://www.instagram.com/unogeeks

Facebook:https://www.facebook.com/UnogeeksSoftwareTrainingInstitute

Twitter: https://twitter.com/unogeeks


Share

Leave a Reply

Your email address will not be published. Required fields are marked *