Wednesday, May 8, 2019

IAnnotationTransformer in TestNG Java

package selenium;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;
public class MyListener implements IAnnotationTransformer {
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
if(testMethod.getName().equals("method3")){
annotation.setPriority(2);
annotation.setEnabled(false);
}
}
}
view raw MyListener.java hosted with ❤ by GitHub
package selenium;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@Listeners(selenium.MyListener.class)
public class SeleniumExample {
@Test(testName="Method1", priority=2)
public void method1(){
System.out.println("Method1");
}
@Test(priority=1)
public void method2(){
System.out.println("Method2");
}
@Test(testName="Method3", priority=0)
public void method3(){
System.out.println("Method3");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
<listeners>
<listener class-name="selenium.AnnotationTransformer" />
</listeners>
<test name="Test">
<classes>
<class name="selenium.SeleniumExample" />
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

No comments:

Post a Comment