"No one is harder on a talented person than the person themselves" - Linda Wilkinson

March 18, 2011

TestNG Example - testng - Passing parmeters in XML File

Our next post is based on my question in stackoverflow site. Let's try an example based on Cedric reply.

Below is example code
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, String D)
{ 
            System.out.println("Passing Three parameter to Test "+A +" and "+B + " and "+C);
            String[] elements = D.split(",");
            for (String element : elements)
            {
                  System.out.println("Item is "+element);
                 
            }
}
}

XML for setting parameters

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<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,6,7,8"/> 
    <classes>
      <class name="TestNGXMLData"/>
    </classes>
  </test>
</suite>

Right Click on XML and run it as TestNG Test. Below is output in console window
Passing Three parameter to Test 1 and 2 and 3
Item is 4
Item is 5
Item is 6
Item is 7
Item is 8

Extending this example and adding multiple tests and setting it for parallel execution and preserve test order.

Modified XML File
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="classes" thread-count="5">
  <test name="firstTest" preserve-order="true">
   <parameter name="A" value="1"/> 
   <parameter name="B" value="2"/> 
   <parameter name="C" value="3"/> 
   <parameter name="D" value="4,5,6,7,8"/> 
    <classes>
      <class name="TestNGXMLData"/>
    </classes>
  </test>
  <test name="secondTest" preserve-order="true">
   <parameter name="A" value="10"/> 
   <parameter name="B" value="20"/> 
   <parameter name="C" value="30"/> 
   <parameter name="D" value="40,50,60,70,80"/> 
    <classes>
      <class name="TestNGXMLData"/>
    </classes>
  </test>
  <test name="thirdTest" preserve-order="true">
   <parameter name="A" value="100"/> 
   <parameter name="B" value="200"/> 
   <parameter name="C" value="300"/> 
   <parameter name="D" value="400,500,600,700,800"/> 
    <classes>
      <class name="TestNGXMLData"/>
    </classes>    
  </test>
</suite>

Modified code adding try-catch block (Keeping up with Coding Guidelines :))

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, String D)
{ 
            try
            {
                  System.out.println("Passing Three parameter to Test "+A +" and "+B + " and "+C);
                  String[] elements = D.split(",");
                  for (String element : elements)
                  {
                        System.out.println("Item is "+element);
                       
                  }
            }
            catch(Exception e)
            {
                  System.out.println("Exception Occurred "+ e.toString());
            }
}
}

Output

Passing Three parameter to Test 1 and 2 and 3
Item is 4
Item is 5
Item is 6
Item is 7
Item is 8
Passing Three parameter to Test 10 and 20 and 30
Item is 40
Item is 50
Item is 60
Item is 70
Item is 80
Passing Three parameter to Test 100 and 200 and 300
Item is 400
Item is 500
Item is 600
Item is 700
Item is 800

Happy Reading!!

No comments: