本文介紹了從 testNG.xml 文件中檢索參數(shù)值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我想從鍵 parameter name ="webdriver.deviceName.iPhone" 打印值 "iPhone5" .
I want to print the value "iPhone5" from the key parameter name ="webdriver.deviceName.iPhone" .
推薦答案
基本上有兩種方法可以在測試類中執(zhí)行此操作(測試類本質(zhì)上是一個包含一個或多個 @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
對象.您可以通過調(diào)用Reporter.getCurrentTestResult().getTestContext()
來訪問當前方法的 - 使用原生注入,其中您有 TestNG 注入
ITestContext
對象.有關(guān)本地注入的更多詳細信息,請參閱 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);
}
}
這篇關(guān)于從 testNG.xml 文件中檢索參數(shù)值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!