SoapUI: Groovy Scripts: CSV

SOAPUI Groovy Scripts: CSV

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

  • Open Flie/DB connection
  • Read from CSV
  • Re-Name Test Steps from Excel Columns
  • Get Values from file 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

//Create a new filereader object, using the context variable so it can be used between test components
context.fileReader = new BufferedReader(new FileReader("C:\\PerTableData\\TestData.csv"))
//Read in the first line of the data file
firstLine = context.fileReader.readLine()
//Split the first line into a string array and assign the array elements to various test case properties
String[] propData = firstLine.split(",")
testCase.setPropertyValue("ElName",propData[0])
testCase.setPropertyValue("AtNum",propData[1])
testCase.setPropertyValue("Symbol",propData[2])
testCase.setPropertyValue("AtWeight",propData[3])
testCase.setPropertyValue("BoilPoint",propData[4])
//Rename request test steps for readability in the log; append the element name to the test step names
testCase.getTestStepAt(0).setName("GetAtomicNumber-" + propData[0])
testCase.getTestStepAt(1).setName("GetAtomicWeight-" + propData[0])
testCase.getTestStepAt(2).setName("GetElementySymbol-" + propData[0])

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")
context.fileReader.close()

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

/*Read in the next line of the file
  We can use the same fileReader created in the Setup script because it
  was assigned to the context variable.*/
nextLine = context.fileReader.readLine()
/*If the end of the file hasn't been reached (nextLine does NOT equal null)
  split the line and assign new property values, rename test request steps,
  and go back to the first test request step*/
if(nextLine != null){
	String[] propData = nextLine.split(",")
	curTC = testRunner.testCase
	curTC.setPropertyValue("ElName",propData[0])
	curTC.setPropertyValue("AtNum",propData[1])
	curTC.setPropertyValue("Symbol",propData[2])
	curTC.setPropertyValue("AtWeight",propData[3])
	curTC.setPropertyValue("BoilPoint",propData[4])
	curTC.getTestStepAt(0).setName("GetAtomicNumber-" + propData[0])
	curTC.getTestStepAt(1).setName("GetAtomicWeight-" + propData[0])
	curTC.getTestStepAt(2).setName("GetElementSymbol-" + propData[0])
	testRunner.gotoStep(0)

}
Tags