SoapUI: Groovy Scripts

Groovy Script in SOAPUI allow for reaching out to external data and return reporting elements and data

  • Open Flie/DB connection
  • Read from Excel/Database
  • Re-Name Test Steps from Excel Columns
  • Get Values from spreadsheet/database for test step(s) values
  • Use variables/properties for
    • logging
    • Renaming Test Cases on-the-fly
    • Populating Request Messages
    • Populating Response Messages
    • Resetting Test Case to default values
  • Closing Flies/DB connections

Script Logging

log.info( "NEXT: : " + curTC.getPropertyValue("Symbol") ) 
log.info( "Test Case TearDown.." )

 

TestSuite

SetupScript

log.info(runner.getTestSuite().toString())
log.info(runner.toString())

 

TestCase

Test Case Properties

Test Properties are VARIABLES used in a test case. Access them by #TestCase#VARIABLE

  • ElName - Uranium
  • AtNum - 92
  • Symbol - U
  • AtWeight - 238.03
  • BoiPoint - 4091

 

Setup Script

import org.apache.poi.hssf.usermodel.*;

//Create data formatter
dFormatter = new HSSFDataFormatter()

//Create a new workbook using POI API
srcBook = new HSSFWorkbook(new FileInputStream(new File("C:\\PerTableData\\TestData-Mix.xls")))

//Create formula evaluator to handle formula cells
fEval = new HSSFFormulaEvaluator(srcBook)

//Get first sheet of the workbook (assumes data is on first sheet)
sourceSheet = srcBook.getSheetAt(0)

//Sets row counter to 0 (first row)-- if your sheet has headers, you can set this to 1
context.rowCounter = 0

//Read in the contents of the first row
sourceRow = sourceSheet.getRow(0)

//Step through cells in the row and populate property values-- note the extra work for numbers
elNameCell = sourceRow.getCell(0)
testCase.setPropertyValue("ElName",dFormatter.formatCellValue(elNameCell,fEval))

atNumCell = sourceRow.getCell(1)
testCase.setPropertyValue("AtNum",dFormatter.formatCellValue(atNumCell,fEval))

symbolCell = sourceRow.getCell(2)
testCase.setPropertyValue("Symbol",dFormatter.formatCellValue(symbolCell,fEval))

atWtCell = sourceRow.getCell(3)
testCase.setPropertyValue("AtWeight",dFormatter.formatCellValue(atWtCell,fEval))

boilCell = sourceRow.getCell(4)
testCase.setPropertyValue("BoilPoint",dFormatter.formatCellValue(boilCell,fEval))

//Rename request test steps for readability in the log; append the element name to the test step names
testCase.getTestStepAt(0).setName("GetAtomicNumber-" + testCase.getPropertyValue("AtNum"))
testCase.getTestStepAt(1).setName("GetAtomicWeight-" + testCase.getPropertyValue("AtWeight"))
testCase.getTestStepAt(2).setName("GetElementySymbol-" + testCase.getPropertyValue("Symbol"))

//Add references to sheet to re-use it in ReadNextLine step
context.srcWkSheet = sourceSheet

TearDown Script

//Cleanup: rename test steps to their generic names and close the file reader
testCase.getTestStepAt(0).setName("GetAtomicNumber")
testCase.getTestStepAt(1).setName("GetAtomicWeight")
testCase.getTestStepAt(2).setName("GetElementSymbol")

Test Step Request

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET">
<soapenv:Header/>
<soapenv:Body>
    <web:GetAtomicNumber>
        <!--Optional:-->
        <web:ElementName>${#TestCase#ElName}</web:ElementName>
    </web:GetAtomicNumber>
</soapenv:Body>
</soapenv:Envelope>

 

Test Step Groovy: Read Next Line

Groovy Script

    import org.apache.poi.hssf.usermodel.*;

    cellDataFormatter = new HSSFDataFormatter()

    //Create formula evaluator
    fEval = new HSSFFormulaEvaluator(context.srcWkSheet.getWorkbook())

    //Increment the rowcounter then read in the next row of items
    context.rowCounter++;

    if(context.rowCounter<=context.srcWkSheet.getLastRowNum()){//Check if we've reached the last row
        curTC = testRunner.testCase
        sourceRow = context.srcWkSheet.getRow(context.rowCounter)//Get a spreadsheet row

        //Step through cells in the row and populate property data
        elNameCell = sourceRow.getCell(0)
        curTC.setPropertyValue("ElName",cellDataFormatter.formatCellValue(elNameCell,fEval))
    
        atNumCell = sourceRow.getCell(1)
        curTC.setPropertyValue("AtNum",cellDataFormatter.formatCellValue(atNumCell,fEval))
    
        symbolCell = sourceRow.getCell(2)
        curTC.setPropertyValue("Symbol",cellDataFormatter.formatCellValue(symbolCell,fEval))
    
        atWtCell = sourceRow.getCell(3)
        curTC.setPropertyValue("AtWeight",cellDataFormatter.formatCellValue(atWtCell,fEval))
    
        boilCell = sourceRow.getCell(4)
        curTC.setPropertyValue("BoilPoint",cellDataFormatter.formatCellValue(boilCell,fEval))
    
        //Rename test cases for readability in the TestSuite log
        curTC.getTestStepAt(0).setName("GetAtomicNumber-" + curTC.getPropertyValue("AtNum"))
        curTC.getTestStepAt(1).setName("GetAtomicWeight-" + curTC.getPropertyValue("AtWeight"))
        curTC.getTestStepAt(2).setName("GetElementSymbol-" + curTC.getPropertyValue("Symbol"))
    
        //Go back to first test request with newly copied properties
        testRunner.gotoStep(0)
    }

 

Tags