Usage Of TestNG expectedExceptions Attribute In Selenium WebDriver With Example

expectedExceptions Is very good feature of testng using which we can Ignore known but not required exceptions. In your selenium webdriver test execution, You can use expectedExceptions when you know that any specific @Test method can throw specific exception but It Is not much more Important and you wants to Ignore that exception.

If you have used expectedExceptions attribute with @Test method then Whenever Exceptions thrown In @Test method, It will compare It with expectedExceptions attribute's value. If both match then It will be handled by TestNG. That means your @Test method will be pass and remaining part of that method will be skipped.

Let's see practical example to see how It works. In bellow given example, I have used expectedExceptions = NoSuchElementException.class with @Test method that means If method testCaseOne_Test_One() will throw NoSuchElementException then It will be handled by TestNG. Element //input[@id='10'] Is not available on page so It will throw NoSuchElementException.

If you are using JUnit then you can VIEW ANSWER OF QUESTION 55 for how to handle expected exception In It.

Let's execute this test In eclipse to observe result.


package Testing_Pack;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class expectedException { 
 
 WebDriver driver; 

 @BeforeTest
 public void setup() throws Exception {
  System.out.println("In @BeforeTest Of Test_One.");
  driver =new FirefoxDriver();     
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.get("http://only-testing-blog.blogspot.com/2014/04/calc.html");
 }
 
 //expectedExceptions attribute with @Test method will handle NoSuchElementException
 @Test(expectedExceptions = NoSuchElementException.class)
 public void testCaseOne_Test_One() {
  System.out.println("Executing testCaseOne_Test_One.");
  //This element Is not available on page so It will throw NoSuchElementException.
  driver.findElement(By.xpath("//input[@id='10']")).click();
  driver.findElement(By.xpath("//input[@id='plus']")).click();
  driver.findElement(By.xpath("//input[@id='6']")).click();
  driver.findElement(By.xpath("//input[@id='equals']")).click();
  String Result = driver.findElement(By.xpath("//input[@id='Resultbox']")).getAttribute("value");
  System.out.println("Result of testCaseOne_Test_One = "+Result);
 }
}

On completion of test execution, console result will looks like bellow. As per console result, We can say NoSuchElementException Is handled by expectedExceptions attribute because test result Is PASSED. Also remaining part of @Test method Is skipped from execution because only first 2 statements are printed In console. Last statement In not printed In console.

VIEW MORE ADVANCED TUTORIALS ON TESTNG

In @BeforeTest Of Test_One.
Executing testCaseOne_Test_One.
PASSED: testCaseOne_Test_One

TestNG result will show this @Test method as pass. See Image bellow.


1 comment:

  1. I still get the eception printed on the console and test method fails. pls help!!

    ReplyDelete