0

I am dealing with creating correct TestNG data provider Object. My goal is to create structure {customerCode, countryCode} e.g. "345","US". I was able to load data from testNG.xml file into two separate arrays, but finally how to put the code so it will return first element from one array paired with first element from second array and so on.

Here is my code, that returns two arrays which is not intended. I want to paired to Object[][] and use it as dataProvider.

public Object[][] createData1(ITestContext context) {
    String customerCodesStr = context.getCurrentXmlTest().getLocalParameters().get("customerCode");
    String[] customerCode = customerCodesStr.split(",");

    String countryCodeStr = context.getCurrentXmlTest().getLocalParameters().get("countryCode");
    String[] countryCode = countryCodeStr.split(",");

    Object[][] result = new Object[][]  {customerCode,countryCode} ;
    return result;
}

TestNG

<parameter name="customerCode" value="1234, 4357" />
<parameter name="countryCode" value="US, MEX" />

It is kind of primitive question how to play with objects, but it is tricky to do that. Well it could be done using for cycle, but I wanted to avoid it. Are there any suggestions?

Michal
  • 610
  • 10
  • 24
  • Ok, I put it like this, any better ideas? Object[][] result = new Object[customerCode.length][countryCode.length]; for (int i = 0; i < customerCode.length; i++) { result[i][0] = customerCode[i]; } for (int i = 0; i < countryCode.length; i++) { result[i][countryCode.length-1] = countryCode[i]; } – Michal Jun 21 '18 at 08:55

1 Answers1

0

Got it!

Object[][] result = new Object[][] {};

    for (int i = 0; i < customerCode.length; i++) {
        result = ArrayUtils.add(result, new Object[] {customerCode[i],countryCode[i]});
    }

    return result;
Michal
  • 610
  • 10
  • 24