We've examined how to write to a database as well as use a file as a DataSource and how to get parameters from a database. Now the time has come to see how we can get parameters from a Groovy DataSource.
Implementation
This tutorial follows the form and the functionality of earlier tutorials.
As usual, we'll first examine what we want to do:
- Get an Amazon User ID from a configuration file
- Create a list of Authors with a Groovy script
- Call Amazons ItemSearch with the User ID and Author
- Handle the Response in some way
- Loop until done
All these can be gotten by downloading the Tutorial .zip.
Step o: Start the project
First we create a project from the URL http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl. In this we’ll choose the ItemSearch request and add this to a TestCase.
We can then trim the request down to containing what we want.
<soapenv:Envelope xmlns:soapennv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://webservices.amazon.com/AWSECommerceService/2007-05-14">
<soapenv:Header/>
<soapenv:Body>
<ns:ItemSearch>
<ns:SubscriptionId></ns:SubscriptionId>
<ns:Shared>
<!--Optional:-->
<ns:Author> </ns:Author>
<ns:SearchIndex> </ns:SearchIndex>
</ns:Shared>
<!--Zero or more repetitions:-->
<ns:Request></ns:Request>
</ns:ItemSearch>
</soapenv:Body>
</soapenv:Envelope>
Step 1: Get your ID
Now we have to populate the request with data and the first step is populating the element <ns:SubscriptionId></ns:SubscriptionId>. (Note, if you don’t have a subscription ID for Amazon Web Services, get it now). We get the data by using a configuration file containing just one line;
SubscriptionID=[your ID]
and retrieving this in a PropertiesStep. We also have a second property, resultCount, which we’ll use later, more about this later, and a Property named type referring to the type of search we’re doing, i.e. books.
Step 2: Create the Groovy Script
Let’s go on to actually populating the request and get to the heart of this tutorial. Let’s add a DataSource.
We also will add one property, Author which will be used by the Groovy script.
Then create the Groovy script itself which is pretty simple.
def list = context.myList
if( list == null )
{
list = ["Iain Banks","Douglas Coupland","William Vollman","William Gibson"]
context.myList = list;
}
if( !list.empty )
{
result.Author = "" + list[0];
list.remove( 0 )
}
Once done, you can test the entire step.
Step 2: DataTransfer
Now that we have collected the data, let’s transfer it into the request.
Let’s add a PropertyTransfer Step with three transfers, MoveAuthor, MoveMyID, MoveType. We can use the XPath Selector tool to make the transfer a breeze.
We have made a small movie showing this step. Watch it here (in a new window)
Step 3: Check the test
What we have now should look like this.
- Gotten base parameters
- Gotten Test Data from a Groovy script
- Transferred into a search
- The Search Request, like this:
Step 4: Using the result
Remember we had a property called resultCount? Let’s use this, we make another PropertyTransfer step, that takes the number of hits from the ItemSearch response and move it to the property, remember to use the XPath selector. Also remember that you have to run the request in order to be able to use the selector, without a proper request you won’t have a proper response to choose from.
Step 5: Two Groovy Steps
In order to see that this is working, let’s log the result and show a dialog with the result.
We add a first Groovy script containing the following;
log.info( "Author: " + context.getProperty( "Groovy Retrieve#Author" ) + ", Books: " + context.getProperty( "My ID#ResultCount" ))
Then we add a second groovy scrip containing the following;
// get target step
def step = testRunner.testCase.getTestStepByName( "My ID" );
def step2 = testRunner.testCase.getTestStepByName( "Groovy Retrieve" );
com.eviware.soapui.support.UISupport.showInfoMessage(
"Got " + step.getPropertyValue( "ResultCount" ) + " hits for author [" +
step2.getPropertyValue( "Author" ) + "]" );
Step 5: Loop it!
Lastly, in order to use all data in the database, we need to finish the test with a DataSource Loop step.
Now we can run the entire test, and should be receiving the following:
And that is it! You should now have a working test!
So that is it! It isn’t harder than that.
You can get the entire project here.
|