问题
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)
- Via the
ITestContextobject. You can get access to the current method'sITestResultobject by callingReporter.getCurrentTestResult().getTestContext() - Using Native injection wherein you have TestNG inject a
ITestContextobject. 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