Home | History | Annotate | Download | only in listeners
      1 package test.listeners;
      2 
      3 import org.testng.IInvokedMethod;
      4 import org.testng.IInvokedMethodListener;
      5 import org.testng.ITestResult;
      6 
      7 import java.util.HashMap;
      8 import java.util.Map;
      9 
     10 public class MyInvokedMethodListener implements IInvokedMethodListener {
     11 
     12     public static Map<String, Integer> beforeInvocation = new HashMap<>();
     13     public static Map<String, Integer> afterInvocation = new HashMap<>();
     14 
     15 
     16     @Override
     17     public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
     18         increments(beforeInvocation, method);
     19     }
     20 
     21     @Override
     22     public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
     23         increments(afterInvocation, method);
     24     }
     25 
     26     private static void increments(Map<String, Integer> map, IInvokedMethod method) {
     27         String stringValue = method.getTestMethod().getMethodName();
     28         Integer count = map.get(stringValue);
     29         if (count == null) {
     30             count = 0;
     31         }
     32         map.put(stringValue, count+1);
     33     }
     34 }
     35