Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

testng - passing list as parmeters in testng.xml

Tags:

java

xml

testng

Would it be possible to pass a list in the testNG parameters. Below is sample code

Example: Trying to pass list of numbers in XML. Not sure if TestNG does not support this feature. Or am i missing anything?

 <suite name="Suite" parallel="none">  
     <test name="Test" preserve-order="false">  
         <parameter name="A" value="1"/>   
         <parameter name="B" value="2"/>   
         <parameter name="C" value="3"/>   
         <parameter name="D" value="{4,5}"/>   
         <classes>  
             <class name="TestNGXMLData"/>  
         </classes>  
     </test>  
 </suite>  

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.thoughtworks.selenium.Selenium;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.*;
import com.thoughtworks.selenium.*;

public class TestNGXMLData {

    @Test
    @Parameters(value = { "A", "B", "C", "D" })
    public void xmlDataTest(String A, String B, String C, ArrayList<String> ls) {

        System.out.println("Passing Three parameter to Test " + A + " and " + B + " and " + C);

        Iterator it = ls.iterator();
        while (it.hasNext()) {
            String value = (String) it.next();
        }
    }
}

Thanks, Siva

like image 580
Siva Avatar asked Mar 15 '11 09:03

Siva


People also ask

Is it possible to pass test data through a TestNG XML file?

TestNG allows the user to pass values to test methods as arguments by using parameter annotations through testng. xml file. Some times it may be required for us to pass values to test methods during run time. Like we can pass user name and password through testng.

Which annotation is used in TestNG to pass a parameter from TestNG XML to a test method?

Parameterization In TestNG Using @Parameters Annotation & XML file. As of now, Parameters have been scoped to only Suite or Test tag in testng. xml file. If the same parameter value is passed in both Suite and Test, the priority is given to parameter value passed in the Test tag.

What is the tag used to specify parameters from TestNG XML file?

TestNG Parameters are present in the xml file. They can be applied either inside the tag or tag. If we want to apply the parameters to all the test cases, then the parameters are applied inside the tag.


1 Answers

You can only pass basic types like this, so you should declare your last parameter as a "String" and then convert "{3, 4}" to a List. I suggest using "3 4" instead and simply parse it with String#split.

If you want to pass more complex parameters and you don't want to bother with converting, switch to using a @DataProvider.

like image 55
Cedric Beust Avatar answered Oct 06 '22 02:10

Cedric Beust