我正在使用带有Selenium webdriver2.0的testNG. 在我的testNG.xml中,我有 suite data-provider-thread-count="2" name="selenium FrontEnd Test" parallel="false" skipfailedinvocationCounts="false" thread-count="2" parameter name="confi
在我的testNG.xml中,我有
<suite data-provider-thread-count="2" name="selenium FrontEnd Test" parallel="false" skipfailedinvocationCounts="false" thread-count="2">
<parameter name="config_file" value="src/test/resources/config.properties/"/>
<test annotations="JDK" junit="false" name="CarInsurance Sanity Test" skipfailedinvocationCounts="false" verbose="2">
<parameter name="config-file" value="src/test/resources/config.properties/"/>
<groups>
<run>
<include name="abstract"/>
<include name="Sanity"/>
</run>
</groups>
<classes>
</classes>
</test>
</suite>
在java文件中
@BeforeSuite(groups = { "abstract" } )
@Parameters(value = { "config-file" })
public void initFramework(String configfile) throws Exception
{
Reporter.log("Invoked init Method \n",true);
Properties p = new Properties();
FileInputStream conf = new FileInputStream(configfile);
p.load(conf);
siteurl = p.getProperty("BASEURL");
browser = p.getProperty("BROWSER");
browserloc = p.getProperty("BROWSERLOC");
}
得到错误
AILED CONFIGURATION: @BeforeSuite initFramework
org.testng.TestNGException:
Parameter ‘config-file’ is required by @Configuration on method initFramework
but has not been marked @Optional or defined in
如何将@Parameters用于资源文件?
看起来您的配置文件参数未在< suite>中定义水平.有几种方法可以解决这个问题:1.确保<参数>元素在< suite>中定义标签但在任何< test>之外:
<suite name="Suite1" >
<parameter name="config-file" value="src/test/resources/config.properties/" />
<test name="Test1" >
<!-- not here -->
</test>
</suite>
2.如果您希望在Java代码中具有参数的默认值,尽管它是否在testng.xml中指定,您可以将@Optional注释添加到method参数:
@BeforeSuite
@Parameters( {"config-file"} )
public void initFramework(@Optional("src/test/resources/config.properties/") String configfile) {
//method implementation here
}
编辑(基于发布的testng.xml):
选项1:
<suite>
<parameter name="config-file" value="src/test/resources/config.properties/"/>
<test >
<groups>
<run>
<include name="abstract"/>
<include name="Sanity"/>
</run>
</groups>
<classes>
<!--put classes here -->
</classes>
</test>
</suite>
选项2:
@BeforeTest
@Parameters( {"config-file"} )
public void initFramework(@Optional("src/test/resources/config.properties/") String configfile) {
//method implementation here
}
无论如何,我建议不要让两个参数具有几乎相同的名称,相同的值和不同的范围.
