我在类部分的testng.xml参数中就是这样。在dataProvider中准备测试时,我想让serviceParameter
获取参数i使用ITestContext
,但它只显示测试节点中的参数。
如何在课堂上获取参数?
<class name="project.tests.application.ModulesTest">
<parameter name="serviceParameter" value="MDM"/>
<methods>
<include name="checkModule"/>
</methods>
</class>
//DataProvider class
@DataProvider(name = "testModuleData")
public static Object[][] testModuleData(ITestContext context, Method method) throws Exception
{
try
{
Map<String, String> parameters = context.getCurrentXmlTest().getLocalParameters();
log.info(parameters);
[..]
答案 0 :(得分:0)
在@Test
注释之前,添加@Parameters
注释,然后在Test方法中添加一个String数据类型的参数。
例如,您的TestNG文件将是这样的:
<test name="IETest">
<parameter name="browser" value="ie" />
<classes>
<class name="Tests" />
</classes>
</test>
你的测试看起来像这样:
@Parameters("browser")
@BeforeClass
public void beforeTest(String browser) {
if (browser.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", "C:/chromedriver.exe");
driver = new ChromeDriver();
} else if (browser.equalsIgnoreCase("ie")) {
System.setProperty("webdriver.ie.driver", "C:/IEDriverServer.exe");
driver = new InternetExplorerDriver();
} else if (browser.equalsIgnoreCase("firefox")) {
System.setProperty("webdriver.gecko.driver", "C:/geckodriver.exe");
driver = new FirefoxDriver();
}
driver.get(URL);
PageFactory.initElements(driver, this);
}
答案 1 :(得分:0)
如果您想在整个班级中使用您的参数,那么您可以在套件级别定义它并在@beforeclass中初始化
<suite name="Default suite" thread-count="5" group-by-instances="false" verbose="1" allow-return-values="false" parallel="none" data-provider-thread-count="10" junit="false" configfailurepolicy="skip" preserve-order="true" skipfailedinvocationcounts="false">
<parameter name="testParam" value="test123"/>
...
...
在您的测试课程中
@BeforeClass
@Parameters({"testParam"})
public void init(String suiteParam) {
this.suiteParam = suiteParam;
}
@DataProvider(name = "testDataProvider")
public Object[][] createData1() {
String testParam = suiteParam;
int theNums[][] = new int[][] {{10,20},{100,110}};
return new Object[][] {
{ testParam, theNums }
};
}
@Test(dataProvider = "testDataProvider")
public void testingParam( String DPparam1, int[][] DPparam2) {
System.out.println("xml param from @beforeclass param through data provider: " + DPparam1);
System.out.println("xml param from @beforeclass without DP: " + suiteParam);
System.out.println("configured param from data provider: " + DPparam2[0][0]);
}
@Test
public void testingParam2( ) {
System.out.println("xml param from @beforeclass in different method: " + suiteParam);
}
以上打印
xml param from @beforeclass param through data provider: test123
xml param from @beforeclass without DP: test123
configured param from data provider: 10
xml param from @beforeclass in different method: test123