Groovy Automated DataSource Loopers
I understand that this is a SoapUI Pro forum but I think creating the loop steps through groovy manually can be a good lesson and can help you customise your loops to your heart (and/or requirement)'s content!
So without further ado this is how I script my groovy loopers
NOTE: in this example I am connecting to an OracleDB to fuel my requests.
1) Structure:
The structure within my test case is quite simple: loopStarter to connect to the db/initialise the data - storing the current information into properties, response to send these properties off to the API through a request, loopEnder to either propagate or exit the loop if the condition is met.
2) loopStarter
Connection to/creation of your data source, be it a database, excel etc
In this example I will be using the groovy.sql class (documentation) to connect to my db, and store the results into properties
import groovy.sql.Sql sql = Sql.newInstance(<connection details>) def res = sql.rows(<sql query>) def loopProperties = testRunner.testCase.getTestStepByName("loopProperties")
//will initialise count in the loopProperties step if count does not yet exist if(!loopProperties.hasProperty("count")){ loopProperties.setPropertyValue("count","0") } def count = Integer.parseInt(loopProperties.getPropertyValue("count"))
//store the properties from the current result loopProperties.setPropertyValue("x",res[count].x) loopProperties.setPropertyValue("y",res[count].y) loopProperties.setPropertyValue("querySize",(String)res.size()) sql.close()
sql.rows returns an array of ArrayLists, so to access the current result (more on this later) we will use res[count]. Just to reiterate this step ONLY sets the properties to be sent off in the request. querySize is set so we can continue looping over all the results from the query.
3) response
To then call your properties into your request step we will do this:
<soapenv:Envelope namespace:ns="namespace"> <soapenv:Header/> <soapenv:Body> <ns:x>${loopProperties#x}</ns:x> <ns:y>${loopProperties#y}</ns:y> </soapenv:Body> </soapenv:Envelope>
if you choose not to store your properties in a property step and wanted to store them at the test case level you can fetch them like so:
... <ns:x>${#TestCase#x}</ns:x> ...
4) loopEnder
This is the loop exit condition. It will tell the loop whether or not there are more results from your query to send through the request. This is also a pretty simple step:
- check count vs querySize
- increment count if true
- go to loopStarter if true, to update the properties (count has been increased and because of this, so will the current result)
def loopProperties = testRunner.testCase.getTestStepByName("loopProperties") def count = Integer.parseInt(loopProperties.getPropertyValue("count")) def querySize = Integer.parseInt(loopProperties.getPropertyValue("querySize")) if( count<querySize-1 ){ count = count+1 loopProperties.setPropertyValue("count", (String)count) testRunner.gotoStepByName("loopStarter") }
5) reset count
Unnecessary step but I like getting into the habbit of clearing all properties after I'm done, and this groovy script will do it for you
def loopProperties = testRunner.testCase.getTestStepByName("loopProperties") String[] removals = loopProperties.getPropertyNames() for(i=0;i<removals.size();i++){ loopProperties.removeProperty(removals[i]) }
I've been thinking of doing something similar to this for the Excel DataSource script Olga_T mentioned, well similar to the loopStarter step anyway - doing it completely through groovy so SoapUI free users can do it too, I just need to look into parsing exel files with groovy.
Anyways I hope this helps you all,
Mo