Selenium TestNG侦听器中的ITestResult.getTestName()返回testname=null,尽管在测试方法上设置了name@Test(testname=";Browser";)

Selenium TestNG侦听器中的ITestResult.getTestName()返回testname=null,尽管在测试方法上设置了name@Test(testname=";Browser";),selenium,testng,Selenium,Testng,如何在侦听器中获取测试的自定义testName TestDemo.java @Test(testName = "Browser", description = "This is browser test", priority = 1) public void launchTest() throws IOException { System.out.println("I am in the test"); } 侦听器类 ListenerDemo.java //s

如何在侦听器中获取测试的自定义testName

TestDemo.java

@Test(testName = "Browser", description = "This is browser test", priority = 1)
public void launchTest() throws IOException {

System.out.println("I am in the test");
               }
侦听器类

ListenerDemo.java

//some code

 public void onTestStart(ITestResult result) {

        System.out.println(result.getMethod().getDescription()); //return dexcription as expected
        System.out.println(result.getMethod().getPriority()); //return priority as expected


        //Already Tried options but none of them worked

        System.out.println("Name "+result.getName()); //returns Name = launchTest
        System.out.println("Name "+result.getTestName()); //returns Name = null
        System.out.println("Name "+ result.getMethod().getMethodName()); //returns Name = launchTest

        //I am expecting one of the statement should return Name="Browser"

      }
已经看过了,但这并没有提供任何有用的答案


我正在使用TestNG 7.1.0版。

经过一些研究,我发现了这一点,它对我起了作用

public void onTestStart(ITestResult result) {

     Method method = result.getMethod().getConstructorOrMethod().getMethod();
     Test test = method.getAnnotation(Test.class);
     String testname = test.testName();
}

经过一些研究,我发现了这一点,这对我来说很有效

public void onTestStart(ITestResult result) {

     Method method = result.getMethod().getConstructorOrMethod().getMethod();
     Test test = method.getAnnotation(Test.class);
     String testname = test.testName();
}