Thursday, August 9, 2012

SOAPUI: groovy script to choose a different line in a file for each test iteration

We have a Property test step containing 2 properties:
Hostname and linecount
We initialize linecount to 0

At each iteration, this Groovy test step will assign to Hostname the next value from a file C:/pierre/workspace/SSS_AutomatedTests/SOAPUIArtifacts/hostnames.txt
Arrived at the end of the file, it will restart from the first line.

linecount= testRunner.testCase.testSteps["Properties"].getPropertyValue( "linecount" )
linecountInt = linecount.toInteger() + 1
//increment property linecount by one
testRunner.testCase.testSteps["Properties"].setPropertyValue( "linecount", String.valueOf(linecountInt) )

def myhostnamesFile = new File("C:/pierre/workspace/SSS_AutomatedTests/SOAPUIArtifacts/hostnames.txt")

//find number of lines in hostnames file
hostnamesinthefile = 0;
myhostnamesFile.eachLine { hostnamesinthefile++ } "hostnames in the file: " + hostnamesinthefile)

//divide modulo
theLineNumber = (linecountInt % hostnamesinthefile) + 1"theLineNumber to choose=" + theLineNumber)
theHostnameToChoose = ""
//scan all lines, line contains the text and lineNo the line number starting from 1
myhostnamesFile.eachLine() { line, lineNo ->
 if ( lineNo == theLineNumber) 
  theHostnameToChoose = line
} "theHostnameToChoose=" + theHostnameToChoose)

testRunner.testCase.testSteps["Properties"].setPropertyValue( "Hostname", theHostnameToChoose )

No comments: