How to get the current row index of the test data in a test case when data iteration is set to all using katalon studio

Hi Guys,

I trust that you all well. I’m new to Katalon Studio. I would like to find out how to get the current row index of the test data in a test case when the data iteration property is set to all. I would like to use this row index to write to a different cell with thin that row. I could not find any documentation on this without having to explicitly create a for loop in the test case script to iterate through the test data.

How do I go about this?

Many thanks

1 Like

No. Katalo does not tell you the index.

Alternative idea:

If you add a column in which you type the number as the index, then your script will get the number as one of the data items. See the left most column of the following picture.

Katalon API does not allow you to overwrite anything into the “test data”. No “setXXX(newdata)” method is provided.


Alternative idea:

Don’t try overwrite it, make a copy with additions.

Your test case can make a copy of the input data file into another file. Your test case accepts the fileA as input, it copies all rows, and write the copied row into another fileB. Katalon does not provide any support of “copying”; you are entirely responsible to implement that. While copying each row, the script can append any number of data items which you will save into the output fileB. The fileB would contain the result you wanted.

Please note that you don’t need to know the current row index at all in oder to implement this desgin.

To retrieve the current row index when using Data Iteration = All in Katalon Studio without manually looping through test data, follow these steps:

1. Use Built-in testCaseContext (Recommended)

Access the row index directly via the testCaseContext binding variable in your test case script:

// In your test case script:
import com.kms.katalon.core.context.TestCaseContext

// Get the current row index (0-based)
int currentRowIndex = (testCaseContext as TestCaseContext).getTestCaseData().getRowIndex()

Note:

  • This method works in Katalon Studio v8.0+.
  • Returns the 0-based index (e.g., first row = 0, second row = 1).

2. Track Index Using a Static Variable

If Katalon doesn’t support testCaseContext.getRowIndex(), use a static counter:

Step 1: Create a Custom Keyword

// File: Keywords/DataIndexTracker.groovy
class DataIndexTracker {
    static int currentRowIndex = 0
}

Step 2: Increment Index in Test Case Setup

Add this to your test case’s Setup Script:

DataIndexTracker.currentRowIndex++

Step 3: Access the Index in Your Test Case

// Get 1-based index (e.g., first row = 1)
int rowIndex = DataIndexTracker.currentRowIndex

// Get 0-based index (first row = 0)
int rowIndexZeroBased = DataIndexTracker.currentRowIndex - 1

3. Use Test Data API (For Manual Index Lookup)

If you need to match data dynamically, load the test data and find the index:

import com.kms.katalon.core.testdata.TestDataFactory

// Load your test data
def testData = TestDataFactory.findTestData("YourTestDataName")

// Get all rows as a list
List<String[]> allRows = []
for (int i = 0; i < testData.getRowNumbers(); i++) {
    allRows.add(testData.getObject(i))
}

// Get current row data
def currentRow = testData.getObject()

// Find the index (0-based)
int rowIndex = allRows.indexOf(currentRow)

4. Write Back to Test Data (Advanced)

To modify the test data during execution (e.g., write to another cell):

import com.kms.katalon.core.testdata.TestDataFactory

// Load test data
def testData = TestDataFactory.findTestData("YourTestDataName")

// Get current row index (0-based)
int rowIndex = (testCaseContext as TestCaseContext).getTestCaseData().getRowIndex()

// Update data in a specific column (e.g., column 3)
testData.setValue(3, rowIndex, "New Value")

Key Notes

  • 1-based vs. 0-based Index: Adjust according to your needs (e.g., rowIndex + 1 for 1-based).
  • Parallel Execution: Static variables may cause conflicts if tests run in parallel. Use testCaseContext for thread safety.
  • Test Data Sources: Works with Excel, CSV, and internal data.

By using these methods, you can dynamically track or calculate the current row index during data-driven executions

I doubt this:

I tried a Test Case script, which didn’t compile

2025-05-16 18:00:02.355 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2025-05-16 18:00:02.359 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/Data-driven samples/Order and check out multiple products
2025-05-16 18:00:02.778 INFO  c.k.katalon.core.main.TestCaseExecutor   - (Default) productName = Flying Ninja
2025-05-16 18:00:03.019 DEBUG t.Order and check out multiple products  - 1: currentRowIndex = getTestCaseData().getRowIndex()
2025-05-16 18:00:03.043 ERROR c.k.katalon.core.main.TestCaseExecutor   - ❌ Test Cases/Data-driven samples/Order and check out multiple products FAILED.
Reason:
groovy.lang.MissingPropertyException: No such property: testCaseContext for class: Script1566468485697
	at Order and check out multiple products.run(Order and check out multiple products:13)
	at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
	at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:486)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:477)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:456)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:448)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:322)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:142)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:130)
	at TempTestCase1747385994533.run(TempTestCase1747385994533.groovy:25)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

2025-05-16 18:00:03.068 ERROR c.k.katalon.core.main.TestCaseExecutor   - ❌ Test Cases/Data-driven samples/Order and check out multiple products FAILED.
Reason:
groovy.lang.MissingPropertyException: No such property: testCaseContext for class: Script1566468485697
	at Order and check out multiple products.run(Order and check out multiple products:13)
	at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
	at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:486)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:477)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:456)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:448)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:322)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:142)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:130)
	at TempTestCase1747385994533.run(TempTestCase1747385994533.groovy:25)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

test case index=0
2025-05-16 18:00:03.357 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/Data-driven samples/Order and check out multiple products

The property testCaseContext is not available for a Test Case script. I wonder if @dineshh verified this answer himself hands-on. Maybe he didnt.


I believe that the property testCaseContext will be available only in a @AfterTestCase-annotated method in a TestListener.

class NewTestListener {
	/**
	 * Executes before every test case starts.
	 * @param testCaseContext related information of the executed test case.
	 */
	@BeforeTestCase
	def sampleBeforeTestCase(TestCaseContext testCaseContext) {
		println testCaseContext.getTestCaseId()
		println testCaseContext.getTestCaseVariables()
	}

Are these functions (getTestCaseData().getRowIndex()) available at in the TestCaseContext import on the free version of Katalon Studio? No so sure why I cannot access them

// In your test case script:
import com.kms.katalon.core.context.TestCaseContext

// Get the current row index (0-based)
int currentRowIndex = (testCaseContext as TestCaseContext).getTestCaseData().getRowIndex()