I want to take a screenshot if a test fails. Rather than wrapping all test methods with try/catch blocks, I would like to add this logic to the method annotated with @AfterMethod
.
How can I detect in the method annotated with @AfterMethod
if the current test has failed?
If the method annotated with @AfterMethod
has an ITestResult
parameter then TestNG will automatically inject the result of the test. (source: TestNG documentation, section 5.18.1)
This should do the job:
@AfterMethod
public void tearDown(ITestResult result) {
if (result.getStatus() == ITestResult.FAILURE) {
//your screenshooting code goes here
}
}