Home > front end >  Get Priority Number In Selenium TestNG
Get Priority Number In Selenium TestNG

Time:11-23

Is there any way of getting/knowing the priority number within a test suit? i.e.

@Test (priority = 1, groups = { "requiredSetUp" })
public void login(){
    System.debug('This test is assigned '   getPriorityNumber());
}

CodePudding user response:

You can implement it using IAnnotationTransformer. For example you could do something like:

public class NewTest implements IAnnotationTransformer{

    private static Map<String, Integer> priorities = new ConcurrentHashMap<>();

    @Test(priority = 123)
    public void test123(ITestContext ctx){
        System.out.println("Running: "   Thread.currentThread()  
                " - "   priorities.get(new Object(){}.getClass().getEnclosingMethod().getName()));
    }

    @Test(priority = 321)
    public void test321(ITestContext ctx){
        System.out.println("Running: "   Thread.currentThread()  
                " - "   priorities.get(new Object(){}.getClass().getEnclosingMethod().getName()));
    }

    @Override
    public void transform(ITestAnnotation annotation, Class testClass,
                          Constructor testConstructor, Method testMethod) {
        priorities.put(testMethod.getName(), annotation.getPriority());
        IAnnotationTransformer.super.transform(annotation, testClass, testConstructor, testMethod);
    }

}

where this is implemented in thread-safe way to support parallelism.

This new Object(){}.getClass().getEnclosingMethod().getName() is the way how to obtain current method name. See: Getting the name of the currently executing method

Disclaimer: This type of listener can only be registered inside testng.xml or as command line argument. So below is the example of testng.xml implementing proper configuration

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" parallel="methods" thread-count="3">
    <listeners>
        <listener class-name="so.testng.NewTest"/>
    </listeners>
    <test name="Regression1">
        <packages>
            <package name="so.testng"/>
        </packages>
    </test>
</suite>
  • Related