Ninth in a series focusing on using JMeter for functional testing of an API.
In the last tutorial we jumped through some hoops in order to be able to extra one folderid from a list of folders. Some of the decision we made would end up causing us problems later on. For instance, the way we extracted a folderid would require us to provide a folderIndex every time we used GetFolders. Since that may not always be the case, it would actually require us to do more work in the instance when we didn’t need to extract a folderid.
In this tutorial, we will replace some of what we did last time with a BeanShell object and write a little bit of Java too.
Before we begin make sure you have the test plan from the previous tutorial or download this one to use.
We will also be utilizing an external library in this tutorial in order to more easily work with JSON responses from the server. Download the minimal-json library here and copy it into your JMeter installation in the lib folder. You can read more about minimal-json here.
Functional Testing 09: Life in a Beanshell
- To begin, open the test plan, expand Sampler LIbrary > Get Folders> GET Folders samplers and delete the JSON Extractor.
- Then expand Files Tests > Get Files > jp@gc – Parameterized Controller and delete the Random Variable. Yes, we just undid almost an entire tutorials worth of work! But don’t worry. We will actually do it better now!
- Right click the Parameterized Controller and select Add > Post Processors > BeanShellPostProcessor.
- Drag the BeanShell PostProcessor up to just beneath Get Folders.
- A BeanShell object lets you write Java code to do things that might not be possible or easy by using the provided JMeter objects. What we are going to use it for is to select a random folderid from the previous response, and then write that value into a getFolderId variable for GET Files to use. We will essentially replace the Random Variable object and the JSON extractor in Get Folders with one little Java script. Long term this will be more flexible and easier to maintain. Note: if you are not familiar with Java, don’t worry I will give you all the code. Additionally, there is a ton of info on how to write Java out on the internet, so you will almost always be able to search for a teach yourself anything you need.
- Click the BeanShell PostProcessor and click in the Script section.
- First thing we are going to do is get the response data from the previous Get Folders call. We have access to the previous SampleResult via the ‘prev’ variable in BeanShell. In the script area type:
//get previous response as a JSON object
String str_response = prev.getResponseDataAsString();
JsonObject json_response = Json.parse(str_response).asObject();
JsonArray folderList = json_response.get(“folders”).asArray();
These lines of code get the previous response data as a string. We then use the minimal-json library we installed prior to starting this tutorial to convert the response into an object we can now easily use (a JsonArray of the files).
- Next let’s get the count of folders in the list. Below the prevous code add:
//determine the size of the folder list
int size = folderList.size();
This will tell us how many folders are in the list and give us the upper bound for choosing a random entry in the list.
- Next we want to generate a random number to use to select one of the folders. So we will want a random a whole number between zero and size-1 (inclusive). Remember arrays index starting at zero, so the first folder in the list is index  in the array. Under the current import line at the top of the script add:
then at the end of script add:
//pick a random index between 0 (inclusive) and size (exclusive)
Random rand = new Random();
int index = rand.nextInt(size);
These lines of code will instance a new random number generator and pick a random integer (whole number) between zero (inclusive) and size (exclusive). So if our array has five entries, possible random indexes would be 0, 1, 2, 3, 4.
- Now that we have an index let’s get the id of the folder at that index. At the end of the script add:
//get folderid using the random index
JsonObject folder = folderList.get(index).asObject();
int folderid = folder.get(“id”).asInt();
These lines will get the folder at the array index we randomly chose and then get the value of the folder’s id in the folderid variable.
- We have now gotten a random folderid from the previous response. All that is left for us to do is now write that id out to a variable which other samplers or JMeter objects can use it. JMeter variables are available in BeanShell via the ‘vars’ object. To the end of the script add:
//write the folderid to the jmeter variables
- Your finished script should look like this:
Remember we write the id value to the variable ‘getFolderId’ because that’s the variable that out GET Files Sampler Library module uses.
- We’ve finished our first BeanShell script! If you don’t have a Debug Sampler after the BeanShell PostProcessor add one now, and then go ahead and run the test! Your testplan should look like this:
- Examining your results you should see that the Debug Sampler Response Data tab lists the folderid variable.
Run a couple times and you should see the id changing as different random choices are made.
Learning how to leverage the power of BeanShell is critical to success with JMeter. It’s just about guaranteed that you will run into a limitation with JMeter, and you’ll need to break out your BeanShell skills to solve the problem.
BeanShell can be used to do many things like:
- Introduce randomness to your tests
- Do more and better validation of Sampler Responses
- Prepare data for a test or sampler to use
- Interact across tests and even across threads
So be sure to brush up those Java skills!