2012-05-18 48 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 {