[URGENT]-Data driven - While executing the test excel data test-case level it's executing 2 time
addId1 | addId2 | addId3 | addId4 | addId5 | addId6 | addId7 | addId8 | addId9 | addId10 |
test | 2 | test | 2 | test | 2 | test | 2 | 9 | 10 |
21 | 22 | 21 | 22 | 21 | 22 | 21 | 22 | 19 | 20 |
^%^&%% | 32 | ^%^&%% | 32 | ^%^&%% | 32 | ^%^&%% | 32 | 19 | 20 |
41 | 41 | 41 | 41 | 19 | 20 | ||||
51 | 1.0 | 51 | 1.0 | 51 | 1.0 | 51 | 1.0 | 19 | 20 |
// Specify the path to your Excel file
def excelFilePath = "D:/soapuibuild/build/excel/datadrivens.xlsx"
// Initialize FileInputStream
def fileInputStream = new FileInputStream(new File(excelFilePath))
// Load the Excel workbook
def workbook = new org.apache.poi.xssf.usermodel.XSSFWorkbook(fileInputStream)
// Get the sheet (assuming data is in the first sheet)
def sheet = workbook.getSheetAt(1)
// Define a map to store test step names
def testStepMap = [
1: "Teststep-add1",
2: "Teststep-add2",
3: "Teststep-add3",
4: "Teststep-add4",
5: "Teststep-add5",
6: "Teststep-add6",
7: "Teststep-add7",
8: "Teststep-add8",
9: "Teststep-add9",
10:"Teststep-add10",
]
// Function to handle different cell types and return cell value as String
def getCellValue(cell) {
if (cell != null) {
switch (cell.getCellType()) {
case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_NUMERIC:
if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {
return cell.getDateCellValue().toString()
} else {
def numericValue = cell.getNumericCellValue()
return numericValue == (int) numericValue ? "${numericValue.toInteger()}" : "${numericValue}"
}
case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING:
return cell.getStringCellValue()
case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BLANK:
return ""
default:
return ""
}
} else {
return ""
}
}
// Loop through rows (start from 1 to skip header)
for (int i = 1; i <= sheet.lastRowNum; i++) {
def row = sheet.getRow(i)
// Loop through cells in the row
for (int j = 0; j < row.getLastCellNum(); j++) {
// Get the cell value
def paramValue = getCellValue(row.getCell(j))
// Log the current parameter value
log.info("Param${j + 1}: ${paramValue}")
// Set properties for API request dynamically
testRunner.testCase.setPropertyValue("Param" + (j + 1), paramValue)
}
// Construct the test step name dynamically
def testStepName = testStepMap[i]
if (testStepName) {
// Run the test step
def testStep = testRunner.testCase.getTestStepByName(testStepName)
if (testStep) {
testStep.run(testRunner, context)
} else {
log.warn("Test step not found: ${testStepName}")
}
} else {
log.warn("No test step defined for row ${i}")
}
// Clear properties for the next iteration
for (int j = 0; j < row.getLastCellNum(); j++) {
testRunner.testCase.setPropertyValue("Param" + (j + 1), "")
}
}
// Close the FileInputStream
fileInputStream.close()
while executing the above script from teststep level it's working as executed. while executing testcase level it's executing 2 time, first executing pass excel data in request payload, 2 run it passing empty data in the request payload.
problem : No need to run 2nd time from the testcase level and first run need to iterate the value as per the excel data. should not pass the last row of data in my all test-step request payload.
If any one have a solution pleas share your input
Is there any question? I didn't see the update as well
Samrayen wrote:
if (testStep) { testStep.run(testRunner, context) } else { log.warn("Test step not found: ${testStepName}") }