The approach of using JMeter for API automation helps in code reusability and flexibility.
Apache JMeter is a widely-used, open-source performance testing tool. It is a standalone application with a user-friendly graphical user interface; the hierarchal structure makes it easy to create the scenarios, and the test scripts are represented in a tree structure.
The major important thing for JMeter is, you don’t need to have good coding skills. The GUI will support you to understand the process and create the test plan. Eventually, you can add a few beans shell scripting for more programming flow if required. The entire JMeter script are saved in JMX format and can be easily managed when an update is needed, and also can be inherited to another JMX file with minor changes.
Although it is a performance testing tool, JMeter can be used to automate APIs. This blog explains the workflow of API automation using JMeter.
The major important thing is for JMeter is that there is no need to have any coding skills.
Mostly we test APIs manually using tools such as Postman, Swagger but in JMeter, we can automate step-by-step interlinking with other APIs
Advantages of JMeter API:
- Open source
- Quick API testing
- Multi programming, flexible language flexible
- Best reporting
Disadvantages:
- Consumes memory when user count increases, i.e., >10,000
- Acquires huge system resource
Instructions to install and run the JMeter script
- Download JMeter from http://jmeter.apache.org/download_jmeter.cgi (Preferred >= ver5.0), Extract the Jar and locate the ApacheJmeter.exe file to run the JMeter
- Install required plug-ins like JSON plug-in, perfmon etc.
Setup
- After installation, create a new test plan by clicking on File -> New
- Then right-click on the Test Plan and create a new Thread group. Thread group shows how many threads (no of users) need to run at a time (concurrent scenario). The ramp-up period is how the users must get added in the meantime
Note: we can add multiple thread groups to achieve more real-time scenarios where multiple APIs will be called by a number of users.
All the operations will be done in the Thread group, and we can add multiple elements for future regression purposes.
User-defined, variable (represents as a parameterization to avoid code redundancy) once after specifying all the variables, we can use the variables all over the API by calling with Names as mentioned below
In addition to using CSV files to parameterize data, JMeter also allows you to call all the variables in the CSV file during policy creation so that they are available throughout the runtime. As seen in the following screenshot, JMeter will access every variable that was parameterized while it runs.
Note: The policies.csv file must be placed where the JMX file is located. Keep the data set (list of CSV files and JMX in one folder)
Creating the first API call
Here we will create an API call to login to a particular application which retrieves Token ID or SID as an output.
After executing the above call, it retrieves the Token/SID, which will be accessible for the remaining API calls to validate the session and authorization.
JSON Extractor
JSON Extractor works as a web service exchange format where it will pass the output of one call to input for another call.
We can achieve this using a regular expression extractor, but it will be quite difficult while having a lot of JSON elements to extract within the same call and having the same attribute names.
For JSON extractors, there are alternatives like BSF or JSR223 Postprocessors that translate the result into JavaScript, and it will impact the performance and require a lot of Beanshell code to be written.
In the above picture, after creating the device policy, it will extract variables in JSON format.
- $ Refers to the main(root) element in JSON expression
- ‘id’ is the immediate sub of the root element. We can use it as “$.id”
- Match number shows which element should be matched in the sub child.
- Default value: Message when there is no value matched with the above specification
Result in JMeter
In JMeter, there are a different number of report types that can display the results of our automation tests; since we are running API scripts, we can show it up on Result Trees, where it will display the status of each API call.
At first, we need to add our required result reports, which are available in the JMeter and then click on the run button available in the GUI or press “Ctrl+r.”
In the picture below, it shows the aggregate report by summing up all the API calls, and also there are multiple result trees such as “Graph Results”, “View Result Tree,” Etc.
Running JMeter from Command Line:
Mostly we run the API script from GUI, but it is not recommended because it will make the script run slower. Instead, we have the feasibility to run through the command line by using the command below.
“Ex: jmeter -n -t C:\Users\jmxfile.jmx -l C:\Users\ result.csv -e -o C:\Users\report”
jmeter -n -t (location of your jmeter test script)
-o [Path to output folder]
-n = non GUI mode
-t = location of JMeter script
-l = location of the result file
-e = generate report in HTML format
-o = shows the output of the HTML Report folder
NOTE: Make sure an empty folder is created using same path
After running the above command, it will generate the HTML report with good GUI graphs and results and shows major errors in API calls and their description.
Conclusion:
JMeter combined with Rest API has good advantages, but it depends on project to project; it will be good enough for test automation since it is robust, has nice reporting features, and can be easily integrated with multiple frameworks and environments.
Reference Links:
https://octoperf.com/blog/2018/04/23/jmeter-rest-api-testing/
for JMeter Installation: https://www.blazemeter.com/blog/how-get-started-jmeter-installation-test-plans