TestNG data provider logic

Hi All,

TestNG data provider returns an array of 19 values. But @Test method forces me to get the 19 values in different variables.

I feel there will be some other way to receive those 19 values through an array again in @ Test method.

Could anyone hep me to achieve this?

Many Thanks,
Revathi

Add Comment
3 Answer(s)

Revathi,

The whole idea here is to group your complete data in a data structure and pass it across to the test. I will simply put the code here for you to understand

@DataProvider(name = "simpleDataProvider")
 public Object[][] simpleDataProvider()
 {
 Dictionary testData = new Hashtable<String, String>();
 testData.put("UserName", "Testuser");
 testData.put("Password", "SamplePassword");
 testData.put("PhoneNumber", "878978979");
Object[][] dataToTestMethod = new Object[1][1];
 dataToTestMethod[0][0] = testData;
 return dataToTestMethod;
 }

@Test(dataProvider = "simpleDataProvider")
 public void simpleTest(Object testData)
 {
 Dictionary dataDict = (Dictionary)testData;
System.out.println("The value of username is " + dataDict.get("UserName"));
 System.out.println("The value of password is " + dataDict.get("Password"));
 System.out.println("The value of phone number is " + dataDict.get("PhoneNumber"));
 }
Virender Singh Professor Answered on May 21, 2015.
Add Comment

Hi Virender Singh,

Thanks for your reply.

My snippet is like the below,

@DataProvider(name=”Test data”)

public Object[][] BookAnEyetest() throws Exception{

ExcelUtils.setExcelFile(Constant.Path_TestData+Constant.File_TestData,Constant.strSheetName);

Object[][] testObjArray = ExcelUtils.getTableArray();

return (testObjArray);

}

@Test (dataProvider = “BET”)

public static void fnMain(Object tabArray){

List<Object> paramlist= new ArrayList<Object>();

String sCtrlValue = null;

…….
}

Exception:
FAILED: fnMain
org.testng.TestNGException:
The data provider is trying to pass 19 parameters but the method executionEngine.DriverScript#fnMain takes 1
at org.testng.internal.Invoker.injectParameters(Invoker.java:1337)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1225)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

Even after trying the solution i am getting the same result.

Thanks,
Revathi

Revathi Train Answered on May 21, 2015.
Add Comment

Revathi,

The method that takes rely on a data provider should have exactly same number of input arguments as the number of values the data provider is trying to pass,

ExcelUtils.getTableArray(); it seems is returning 19 elements in an array and what you are accepting in the TestMethod is just one argument.

You try to understand your getTableArray() method and based on that define the signature of TestMethod.

Virender

Virender Singh Professor Answered on May 21, 2015.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.