How to use for loop variable value inside script?


#1

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 (‘https://www.test.com’)
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’, …]


#2

Try this :

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


#3

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


#4

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 (‘https://paradigm-fe5ghyt.webflow.io/request-access’)
WebUI. selectOptionByValue ( findTestObject (‘SignUP/select_country’), findTestData (‘SignUP’).getValue(1, i.toString()), false )
}


#5

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  ('https://paradigm-fe5ghyt.webflow.io/request-access')
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.


#6

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 )


#7

Aaaah gotcha, thanks for the clarification !!


#8

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


#9

sweet, thanks a ton !!!