How do I use IAnnotationTransformer in testNG? How do I use IAnnotationTransformer in testNG? selenium selenium

How do I use IAnnotationTransformer in testNG?


Listeners are not automatically picked up until you have set them up using serviceloaders.

There are multiple ways to specify a listener, based on need.
If you do not want to apply them to all your tests, you may choose to apply a listener on a single class

eg.

@Listeners(Transformer.class)public class InterceptorTC {@Test(priority..

You can also specify a listener from maven surefire plugin. Read the using custom listener section here

Other ways to specify listener can be to define it in your suite xml and specify your suite xml file in the maven plugin.

.


void transform(ITestAnnotation annotation, java.lang.Class testClass, java.lang.reflect.Constructor testConstructor, java.lang.reflect.Method testMethod)

This method will be invoked by TestNG to give you a chance to modify a TestNG annotation read from your test classes. You can change the values you need by calling any of the setters on the ITest interface. Note that only one of the three parameters testClass, testConstructor and testMethod will be non-null.


public class Retry implements IRetryAnalyzer {int retrycount = 0; int maxretyrcount =2;@Overridepublic boolean retry(ITestResult result) {    // TODO Auto-generated method stub    if (retrycount<maxretyrcount){        System.out.println("Retrying test " + result.getName() + " with status "                + getResultStatusName(result.getStatus()) + " for the " + (retrycount+1) + " time(s).");        retrycount++;        return true;    }    return false;} public String getResultStatusName(int status) {        String resultName = null;        if(status==1)            resultName = "SUCCESS";        if(status==2)            resultName = "FAILURE";        if(status==3)            resultName = "SKIP";        return resultName;    }

}

// another classpublic class RetryListner implements IAnnotationTransformer{

@Overridepublic void transform(ITestAnnotation arg0, Class arg1, Constructor arg2,Method arg3) {    // TODO Auto-generated method stub    IRetryAnalyzer analyzer = arg0.getRetryAnalyzer();    if (analyzer == null)   {        arg0.setRetryAnalyzer(Retry.class);    }}

}