Search code examples
javareportingscreen-scrapingtestng

TestNG suppressing @AfterMethod to reporter


I am trying to write a TestNG @AfterMethod function that essentially mimics a "screenshot on failure". Except it uses HTMLUnit and collects XML using getContent().

So this "Test" runs after every method to see if the method resulted in a pass or failure, and prints out XML if the ITestResult is a FAILURE. If it is a success, the body is blank. But after every method, even if the Test was a Success, it still prints the @AfterMethod configuration on the Report page with the parameters passed into the @AfterMethod.

I only want this @AfterMethod function to appear in the report page if the test failed. Otherwise, it is really obnoxious and takes up a surprising amount of space with the parameters, even if the body is blank.

Any ideas on how to do this?


Solution

  • I would look at using TestNG listeners rather than @AfterMethod as that will decouple your tests from the reporting, leaving you tests to just generate scenarios and assert failures. ITestListener will allow you "listen" out for failed tests.

    Here is an example of how I have used it in Webdriver; Capture WebDriver Screenshots When Running Parallel Tests With TestNG

    I understand you are using HTMLUnit, but the ideas and concepts are there