Monday, November 9, 2009

Testing Restful services with Jmeter

Outside of Transaction Controllers, regular expressions and xpath extractors are probably used frequently in your scripts.  Sometimes you have the need to collect multiple variables from one or more responses and store them in one variable that will become a data parameter in a subsequent request.
Generally if you read through Jmeter User Group threads you’ll see lots of people trying to do this or at least asking how do I store variables in an array type of data structure.
In order to do this



lets review a few concepts about regex, and Xpath extractors.  When you use these types of extractors your parsing a response because your interested in one more pieces of information.  This information your interested in matches a pattern.  All matches to the pattern are stored in  capture groups.  These groups are referenced by there  variable name and an underscore with a number for the specific capture group.
If you’ve used regex or Xpath before its fairly easy to specify a specific value by applying a pattern, and then using its resulting capture group when needed. 
But what do you do when you need to collect all the capture groups and put them together to create a specific data parameter.
Enter Beanshell
Beanshell is a light weight java like scripting language that allows you to do work with Jmeter variables.  In the following scenario let’s assume I have to test a rest API service which returns an XML response.  In the next request I will need to take specific response data from the 1st API request and collect the data by including a comma in between data values which will generate from our capture groups to be used in the next out going request.
Our first API response is the following:

<user>
<name>Adam Rowe</name>
<uri>myapi/user/+Adam+Rowe</uri>
<id> Adam Rowe</id>
</user>

<user>
<name>Albert Cruise</name>
<uri>myapi/user/Albert+Cruise</uri>
<id>Albert Cruise</id>
</user>

<user>
I’ve highlighted the parts of the response that I”m interested in in blue.  We want to be able to collect the users id values which are shown by <id> tag in blue.
We then need to append them in in one variable and store a comma value in between each of the capture groups.   The following screen shot shows how we would then use this newly created variable that contains a list of ids shown here as ${list_of_users}
api1_snapshot
Our test plan:
api2_treeshot
In the response of ListUsers Transaction /datamanager/Collection/all we can apply a xpath extractor to collect the different <id> tag values which will be returned in the response.  In Requesting Users Transaction specifically the /user request we apply a BeanShell Pre-processor which is applied before the request.  In this Beanshell preprocess element we will access different variables that are set in the Jmeter scope in order to reference them in the beanshell scope.  Below is a sample of basic code used to do this.




This simple code snippet does the following:
a) gets the total number of Xpath capture groups another words the total times id’s was collected
b) Loops through the total number of capture groups appending the value and a comma into the         “list_of_users” variable.

c) it removes any extra unneeded characters
d) places the newly constructed variable back in jmeter scope.


You can download the template or if your fancy pants use git
git://github.com/mordechai/Sample_Code.git 

No comments:

Post a Comment