2012-05-18 123 views
1

我使用testNGSelenium webdriver2.0如何在@BeforeSuite中使用testNG @Parameters读取资源文件

在我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配置:@BeforeSuite initFramework org.testng.TestNGException: 参数“配置文件”是由@Configuration在met上需要的HOD initFramework 但仍未标记@optional或

定义如何使用@Parameters的资源文件?

+0

你的testng.xml数据缺少您的帖子.. –

+0

请发布您的testng.xml文件的内容 - 它需要看到'config-file'参数的上下文。 – artdanil

回答

7

看起来像你的config-file参数未在<suite>级别定义。有几种方法来解决这个问题: 1.确保<parameter>元素<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注释方法参数:

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

在任何情况下,我建议不具有两个参数几乎相同的名字,相同的值,和不同的范围。

+0

我的testng.xml内容 –

+0

<包括名称=” 抽象 “/> <包括名称=” 理智“/>

+0

问题与该XML是你有两个参数:配置文件“config_file”和测试层的“config-file”,我建议在suite层次上留下一个参数,并相应地命名 - Java文件中的名字和XML应该是相同的。你想在考试级别离开,然后ch将@Framework(String)方法的注解改为@BeforeTest。另外请注意,您的类定义没有指定要运行的任何类。你能够执行任何测试吗? – artdanil

1

您想在@BeforeSuite中使用@Parameter。一旦该套件开始执行套房级别的参数解析,我相信TestNG的调用@BeforeSuite套房被处理甚至在:

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

@BeforeSuite(groups = { "abstract" }) 
@Parameters({ "configFile" }) 
public void initFramework(ITestContext context, String configFile) throws Exception {