Retrieve parameter value from testNG.xml file

前端 未结 1 1687
挽巷
挽巷 2021-01-16 21:48

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

1条回答
  •  礼貌的吻别
    2021-01-16 22:16

    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);
      }
    }
    

    0 讨论(0)
提交回复
热议问题