Functional Testing 08: Using Response Data – Part 2

Eighth in a series focusing on using JMeter for functional testing of an API.

In the last tutorial we used the response from Signin in to make another call to the API. Now we’ll take it one step further. We’ll introduce a little bit of validation to our Folders Sampler Library module in order to be able to make one more call to get the list of files within a folder. We’ll also introduce a little bit of randomness to our test, in a way that we will improve on in the next tutorial when things really start to get fun.

This tutorial took me a little while long to get written. My plan was to do a simpler version of what we will do in the next tutorial when we introduce writing some Java to help our test. However, one of the many thing JMeter is good at is messing up your best laid plans. So while this tutorial isn’t quite what I hoped it would be, it is still a good step in our journey, and what you learn here will help you understand the next tutorial a lot more easily.

Before we begin make sure you have the test plan from the previous tutorial or download this one to use.

You should also be familiar with what query strings and parameters and how they are used. You can read more here. We will use query parameters to pass information to the server in this tutorial.

Functional Testing 08: Using Response Data – Part 2

First let’s add some validation to the Get Folders Sampler Library module. We will want to make sure that the number of folders matches the count that we expect.

  1. Open the Test Plan and expand Sampler Library > Get Folders.
  2. Right-click the GET Folders sampler and select Add > Assertions > JSON Assertions. You may remember this handy assertion from Functional Testing 04.
  3. In the JSON Assertion…
    Set Assert JSON Path exists to “$.total
    Check Additionally assert value
    Set Expected value to “${expectedCount}

    Your test plan should look like this. Now let’s remember to add a little documentation of the expectedCount variable so that when we come back use or fix this module in the future we will have an easier time of remembering what we did.
  4. Right-click on Get Folders Simple Controller and select Add > Listener > Beanshell Listener. Right-click on the listener and select Disable, then drag it above the HTTP Header Manager.
  5. Add the following text to the Listener, similar to what we did for the Signin module.
  6. Now we need to go back and update our Folders test to provide the expectedCount value. Expand Folder Tests > Get Folders.
  7. Right-click on the Get Folders Transaction Controller and select Add > Logic Controller > jp@gc – Parameterized Controller.
  8. Drag the Get Folders Module Controller into the Parameterized Controller.
  9. Click the Paramterized controller and add a row to the Parameterized Controller and …
    Set name to “expectedCount
    Set value to “2

    You may ask “Why do we set it to 2, when we know there are 3 folders?!” The answer is that I like to test my changes as I go. So first I’ll set it to a value which I know is wrong and run the test. If it fails, that’s good. Then I will set it to the correct value, and if it passes I feel doubly good that the change is working. If we were to simply set the value to our known good value and move on, we would always have some uncertainty that any failure would be caught.
  10. Let’s run our tests.

    Hooray it failed! How often do you hear that?!
  11. Now set expectedCount to “3” and run again.

    Hooray it passed! Well, at least the check for the total property in the response passed.There could still be a problem with the size of the actual list of folders that we wouldn’t catch (for instance the list could actually only have two folders in it instead of three). We will look at how we can validate that in a future tutorial.

But Great! We can now partially validate that the correct number of folders was returned.

Now we will create a new Sampler Library module for Files and then use it with information from the response from GET Folders.

  1. Right-click Sampler Library and select Add > Logic Controller > Simple Controller. Rename it to “Get Files“. Now using what you’ve learned from creating the other two Sampler Library modules, create a sampler that based on this information…
    – The url for the Files API is: ${API_ROOT}files
    – The API requires an authorization and Content-Type header set to application/json
    Check your work here.
  2. For this call we will need to pass some additional information to the API. We will need to pass (at a minimum) the folder id from which we want to get the list of files. This API takes the folderid as a query parameter. So to the end of the API url, we need to add data after a question mark; something like “…/files?folderid=3”.
  3. Select the Get Files Sampler.
  4. On the Parameters tab on the bottom half of the Sampler, add a new row by clicking the Add button at the top bottom of the tab.
    Set Name to “folderid
    Set Value to “${getFolderId}
    Check Encode?
    Make sure Include Equals? is checked as well

    What we have done is configured the HTTP sampler to add query parameters on to the end of the API call and to do so in a way that we can use it for many different folderids. The GET Files Sampler will construct a url like:
    /learnjmeter/api/files?folderid=3

If you remember the response data from the GET Folders call, it looked like this:

Each folder has an id, and it is this id we must pass to the GET Files Sampler in order to retrieve the gif files in that folder.

