For Loop with Test Data

Hi, I’m hoping someone can help me with my query. I have searched for historic posts and i think other people have asked similar questions and i have tried the suggestions made by other experts but i’m still getting issues.

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 static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
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.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows
import internal.GlobalVariable as GlobalVariable
import org.openqa.selenium.Keys as Keys

WebUI.openBrowser('')

WebUI.navigateToUrl('<private URL>')

WebUI.setText(findTestObject('Object Repository/Page_Login/input_Login_login'), '<private login>)

WebUI.setEncryptedText(findTestObject('Object Repository/Page_Login/input_Login_password'), '<private password>')

WebUI.sendKeys(findTestObject('Object Repository/Page_Login/input_Login_password'), Keys.chord(Keys.ENTER))

WebUI.selectOptionByValue(findTestObject('Object Repository/Page_<something>/select_Live ModeTest Mode'), '1', true)

WebUI.click(findTestObject('Object Repository/Page_<something>/span_Add Group'))

WebUI.selectOptionByValue(findTestObject('Object Repository/Page_<something>/select_noneOption1Option2'), 'Option2', true)

 for (def index : (0..0)) {
    WebUI.setText(findTestObject('Object Repository/Page_<something>/input_Tracking Numbers__tracking_number'), tracking_number)

    WebUI.sendKeys(findTestObject('Object Repository/Page_Parcel Groups/input_Tracking Numbers__tracking_number'), Keys.chord(
            Keys.ENTER))
}

WebUI.click(findTestObject('Object Repository/Page_<something>/span_Submit'))

What i’d like the test case to do is

  1. Open web page
  2. Login
  3. Open a section of the page which will allow multiple inputs
  4. Loop through some test data (csv)
  5. Submit data
  6. Close browser

When i setup a Test Suite with the Test Case and Test Data the browser is opened for each item in the test data, however i need it to keep the browser open and loop through the data and then close the browser.

Is this possible in Katalon?

Thanks!

1 Like

Have a look at the following article:

This article tells you that you have 2 approaches of Data Driven testing in Katalon Studio:

  1. Execution From test Suites
  2. Execute From a Test Case

I am sure, you have applied the “Execution From test Suite” approach. I would suggest to you to try the alternative.

That article shows the following code as an example of “Execute From a Test Case”

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

InternalData data = findTestData(&quot;Demo_Account&quot;)
for (def index : (0..data.getRowNumbers() - 1)) {

    WebUI.openBrowser('')

    WebUI.navigateToUrl('http://demoaut.katalon.com/profile.php')
    WebUI.setText(findTestObject('Page_Login/txt_UserName'), data.internallyGetValue(&quot;demo_usn&quot;, index))
    WebUI.setText(findTestObject('Page_Login/txt_Password'), data.internallyGetValue(&quot;demo_pwd&quot;, index))
    WebUI.click(findTestObject('Page_Login/btn_Login'))
    WebUI.verifyElementPresent(findTestObject('Page_CuraHomepage/btn_MakeAppointment'), 0)

    WebUI.closeBrowser()
}

This code repeats opening/closing Web browser, which you want to avoid.

Then all you should do is to change the code slightly: move WebUI.openBrowser('') and WebUI.closeBrowser() out of the for loop.

...
WebUI.openBrowser('')
WebUI.navigateToUrl('http://demoaut.katalon.com/profile.php')
for (def index : (0..data.getRowNumbers() - 1)) {
    ...
    WebUI.verifyElementPresent(findTestObject('Page_CuraHomepage/btn_MakeAppointment'), 0)
}
WebUI.closeBrowser()

Thanks very much - will take a look

It looks like i am experiencing this issue instead:

Possibly becuase i have the ‘open browser’ as the first step in my script - however that is unavoidable as i need to open the browser!

Any ideas here? thanks

I do not see what you are experiencing. Please have a look at the following guidance.

https://forum.katalon.com/t/tip-how-to-help-us-help-you/12919/29

Thanks - i’ve ditched using the Test Suite and used the Test Case help you provided in your orginal reply to me and its working great…thanks!

But i’ve had to change to use an Internal Data set - instead of a CSV. The CSV option meant i could copy and paste into the CSV easily with new data each day but i can’t see a way of doing that easily with Internal Data

this is kind of where i am…How can we write to Internal Data File in Katalon

but don’t know whether the gist would help me…maybe moving to a database or somehow getting back to using CSVData

i’ve got it working now using the CSVData class https://docs.katalon.com/javadoc/com/kms/katalon/core/testdata/CSVData.html

thanks for your help!