# Increasing variable name in loop

**URL:** <https://forum.katalon.com/t/increasing-variable-name-in-loop/61857>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [January 21, 2022, 7:51pm UTC](https://forum.katalon.com/t/increasing-variable-name-in-loop/61857 "2022-01-21T19:51:45Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![gumislaw](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/gumislaw/32/16104_2.png) [@gumislaw](https://forum.katalon.com/u/gumislaw)\
**Post date:** [January 21, 2022, 7:51pm UTC](https://forum.katalon.com/t/increasing-variable-name-in-loop/61857/1 "2022-01-21T19:51:45Z")

</div>

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) 🙂

---

<div class="post-metadata">

**Author:** ![grylion54](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/grylion54/32/16265_2.png) [@grylion54](https://forum.katalon.com/u/grylion54)\
**Post date:** [January 21, 2022, 9:24pm UTC](https://forum.katalon.com/t/increasing-variable-name-in-loop/61857/2 "2022-01-21T21:24:06Z")

</div>

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” 🙂 to go.

[Lists in Groovy | Baeldung](https://www.baeldung.com/groovy-lists)

```auto
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])
}

```

---

<div class="post-metadata">

**Author:** ![Brandon\_Hein](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/brandon_hein/32/1092_2.png) [@Brandon\_Hein](https://forum.katalon.com/u/Brandon_Hein)\
**Post date:** [January 22, 2022, 9:34pm UTC](https://forum.katalon.com/t/increasing-variable-name-in-loop/61857/3 "2022-01-22T21:34:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anon46315158](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@anon46315158](https://forum.katalon.com/u/anon46315158)\
**Post date:** [January 23, 2022, 9:31am UTC](https://forum.katalon.com/t/increasing-variable-name-in-loop/61857/4 "2022-01-23T09:31:17Z")

</div>

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](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](https://docs.katalon.com/javadoc/com/kms/katalon/core/testdata/TestData.html)

---

<div class="post-metadata">

**Author:** ![gumislaw](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/gumislaw/32/16104_2.png) [@gumislaw](https://forum.katalon.com/u/gumislaw)\
**Post date:** [April 1, 2022, 12:38pm UTC](https://forum.katalon.com/t/increasing-variable-name-in-loop/61857/5 "2022-04-01T12:38:54Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![anon46315158](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@anon46315158](https://forum.katalon.com/u/anon46315158)\
**Post date:** [April 1, 2022, 4:04pm UTC](https://forum.katalon.com/t/increasing-variable-name-in-loop/61857/6 "2022-04-01T16:04:19Z")

</div>

> [@gumislaw](#):
>
> for ( int i = 1; i \< NrOfRuns; i++) {

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](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).

```auto
 ArrayList[i])

```

perhaps you should provide here the `values` variable created before …

---

<div class="post-metadata">

**Author:** ![grylion54](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/grylion54/32/16265_2.png) [@grylion54](https://forum.katalon.com/u/grylion54)\
**Post date:** [April 1, 2022, 4:10pm UTC](https://forum.katalon.com/t/increasing-variable-name-in-loop/61857/7 "2022-04-01T16:10:54Z")

</div>

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:

```auto
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:

> [@gumislaw](#):
>
> ```auto
> List<List> data...
> 
> ```

to `List<String> data...`

---

<div class="post-metadata">

**Author:** ![gumislaw](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/gumislaw/32/16104_2.png) [@gumislaw](https://forum.katalon.com/u/gumislaw)\
**Post date:** [December 9, 2022, 11:34am UTC](https://forum.katalon.com/t/increasing-variable-name-in-loop/61857/8 "2022-12-09T11:34:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![grylion54](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/grylion54/32/16265_2.png) [@grylion54](https://forum.katalon.com/u/grylion54)\
**Post date:** [December 9, 2022, 3:15pm UTC](https://forum.katalon.com/t/increasing-variable-name-in-loop/61857/9 "2022-12-09T15:15:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gumislaw](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/gumislaw/32/16104_2.png) [@gumislaw](https://forum.katalon.com/u/gumislaw)\
**Post date:** [December 14, 2022, 5:47pm UTC](https://forum.katalon.com/t/increasing-variable-name-in-loop/61857/10 "2022-12-14T17:47:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![grylion54](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/grylion54/32/16265_2.png) [@grylion54](https://forum.katalon.com/u/grylion54)\
**Post date:** [December 14, 2022, 9:03pm UTC](https://forum.katalon.com/t/increasing-variable-name-in-loop/61857/11 "2022-12-14T21:03:55Z")

</div>

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:

```auto
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)
}

```
