expectedExceptions Is rattling proficient characteristic of testng using which nosotros tin bathroom Ignore known but non required exceptions. In your selenium webdriver attempt execution, You tin bathroom purpose expectedExceptions when you lot know that whatever specific @Test method tin bathroom throw specific exception but It Is non much to a greater extent than Important and you lot wants to Ignore that exception.
If you lot bring used expectedExceptions attribute amongst @Test method then Whenever Exceptions thrown In @Test method, It volition compare It amongst expectedExceptions attribute's value. If both jibe as well as then It volition hold out handled yesteryear TestNG. That agency your @Test method volition hold out top as well as remaining business office of that method volition hold out skipped.
Let's meet practical instance to meet how It works. In bellow given example, I bring used expectedExceptions = NoSuchElementException.class with @Test method that agency If method testCaseOne_Test_One() volition throw NoSuchElementException as well as then It volition hold out handled yesteryear TestNG. Element //input[@id='10'] Is non available on page therefore It volition throw NoSuchElementException.
If you lot are using JUnit as well as then you lot tin bathroom VIEW ANSWER OF QUESTION 55 for how to grip expected exception In It.
Let's execute this attempt In eclipse to notice 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; populace degree expectedException { WebDriver driver; @BeforeTest populace 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(" "); } //expectedExceptions attribute amongst @Test method volition grip NoSuchElementException @Test(expectedExceptions = NoSuchElementException.class) populace void testCaseOne_Test_One() { System.out.println("Executing testCaseOne_Test_One."); //This chemical component Is non available on page therefore It volition 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 attempt execution, console upshot volition looks similar bellow. As per console result, We tin bathroom say NoSuchElementException Is handled yesteryear expectedExceptions attribute because attempt upshot Is PASSED. Also remaining business office of @Test method Is skipped from execution because solely kickoff ii statements are printed In console. Last disputation In non printed In console.
VIEW MORE ADVANCED TUTORIALS ON TESTNG
VIEW MORE ADVANCED TUTORIALS ON TESTNG
In @BeforeTest Of Test_One. Executing testCaseOne_Test_One. PASSED: testCaseOne_Test_One
TestNG upshot volition exhibit this @Test method equally pass. See Image bellow.