FOR Loop within a specified set of steps with Data File

Ok guys, this probably a very simple task but I’m not familiar with Katalon and there is no tutorial or existing post on this that I can find. What I have a very simple script.

  1. Open URL,
  2. Login as user,
  3. Verify something exist,
  4. log out,

Next User

  1. Login as Next user
  2. Verify something exist,
  3. log out,

Next user

  1. Login as Next user
  2. Verify something exist,
  3. log out,

ETC…

Now I have created the Data File (from spreadsheet, it has the parameter of USERID.

I know you have load the file first some how, then call the next USERID for each loop. What I can’t find in the tutorials or forum posts that shows how to do that. I saw the DataDriven tutorial that lead m to create the spreadsheet file of USERID_DATA. Then I saw the information at https://docs.katalon.com/display/KD/Control+Statements. I also found some sort of FOR LOOP statement in a thread somewhere but that information didn’t help either.

But what I can’t find is how to put this together. A screen shot shows what I have so far but doesn’t work.

Can anyone please explicitly explain to me how to do this?

Thanks for any help in advance.

KatalonForLoopSteps.png

Does NOONE have any information on this???

Hi there,
There are some tutorial videos about using data driven on youtube:



Your case is very simple, hope those links can help you.
Thanks.

1 Like

Hey DuyLuong,

While the information in the 3rd (bottom) video was slightly more insightful, it still didn’t show me how to loop through data in the ‘Test Case’ itself. What I’m looking for is guidance on the FOR loop statement in a ‘test’ case’ not the ‘test suite’. my image was not retained from the initial post and not sure why. BUT here is what I have but I KNOW i’m missing something just not sure what. Below is what I have so far. i have my data file defined, i’m missing something that loops through the rows of the data file for the users. Again, the last video you posted was pretty close BUT it doesn’t show me how to the perform the FOR loop in a test case from the data file.

def data = TestDataFactory(“UserID_TesData”)

WebUI.openBrowser(’’)

WebUI.navigateToUrl(‘https://www.website.com/home’)

for (def index : (0…data.getRowNumbers() - 1)) {
WebUI.click(findTestObject(‘Home (1)/a_Log In’))

WebUI.waitForElementVisible(findTestObject('Home (1)/input_txtpassword'), 30)

WebUI.setText(findTestObject('Home (1)/input_txtusername'), UserID_S)

WebUI.setText(findTestObject('Home (1)/input_txtpassword'), '<password>')

WebUI.click(findTestObject('Home (1)/button_Log In'))

WebUI.rightClick(findTestObject('Quick Entry (1)/span_Hi'))

WebUI.click(findTestObject('Quick Entry (1)/span_Log Out'))

}

not_run: WebUI.closeBrowser()

Ok here is a followup: I got a little further but now running into an error I have no clue about… Might be a code issue??

Here is the error message:

11-08-2017 10:11:42 AM - [ERROR] - Test Cases/Test7 ASD Validation FAILED because (of) 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, com.kms.katalon.core.testdata.InternalData, java.lang.Integer) values: [TestObject - ‘Object Repository/Home (1)/input_txtusername’, …]
Possible solutions: setText(com.kms.katalon.core.testobject.TestObject, java.lang.String), setText(com.kms.katalon.core.testobject.TestObject, java.lang.String, com.kms.katalon.core.model.FailureHandling), getText(com.kms.katalon.core.testobject.TestObject), getText(com.kms.katalon.core.testobject.TestObject, com.kms.katalon.core.model.FailureHandling)

Here is my script:
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.checkpoint.CheckpointFactory as CheckpointFactory
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as MobileBuiltInKeywords
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testcase.TestCaseFactory as TestCaseFactory
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testdata.TestDataFactory as TestDataFactory
import com.kms.katalon.core.testobject.ObjectRepository as ObjectRepository
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WSBuiltInKeywords
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUiBuiltInKeywords
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable
import org.openqa.selenium.Keys as Keys
import com.kms.katalon.core.testdata.InternalData as InternalData

InternalData data = findTestData(‘UserID_TestData’)

WebUI.openBrowser(’’)

WebUI.navigateToUrl(‘https://test.com/home’)

for (def index : (0…data.getRowNumbers() - 1)) {
WebUI.click(findTestObject(‘Home (1)/a_Log In’))

WebUI.waitForElementVisible(findTestObject('Home (1)/input_txtpassword'), 30)

WebUI.setText(findTestObject('Home (1)/input_txtusername'), UserID_S, index)

WebUI.setText(findTestObject('Home (1)/input_txtpassword'), '<password>')

WebUI.click(findTestObject('Home (1)/button_Log In'))

WebUI.rightClick(findTestObject('Quick Entry (1)/span_Hi'))

WebUI.click(findTestObject('Quick Entry (1)/span_Log Out'))

}

not_run: WebUI.closeBrowser()

Please note I do have my data in the data file defined with variable as UserID_S.
ALSO note that the import com.kms.katalon.core.testdata.InternalData as InternalData was copied from one of the tutorials I found on Katalon.
ANY help on this would be highly appreciated…

Hi,

I have the similar problem. I think for loop statement only execute once and then browser closed.
Is there any setting I must do…?
NB: I copy paste the script from tutorial on Katalon.com

Regards,
Broto W

The Data-Driven tutorial shows in the script example that u retrieve the value from your data in the following way:

WebUI.setText(findTestObject(‘Page_Login/txt_UserName’), data.internallyGetValue(“demo_usn”, index))

I think you are only missing the data.internallyGetValue method in your script.

Gerard van der Winkel,

So what the video shows above is how to get the data based on full execution of the script… NOT FROM WITHIN A FOR LOOP… How does this help me in my situation?

So I added this as you stated above despite the fact that they were not using a FOR loop…

WebUI.setText(findTestObject(‘Page_Home (1)/input_txtusername’), data.internallyGetValue(“UserID_S”, index))

The error I got was:
Test Cases/User Validation FAILED because (of) java.lang.ArrayIndexOutOfBoundsException: -1

So again, I need ALL of the required code segments to add to the script to be able to LOOP through the Data File until it reaches the end of the file and then finish and close out browser. What you gave me failed for data.internallyGetValue(“UserID_S”, index))

If I can get a example of this all I’ll need to do is replace your variables with mine for this to work. But I need to know what all is needed before I can start to understand how it all pieces together.

If there is ANY special setup that needs to happen to assign a variable to the 1 column data in the data file, that would be helpful to know as well. I’m guessing I have this setup correct as I use UserID_S as the variable name, this linked to the column in the data file… There is only 1 column in the data file for usernames btw.

1 Like

Ok, SO here is what I have so far for understanding how this works…

  1. I got this working by creating a ‘Internal Data’ file (not excel ), Right click Data File > New > Test Data > Internal Data option (please note I have the column name of UserID)
  2. The very first (row 1) is skipped and starts with Row 2 for some reason, I haven’t figured this out yet…
  3. In the FOR statement… YOU MUST start with the #1 … for (def index : (1…data.getRowNumbers() -1)) {… DO not start with zero (0), this will cause an error.

Below is the code I got work for this ( please note i’m only showing what worked for in bold below with the relevant code pieces:

import com.kms.katalon.core.testdata.InternalData as InternalData

InternalData data = findTestData(‘UserID_TestData’)

WebUI.openBrowser(’’)

WebUI.navigateToUrl(‘https://test.com/home’)

for (def index : (1…data.getRowNumbers() - 1)) {

WebUI.click(findTestObject(‘Home (1)/a_Log In’))

WebUI.setText(findTestObject(‘Home (1)/input_txtusername’), data.internallyGetValue(‘UserID’, index))

I have not tried to figure this out with excel spreadsheet. IF anyone knows how to setup and handle an Excel file in the same manor please post it here or point me in the right direction please. I did try to create the same file structure with excel spreadsheet and just replace the variables in the above script but that didn’t work. I got errors. So, again, I’m missing something. I hope this post helps other to be able to use a FOR Loop with Internal Data file with in a Test Case and NOT from the Test Suite.

Ok guys, I found the issue #2 above… When I used import com.kms.katalon.core.testdata.InternalData as InternalData
with
InternalData data = findTestData(‘UserID_TestData’)

This above was the cause my issue…

WHEN i used the following:
import com.kms.katalon.core.testdata.TestData as TestData
with
TestData data = findTestData(‘UserID_TestData’)

The above solved my issue where the very first row of the Internal Data ‘file’ was getting skipped.

3 Likes

Hello!
Did you find a way?

I’m trying to do the same thing but I don’t know how.