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