Archive for May 19, 2012

Now that i am learning + working on some really cool technology stuff, i thought of sharing one of the best hacks i have written in soapUI Open Source version.

People who have evaluated the soapUI Pro version (Trial or Licensed), must have liked few of the features which are handily + heavily used. One such feature which is exhaustively used is “DataSource” & “DataLoop”. Very simple concept (just looping) and comes very handy when you have to perform the test on the set of data values stored in specific location (say XLS, Text file).

Here is how you can also do it without procuring soapUI Pro license. Yes, you read it right “without procuring soapUI Pro License”. i.e., using few set of Groovy script lines.
In your testcase, add a Groovy Script teststep and name it as “Groovy Script – DataSource”. Paste the following code into newly created teststep.

/*
@Author : Pradeep Bishnoi
@Description : Data Source to read .txt file and pass the value to corresponding property.
@GroovyTestStepName : "Groovy Script - DataSource"
*/

import com.eviware.soapui.support.XmlHolder
def myTestCase = context.testCase
def counter,next,previous,size
File tickerEnumFile = new File("D:/Input.txt") //make sure input.txt file already exists and contains different set of values sepearted by new line (CR).
List lines = tickerEnumFile.readLines()
size = lines.size.toInteger()
propTestStep = myTestCase.getTestStepByName("Property - Looper") // get the Property TestStep
propTestStep.setPropertyValue("Total", size.toString())
counter = propTestStep.getPropertyValue("Count").toString()
counter= counter.toInteger()
next = (counter > size-2? 0: counter+1)
tempValue = lines[counter]
propTestStep.setPropertyValue("Value", tempValue)
propTestStep.setPropertyValue("Count", next.toString())
next++
log.info "Reading line : ${(counter+1)} / $lines.size"
propTestStep.setPropertyValue("Next", next.toString())
log.info "Value '$tempValue' -- updated in $propTestStep.name"
if (counter == size-1)
{
propTestStep.setPropertyValue("StopLoop", "T")
log.info "Setting the stoploop property now..."
}
else if (counter==0)
{
def runner = new com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner(testRunner.testCase, null)
propTestStep.setPropertyValue("StopLoop", "F")
}
else
{
propTestStep.setPropertyValue("StopLoop", "F")
}

Now create a Property teststep and name it as “Property – Looper”. Add few user-defined properties with following name : “Total” , “Value” , “Count” , “Next” , “StopLoop”.

Then add the TestRequest Step, in which you want to pass the data read from the data source. Let’s name it as “GEC_Symbol_Enumeration” or anything. Now open your request and the tag which you want to parameterize and put the property expansion code i.e., ${Property- Looper#Value}

And lastly, add another Groovy Script teststep and name it as “Groovy Script – Data Loop”. Also, paste the following code into newly created teststep.

/*</pre>
@Author : Pradeep Bishnoi
@Description : Data Source Looper responsible for looping a specific teststep.
@GroovyTestStepName : "Groovy Script - Data Loop"
*/
def myTestCase = context.testCase

def runner
propTestStep = myTestCase.getTestStepByName("Property - Looper") // get the Property TestStep
endLoop = propTestStep.getPropertyValue("StopLoop").toString()

if (endLoop.toString() == "T" || endLoop.toString()=="True" || endLoop.toString()=="true")
{
 log.info ("Exit Groovy Data Source Looper")
 assert true
}
else
{
 testRunner.gotoStepByName("Groovy Script - DataSource") //setStartStep
}

And we are done. Now you can execute your testcase and see it will loop across the specified testrequest step with input values read from the provided input file.

Refer this snapshot for quick understanding and issue resolution.

I hope this will help.

NOTE : This code might look dirty & complex. Also, you will find dependencies over the variable/teststep names to be used. Well if you can cleanse it, then please share. This will help other 🙂

Till next blog enjoy looping & happy sharing!!