How to use testNG @Parameters in @BeforeSuite to r

2019-02-07 15:25发布

问题:

I am using testNG with Selenium webdriver2.0.

In my testNG.xml I have

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

In java file

@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");

}

Getting error as

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

How to use @Parameters for resource file?

回答1:

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

EDIT (based on posted testng.xml):

Option 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>

Option 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.



回答2:

You want to use @Parameter in @BeforeSuite. Suite level parameters are parsed once the suite begins execution and I believe TestNG invokes @BeforeSuite even before the suite is processed:

Here is a workaround: add ITestContext in method parameters to inject

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


标签: java testng