Selenium code to capture screenshot only when test fails.

ITestResult is an interface which give information about our current test case which is under execution
result variable will store all the details of @Test along with the pass or fail result.


——————-
@AfterMethod
public void tearDown(ITestResult result) throws IOException
{
if(ITestResult.FAILURE==result.getStatus())
{
UtilityScrnSht.ScreenShot(Page.driver, result.getName());
//user created method
}
}
——————–