从 testNG.xml 文件中检索参数值

Retrieve parameter value from testNG.xml file(从 testNG.xml 文件中检索参数值)
本文介绍了从 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)

  1. 通过 ITestContext 对象.您可以通过调用 Reporter.getCurrentTestResult().getTestContext()
  2. 来访问当前方法的 ITestResult 对象
  3. 使用原生注入,其中您有 TestNG 注入 ITestContext 对象.有关本地注入的更多详细信息,请参阅 TestNG 文档此处
  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);
  }
}

这篇关于从 testNG.xml 文件中检索参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Reliable implementation of PBKDF2-HMAC-SHA256 for JAVA(PBKDF2-HMAC-SHA256 for JAVA 的可靠实现)
Correct way to sign and verify signature using bouncycastle(使用 bouncycastle 签名和验证签名的正确方法)
Creating RSA Public Key From String(从字符串创建 RSA 公钥)
Why java.security.NoSuchProviderException No such provider: BC?(为什么 java.security.NoSuchProviderException 没有这样的提供者:BC?)
Generating X509 Certificate using Bouncy Castle Java(使用 Bouncy Castle Java 生成 X509 证书)
How can I get a PublicKey object from EC public key bytes?(如何从 EC 公钥字节中获取 PublicKey 对象?)