Forum Discussion

Samrayen's avatar
Samrayen
Occasional Contributor
12 months ago

[URGENT]-Data driven - While executing the test excel data test-case level it's executing 2 time

addId1addId2addId3addId4addId5addId6addId7addId8addId9addId10
test2test2test2test2910
21222122212221221920
^%^&%%32^%^&%%32^%^&%%32^%^&%%321920
41 41 41 41 1920
511.0511.0511.0511.01920

 

// 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}") }

     

  • nmrao's avatar
    nmrao
    Champion Level 3

    How does the test case look a like? Screen shot would help.

    May be you can try by disabling all the test steps except groovy script step of the test case to prevent from running.

    • Samrayen's avatar
      Samrayen
      Occasional Contributor

      yes I tried, problem is my requirement, need to capture  every  teststep execution request and response payload result capture in my html report. if am disabling the teststep will not get the result for every teststep level in my report

       

       

      • nmrao's avatar
        nmrao
        Champion Level 3

        How is the current report generated? Any files generated in certain location on execution of each step? Can't that be achieved with in groovy?

  • Samrayen's avatar
    Samrayen
    Occasional Contributor

    So, the execution of steps are now running once after disabling steps?

    No it's not working if am passing invalid data in my excel  my teststep script status is pass. not working as expected. 

  • nmrao's avatar
    nmrao
    Champion Level 3

    That is correct as per the current script which is not checking the status after the execution.

              testStep.run(testRunner, context)

    Please refer below script to check the result after running the each step.

    def result = testStep.run(testRunner, context)
    assert result.status == com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus.FAILED, "$testStep.name failed"
      • Samrayen's avatar
        Samrayen
        Occasional Contributor

        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. 

  • nmrao's avatar
    nmrao
    Champion Level 3

    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}") }