Custom Listeners in testng using iReporter

We can make our custom reports using ireporter listener.Ireporter is an interface.

We have to override its function generatereport in the class which implements this interface. And then we can add CustomReporter class as a listener in testing.xml ,the control automatically goes to the function generatereport of this CustomReport class once all test cases are executed.

In generatereport function we will use a for loop to iterate through each suite we defined in testing.xml We then get the current suite from the list of suite and give it a name currentTestSuite and then print its name.

public class customreporting implements IReporter{
    public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
  
       //In that function we will use a for loop to iterate through each suite we defined in testing.xml
      //We then get the current suite from the list of suite and give it a name currentTestSuite.

      for(int suiteNum=0; suiteNum<suites.size();suiteNum++){ 
      ISuite currentTestSuite  =  suites.get(suiteNum);
     //  We then get results of each suite using the command currentTestSuite.getResults();getResults will return a map with a key value pair.
    //So we save the results in a map.And then get the set of keys from the map in the form of a set.Then use a iterator to iterate through all keys of the 
   //set.
   Map<String,ISuiteResult>  suiteResults  =  currentTestSuite.getResults();
   Set<String> suiteResultkeys  =  suiteResults.keySet();
   Iterator<String> suiterResultYeysiterator  = suiteResultkeys.iterator();
   //Then we use a while loop on the iterator to print the name of the keys.The keys are basically the names of test cases we gave in our suites.
   //Like  for example if we have a  suite named SuiteB and we have tests in that suite like for example threy can be  named  Test B1 and B2 etc.

   // Now we know using a while loop and hasnext function on the iterator of the set we can get all the keys.Key is basically name of the test case we  
   //gave in the suite.But we need to reach each and every value associated with each key. 


   while(suiteResultKeysiterator.hasNext()){
    String testname  =  suiteResultKeysiterator.next();
    
     //But we need to reach each and every value associated with each key. 
     ISuiteResult testResult = suiterResults.get(testname);
       //Then from testresult we save all the failed tests ,passes tests and skipped tests in different maps.

    IResultMap failedTests = testresult.getTestContext().getFailedTests();
    IResultMap passedTests = testresult.getTestContext().getPassedTests();
    IResultMap skippedTests = testresult.getTestContext().getSkippedTests();

    //And we save all the methods or test cases in a particular class in ItestNgMethod type array.i.e save all test cases here regardless whether they 
   passes ,failed or skipped.Then we iterate through all the testcases using a for loop and see whether that particular test case lies in the collection of 
   failed test cases ,passed test cases or skipped test cases.Then we can print all the passed,failed and skipped test cases.

   ITestNGMethod[] allTests = testresult.getTestContext().getAllTestMethods();
    for (int i=0;i<allTests.length;i++){
    System.out.println(allTests[i].getMethodName());
    if( failedTests.getAllMethods().contains(allTests[i])){
    System.out.println(allTests[i].getMethodName()+"----Failed---");
    Collection<ITestNGMethod> allfailedMethods  =  failedTests.getAllMethods();
    Iterator<ITestNGMethod> allfailedMethodsItor   = allfailedMethods.iterator();
    while(allfailedMethodsItor.hasNext()){
        ITestNGMethod meth  = allfailedMethodIitor.next();
        if(allTests[i].getMethodName().equals(meth.getMethodName()));
        Set<ITestResult> failures   = failedTests.getResults(meth);
        Iterator<ITestResult> failureIter  = failures.iterator();
        while(failureIter.hasNext()){
            System.out.println("Reason of failure is----"+failureIter.next().getThrowable().getMessage());
        }
           }
    }
    else if(passedTests.getAllMethods().contains(allTests[i])){
        System.out.println(allTests[i].getMethodName()+"----Passed---");
        }
    else if( skippedTests.getAllMethods().contains(allTests[i])){
       System.out.println(allTests[i].getMethodName()+"----Skipped---");
       Collection<ITestNGMethod> allskippedMethods  =  skippedTests.getAllMethods();
       Iterator<ITestNGMethod> allskippedMethodsItor   = allskippedMethods.iterator();
       while(allskippedMethodsitor.hasNext()){
         ITestNGMethod meth  = allskippedMethodsItor.next();
        if(allTests[i].getMethodName().equals(meth.getMethodName()));
        Set<ITestResult> skipped   = skippedTests.getResults(meth);
        Iterator<ITestResult> skippedIter  = skipped.iterator();
        while(skippedIter.hasNext()){
            System.out.println("Reason for skipped is----"+skippedIter.next().getThrowable().getMessage());
          }
        }
       }
        }
    }
  }
 } 
}