Vignesh
Vignesh

Reputation: 165

Retrieve parameter value from testNG.xml file

enter image description here

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

Upvotes: 0

Views: 10030

Answers (1)

Krishnan Mahadevan
Krishnan Mahadevan

Reputation: 14746

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

Upvotes: 5

Related Questions