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:
groovy Tutorial => Each and EachWithIndex.

 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...

Hi!
I am back to this problem, due to the fact that my code is too large for katalon and I need to optymize it. I try solution to use your idea and i place a line:
findTestData(‘Data file’).getValue(13,Current_row)

Problem is with this Curren_row variable, I am using Test Suite, and it runs line after line of my excel document. I do not know how to get te Current_row

When you say “too large”, do you mean on one Test Case? You can make your test run over more than one Test Case by making a Test Suite and listing all the Test Cases on it. If you need to exceed 5 Test Cases, then I would create another Test Suite to carry on.

I am aware, I use test suite, but single test case is too big. I want also the code to be more robust, that is why I want to use loop for 25 inridients and not making 25 repeate a part of code.

Hmmm. If you mean that you have statically defined Current_row, like Current_row = 3 then you should look up the use of a “for loop”. In this case, you have a counter that you can use to increment, or decrement, which value you have for Current_row.

As an example:

for (int Current_row = 1; Current_row < 7; Current_row++) {
	// move to new page

	WebUI.comment("at row ${Current_row.toString()}")

	myItem = findTestData('Data file').getValue(13,Current_row)
	WebUI.setText(findTestObject('myPage/itemCost'), myItem)
	WebUI.delay(1)
}