如何在@BeforeSuite中使用testNG @Parameters来读取资源文件 [英] How to use testNG @Parameters in @BeforeSuite to read resource file

查看:797
本文介绍了如何在@BeforeSuite中使用testNG @Parameters来读取资源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 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 $ b $需要参数'config-file' b但尚未标记为@Optional或在

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 对于资源文件?

How to use @Parameters for resource file?

推荐答案

看起来像 config-file 参数未在< suite> 级别定义。有几种方法可以解决这个问题:
1.确保< parameter> 元素在< suite> 标记,但在任何< test> 之外:

Looks like your config-file parameter is not defined at the <suite> level. There are several ways to solve this: 1. Make sure the <parameter> element is defined within <suite> tag but outside of 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 方法参数的注释:

2. If you want to have the default value for the parameter in the Java code despite the fact it is specified in testng.xml or not, you can add @Optional annotation to the method parameter:

@BeforeSuite
@Parameters( {"config-file"} )
public void initFramework(@Optional("src/test/resources/config.properties/") String configfile) {
    //method implementation here
}

编辑(基于发布的testng.xml):

EDIT (based on posted 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
}

无论如何,我会建议不要使用名称几乎相同,值相同且范围不同的两个参数。

In any case, I would recommend not having two parameters with almost identical names, identical values, and different scope.

这篇关于如何在@BeforeSuite中使用testNG @Parameters来读取资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