Retrieve parameter value from testNG.xml file

时间秒杀一切 提交于 2020-02-29 08:40:07

问题


I want to print the value "iPhone5" from the key parameter name ="webdriver.deviceName.iPhone" .


回答1:


There are basically two ways in which you do this from within a Test Class (A test class is essentially a class that houses one or more @Test/configuration methods)

  1. Via the ITestContext object. You can get access to the current method's ITestResult object by calling Reporter.getCurrentTestResult().getTestContext()
  2. Using Native injection wherein you have TestNG inject a ITestContext object. For more details on native injection please refer to the TestNG documentation here

Here's a sample that shows both these in action.

import org.testng.ITestContext;
import org.testng.Reporter;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class SampleTestClass {

  private static final String KEY = "webdriver.deviceName.iPhone";

  @BeforeClass
  public void beforeClass(ITestContext context) {
    String value = context.getCurrentXmlTest().getParameter(KEY);
    System.err.println("webdriver.deviceName.iPhone = " + value);
  }

  @Test
  public void testMethod() {
    String value = Reporter.getCurrentTestResult().getTestContext().getCurrentXmlTest().getParameter(KEY);
    System.err.println("webdriver.deviceName.iPhone = " + value);
  }
}


来源:https://stackoverflow.com/questions/50304143/retrieve-parameter-value-from-testng-xml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!