07
Oct

Parse testng.xml parameter values

package DataproviderExample;

import org.testng.ITestContext;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DataProviderwithTestng {

	@Test(dataProvider = "myCustomDP")
	public void myFunction(String x) {
		System.out.println("Hello !! " + x);
	}

	@DataProvider
	public Object[][] myCustomDP(ITestContext context){
        String parameter = context.getCurrentXmlTest().getLocalParameters().get("param1");
        String[] names = parameter.split(",");
        Object[][] returnValues = new Object[names.length][1];
        int index = 0;
        for (Object[] each : returnValues) {
            each[0] = names[index++].trim();
        }
        return returnValues;
	}
}


testng.xml:
===========

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
	<test thread-count="5" name="Test">
		<parameter name="param1" value="Sheetal, Soni, Arunima, Ananya" />
		<classes>
			<class name="DataproviderExample.DataProviderwithTestng" />
		</classes>
	</test> 
</suite>


Output on running testng.xml:

[RemoteTestNG] detected TestNG version 7.0.0
Hello !! Sheetal
Hello !! Soni
Hello !! Arunima
Hello !! Ananya