Forum Discussion
nmrao, I tired below approach, we need to execute the teststep script in testcase level
// 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() - any code changes is required please let me know.
Related Content
- 3 years ago
- 6 years ago
- 5 months ago
- 2 years ago