That completes the setup for now on our new GET Files Sampler. But what we have is two unconnected Samplers. GET Folders retrieves folder information including “id” and GET Files needs the folder id, but we have no connection, no way of getting the id from GET Folders to the GET Files call. TO connect the two, we’ll configure the test to randomly choose one of the folders and then get the files in that randomly selected folder.

  1. Copy and paste the Folders Tests Thread Group and rename it to Files Tests. The rename the Transaction Controller beneath it as well to Get Files. I know, I did it again. There is another way of doing this so that we are not copying the thread, and we will get into that in a tutorial about Workflows. But for now, it’s easiest to simply copy and paste to re-use our existing Get Folders work.
    Let’s also disable the Folder Tests Thread Group.
  2. Expand Get Files and then the Parameterized Controller.
  3. Right-click the Parameterized Controller and select Add > Config Element > Random Variable.

    We will use this Random Variable config element to select a random number between 0 and 2 (inclusive) and use that random number as an index in the folders array.
  4. In the Random Variable element…
    In the Output Variable section, set  Variable Name to “folderIndex
    In the Configure the Random generator section, set Minimum to 0 and Maximum to 2

    The Random Variable element will now randomly select either 0, 1, or 2, and store selection in the folderIndex variable. Next we will need to retrieve the folder id for one of the folders using the index into the folders array. Remember array indexes start at zero, so the first entry in an array is index zero (or [0]) and not one. (See note about how the Random Variable Config Element works below).
  5. Now back in our GET Folders Sampler Library Module, right-click on the GET Folders HTTP Sampler and select Add > Post Processors > JSON Extractor. You will remember we used this element in our Signin Library module to extract the token from the Signin response. We are going to use it this time to extract a folder id from the GET Folders response.
  6. In the JSON Extractor…
    Set Names of Created Variables to “getFolderId
    Set JSON Path Expressions to “$.folders[${folderIndex}].id

    Let’s breakdown the JSN Path Expressions.
    The $ means we start at the root of the GET Folders response.
    .folders means we want to refer to the array of folder information
    [] means we are going to select one of the folders in the array by index number (either 0, 1, or 2), and that index will be in the getFolderIndex variable
    .id means we want to extra the id value from the folder we choose.
    That value will be stored in the getFolderID variable that the GET Files Sampler needs.
  7. Now we’ve done a lot of work, its probably a good time to stop and do some testing to make sure things are working how we expect. So lets add a Debug Sampler by right-clicking on the Parameterized Controller and selecting Add > Sampler > Debug Sampler and then run our tests. Your test plan should look like this…

    and your results like this…

    Clicking on the Debug Sampler and selecting the Response data tab, you should see the folderIndex variable with a value of 0, 1, or 2 and the getFolderId variable with the id of the corresponding folder in the array.

    In this case, the random index was 2 and the corresponding folder id was 35.
    Run the test a couple times and verify you are seeing results that vary by the folder which is randomly selected.
  8. Now that we know everything is working correctly all we need to do is add the new Get Files Sampler Library module to our test!
  9. Right-click the Get Files Transaction Controller and select Add > Logic Controller > Module Controller. Rename it to GET Files.
  10. In the Module Controller select the Get Files Sampler Library module.
  11. Now run your tests!
    You should see a successful call to GET Files where the url includes a folderid.
    In this case, folderid=3 was chosen.  And the response contained a long list of gifs.

Putting the response data into Online JSON Viewer helps us better see the results.
We can see we get the total number of gifs in the folder, the pagesize of the list of files, and the offset  of the first item in the list. What’s with pagesize and offset?
Run the test a couple more times and see that you are getting different folders chosen each time.

The downsides of what we did in this tutorial:

In this tutorial we built the random selection of one id from the list of folders, into our GET Folders Sampler Library module. In real life this is probably not something you would want to do. If we left our Sampler Library the way it is, every time we call GET Folders we would need to provide an index into the results folder array. It’s a little too much for something we may only need for a subset of tests.

We will fix this in the next tutorial.

A note about how Random Variable Config Element works:

JMeter has a number of elements which are processed only once and at the very beginning of the test run. If you remember early on we discussed how User Defined Variable elements are processed all at once at the start of test. This meant that even if you included multiple User Defined Variable elements, and had the same variable with two different values, the last value found for that variable would be used for the entire test run.

Random Variable elements work the same way. SO so even though we included the Random Variable element in the GET Files test, it is actually only processed once t the start of the test. This means that even if we were to loop over the Random Variable many times, it would always have the same value. It also means, that if we try to use a variable for either the minimum or the maximum we will almost always get zero (0) as the random result. Since the Random Variable is processed at the start of the test, before our variables have any values, the random minimum and maximum would always be 0.


Downloads

Tutorial Test Plan

Leave a Reply

Your email address will not be published. Required fields are marked *