How to use for loop variable value inside script?

Hello community,

I need to test submitting a form and want to change the username each time I fill out a new form. I trying to use the variable in my for loop to make the username unique, but the script keeps failing. Here is my code snippet :slight_smile:

WebUI. openBrowser (’’)
WebUI. navigateToUrl (‘’)
for ( int i=1; i<5; i++) {
WebUI. setText ( findTestObject (‘Object Repository/SignUP/input_Last Name_individual-las’), i)

I’m attempting to populate number 1 in Last Name field for the first iteration, 2 in second loop iteration and so on. Any help with this is much appreciated, looked through the forum and videos online but I’m stuck here. Thanks for reading :pray: !!!

Oh, this is the error when I run this script :

Test Cases/Signup/Individual - Copy FAILED because (of) (Stack trace: groovy.lang.MissingMethodException: No signature of method: static com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.setText() is applicable for argument types: (com.kms.katalon.core.testobject.TestObject, java.lang.Integer) values: [TestObject - ‘Object Repository/SignUP/input_Last Name_individual-las’, …]

Try this :

WebUI. setText ( findTestObject (‘Object Repository/SignUP/input_Last Name_individual-las’), i.toString())

1 Like

Thanks a ton that worked, spent an hour trying to get this to work on my own :smiley:
Have a great weekend !!!

Hi @Nilau, thanks for your help earlier ! I’m now trying to submit a form and select a country from a drop down list. There are 200+ countries in the dropdown so I exported them to a test data file and created a for loop for 200 iterations. I’m trying to reference the test data file using the for loop variable but that doesn’t seem to work. Any ideas !? :pray:t4:

Here is the code :slight_smile:

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

WebUI. openBrowser (’’)
WebUI. navigateToUrl (‘’)
WebUI. selectOptionByValue ( findTestObject (‘SignUP/select_country’), findTestData (‘SignUP’).getValue(1, i.toString()), false )

What your actual test case workflow/intended behavior ?

I think it should be something like this :

for (  int  i = 1; i &lt; 200; i++) {

WebUI.openBrowser  ("")
WebUI.navigateToUrl  ('')
WebUI.selectOptionByValue (findTestObject*(‘SignUP/select_country’),  findTestData (‘SignUP’).getValue(1, i),  false  )

You don’t need the “toString()” here. You only need this when you have an object you want to change to a text (String) format. GetValue() take two integers (numbers) as parameters.

1 Like

Thanks for the response !
My testcase is to submit a form with name and country fields. I want to test submission works for all countries in the dropdown, so I need to create 200+ submissions. I exported the list of countries to a test data file and thought I’d reference the test data file in my script. I got this to work using this code, open to optimization ideas :smiley:

WebUI. selectOptionByIndex ( findTestObject (‘SignUP/select_country’), i.toString(), FailureHandling. STOP_ON_FAILURE )

Aaaah gotcha, thanks for the clarification !!

It could be done differently.

int dropdownSize = WebUI.getNumberOfTotalOption(findTestObject(‘SignUP/select_country’);
for(int i=0; i< dropdownSize;i++)
WebUI.selectOptionByIndex(findTestObject(‘SignUP/select_country’), i)

1 Like

sweet, thanks a ton !!!

Hi William

May I ask how your test data looks like please? Trying to do this on my end too.



I am using list as a variable ,So the value which i am trying to print for two times it is printing same time all the values in the particular field