我想使用Listners打印传递给此方法的参数。
@Test
@Parameters({"browsername","URL", "ebrowse","eURL"})
public static void LaunchApplication(String browsername, String URL, String ebrowse, String eURL ) throws InterruptedException, IOException{我使用了以下代码:
public void onTestSuccess(ITestResult result) {
System.out.println("\nThe method is in " +result.getTestClass().getName());
String paramet = null;
for(Object parameter : result.getParameters()){
paramet += parameter.toString() +",";
}
System.out.println("The parameters of the method are: " +paramet);
}输出:
The parameters of the method are: nullch32,http://localhost:90/fintech/login.html,ch32,http://localhost:90/fintech/login.html,我得到的输出是传递给参数的值。它还会打印我用来初始化字符串的null。如何消除空值,只打印参数而不打印值?
发布于 2018-09-06 03:44:40
要删除null,只需使用"“(空字符串)初始化字符串即可。
String paramet = "";我不认为有一种方法可以打印参数名称...只有值,因为这是TestNG提供的。我不知道你为什么要这样做,因为参数名不会改变,只会改变值。
发布于 2018-09-06 06:12:05
您正在尝试仅获取参数值,即@Parameters注释生成的参数值。您实际需要的是使用反射访问注释本身,并提取传递给它的配置属性。
要获取参数名称,您应该具有类似于以下代码的内容:
String[] parameters = testResult.getMethod().getMethod().getAnnotation(Parameters.class).value();
System.out.println("Parameter 1: " + parameters[0]); //will print browsername
System.out.println("Parameter 2: " + parameters[1]); //will print URL
System.out.println("Parameter 3: " + parameters[2]); //will print ebrowse
System.out.println("Parameter 4: " + parameters[3]); //will print eURLhttps://stackoverflow.com/questions/52191241
复制相似问题