Retrieve parameter value from testNG.xml file(从 testNG.xml 文件中检索参数值)
问题描述
我想从键 parameter name ="webdriver.deviceName.iPhone" 打印值 "iPhone5" .
I want to print the value "iPhone5" from the key parameter name ="webdriver.deviceName.iPhone" .
推荐答案
基本上有两种方法可以在测试类中执行此操作(测试类本质上是一个包含一个或多个 @Test 的类
/配置方法)
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)
- 通过
ITestContext
对象.您可以通过调用Reporter.getCurrentTestResult().getTestContext()
来访问当前方法的 - 使用原生注入,其中您有 TestNG 注入
ITestContext
对象.有关本地注入的更多详细信息,请参阅 TestNG 文档此处
ITestResult
对象- Via the
ITestContext
object. You can get access to the current method'sITestResult
object by callingReporter.getCurrentTestResult().getTestContext()
- 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);
}
}
这篇关于从 testNG.xml 文件中检索参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 testNG.xml 文件中检索参数值


- Eclipse 的最佳 XML 编辑器 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 转换 ldap 日期 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 获取数字的最后一位 2022-01-01