2

我正在testNG使用Selenium webdriver2.0.

在我的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:@Configuration 方法 initFramework 需要参数“config-file”,但尚未标记为 @Optional 或在

如何使用@Parameters资源文件?

4

2 回答 2

13

看起来您的config-file参数未在<suite>级别定义。有几种方法可以解决这个问题: 1. 确保<parameter>元素定义在<suite>tag 内但在 any 之外<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给方法参数添加注解:

@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
}

在任何情况下,我都建议不要使用几乎相同名称、相同值和不同范围的两个参数。

于 2012-05-21T19:40:25.277 回答
1

你想@Parameter@BeforeSuite. 一旦套件开始执行,套件级别的参数就会被解析,我相信 TestNG@BeforeSuite甚至在套件被处理之前就会调用:

这是一个解决方法:添加ITestContext方法参数以注入

@BeforeSuite(groups = { "abstract" } )
@Parameters({ "configFile" })
public void initFramework(ITestContext context, String configFile) throws Exception {
于 2016-06-22T04:30:22.377 回答