JSON Path Usage for Gatling Tests
Learn more about how to extract data with jsonpath.
Join the DZone community and get the full member experience.
Join For FreeAs Loadium gives support to the Gatling tool, we thought it would be great to give some tips about Gatling’s data extraction strategies. The purpose of a Gatling test is not different than Apache JMeter test, and that is performing a load test on your application. For a realistic performance test, you need to use dynamic data. To overcome this challenge, you need to extract data from a JSON, XML, or HTML file. Let’s take a look at jsonpath
expression usage with some examples.
How to Extract Data With JSONPATH
We are going to use JSON Placeholder web services for JSON Path example.
At first, we need to make an HTTP request to an endpoint to receive a JSON response. For that, we need to create a request to “/todos” endpoint and receive a response body as below.
object Task {
def get_task = exec(http("Get all task ids")
.get("/todos")
}
We create an exec object that makes a get request to /todos. You can change the value in the HTTP object to change its name shown in the reports. When we run the test, our request will receive a response body like below:
[
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
},
{
"userId": 1,
"id": 2,
"title": "quis ut nam facilis et officia qui",
"completed": false
}
….
]
Secondly, we need to extract the ids, so we send a specific HTTP request to “todos/id.” So, we change our HTTP request object as below to save all values extracted from JSON.
object Task {
def get_task = exec(http("Get all task id")
.get("/todos")
.check(jsonPath("$[*].id").findAll.saveAs("taskIds")))
}
By using the check function, we are able to validate the response body. Then, we call the jsonPath
method and finally save all id values in the taskIds
variable. So, we use it in another request.
Then, we loop through all the values that we got from the first request by using for each function. In order to call a variable in your source code, you need to use the ${variableName}
structure.
object Task {
def get_task = exec(http("Get aall task id")
.get("/todos")
.check(jsonPath("$[*].id").findAll.saveAs("taskIds")))
.foreach("${taskIds}", "taskId") {
exec(http("Get task info")
.get("/todos/${taskId}")
)}
}
Now, you are ready to execute your parametrized Gatling test with as many virtual users as you want. Just upload your script on Loadium and monitor the results.
Go loadium.com and execute your Gatling tests
Published at DZone with permission of Canberk Akduygu. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments