Increasing variable name in loop

Hi!

I neet to input many variables in inputs while running test suite basing on excel sheet.
In data file i have variables Ingridient1 to Ingridient23 and variable amount1 to amount23 and NrOfRuns which describe how many Ingridinets there is in one row of data file.

I look for solution how to increase variable name in a loop for every runs eg:

for ( int i=1; i<NrOfRuns; i++) {
WebUI.setText(findTestObject(‘Object Repository/Page/input’), Ingridient1)
WebUI.setText(findTestObject(‘Object Repository/Page/input_1’), Amount1)
}

The purpose is to know how to make the script smaller (not having 23 lines but 1 loop) :slight_smile:

Are you familiar with Lists, Arrays or other Collections. For these, you increment an indices attached to the variable, not exactly like you want but it is “the way” :slight_smile: to go.

Lists in Groovy | Baeldung

for ( int i = 0; i < NrOfRuns; i++) {
   WebUI.setText(findTestObject('Object Repository/Page/input'), Ingridient[i])
   WebUI.setText(findTestObject('Object Repository/Page/input_1'), Amount[i])
}

You cannot increment variable reference names like you are hoping. I agree with @grylion54, reading the data into a list, then iterating the list seems like the best solution.

not sure if will help with test data maped to the suite, but, if you change the approach to use testDataFactory instead :
https://docs.katalon.com/javadoc/com/kms/katalon/core/testdata/TestDataFactory.html
provided your data file has a fixed structure and it is exactly ordered as you described, you will have to iterate over the elements of a single row.
supposed in your current execution you have the data into a curr_row variable, you will have:

curr_row[1] <-- Ingredient1
curr_row[23] <-- Ingredient23
… and so on

for the Amount columns, you will do some math …

the above is if you access the data by using the
public String getValue(int columnIndex, int rowIndex) method.

However katalon TestData API provides another signature for this method:
public String getValue(String columnName, int rowIndex)

So in your current iteration you can build a string variable using interpolation, something like:
ingredient_idx = “Ingredient${i}” //here i assume i is the index
and access the column by:
ingredient = yourTestData.getValue(ingredient_idx, curr_row)

The same you do for Amount.

ref: https://docs.katalon.com/javadoc/com/kms/katalon/core/testdata/TestData.html

Hi!

I got familiar with List, and was able to create list of elements from excel file.

I tried to take value from list and put it in input on page but it shows failure.

List<List> data = findTestData(“Data Files/test”).getAllData();
List values = new ArrayList();
for(List row : data) {
values.addAll(row);
}

for ( int i = 1; i < NrOfRuns; i++) {

WebUI.click(findTestObject(‘Page/input_Kid_downshift-8-input’))
WebUI.setText(findTestObject(‘Page/input_Kid_downshift-8-input’), ArrayList[i])

Failure:

Reason:
groovy.lang.MissingMethodException: No signature of method: static java.util.ArrayList.getAt() is applicable for argument types: (java.lang.Integer) values: [1]

where the iteration is supposed to stop, since you do also:
ArrayList[i]) (which may be 0 indexed, but you start to iter at 1)

a better approach will be to iterate over the list you create, see for reference:
https://riptutorial.com/groovy/example/32092/each-and-eachwithindex#:~:text=groovy%20Ways%20of%20Iteration%20in%20Groovy%20Each%20and%20EachWithIndex&text=each%20and%20eachWithIndex%20are%20methods,change%20the%20default%20iterator%2Findex.

 ArrayList[i])

perhaps you should provide here the values variable created before …

It is a good attempt but I wonder why are you using the “extra” ArrayList, values. You have a perfectly good List of data from Excel. What about using it one by one, like you use the “extra” List.

I don’t know where NrOfRuns is initialized, but that is small issue. There is a statement to get the total number of rows from Excel.

Perhaps, something like below:

def data = TestDataFactory.findTestData("Data Files/test")

NrOfRuns = data.getRowNumbers()

for ( int i = 1; i < NrOfRuns ; i++) {

    WebUI.click(findTestObject('Page/input_Kid_downshift-8-input'))
    WebUI.setText(findTestObject('Page/input_Kid_downshift-8-input'), data.getValue(2, i)

If you stay with your concept, then you at least need to change the below:

to List<String> data...