Omegaspard
Omegaspard

Reputation: 1960

TestNG, 0 or multiples parameters from a xml file

i'm using the testNG framework. I have tests that can have parameter or not. By exemple i could have the following xml :

 <?xml version="1.0" encoding="UTF-8"?>
   <suite name="Suite" parallel="tests">
   <test name="FlowTest0">
       <parameter name="node" value="http://192.168.117.135:5555/wd/hub"/>
       <parameter name="name" value="nameTest0"/>
       <parameter name="direction" value="IN"/>
       <classes>
           <class name="selenium.test.flow.FlowSaveTest"/>
       </classes>
   </test>
   </suite>

Or i could have this one :

   <?xml version="1.0" encoding="UTF-8"?>
   <suite name="Suite" parallel="tests">
   <test name="FlowTest0">
       <parameter name="node" value="http://192.168.117.135:5555/wd/hub"/>
       <classes>
           <class name="selenium.test.flow.FlowSaveTest"/>
       </classes>
   </test>
   </suite>

Or no parameters at all or a lot more. Then in my test case i get the parameters this way :

@Parameters({"name", "direction"})
public void initTest(String name, String direction) {
    //code of the method
}

What i want is to be able to pass a variable number of value depending what i have in the xml. Can i do this with the testNG parameters annotation ?

EDIT 1 :

I would expect something like the declaration of the printf declaration in C, something like :

@Parameters("node", Something that will get all the paramaters)
@BeforeTest
public void initTest(Object ...) {

}

Upvotes: 0

Views: 754

Answers (1)

juherr
juherr

Reputation: 5740

You can inject the ITestContext and get all attributes from there:

@Test
public void initTest(ITestContext context) {
    Map<String, String> params = context.getCurrentXmlTest().getAllParameters();
    // ...
}

Upvotes: 3

Related Questions