Site icon Automation Dojos

How To Do API Testing with JMeter

Table of Contents

Introduction

Application Programming Interface is a very popular term among developers. It is simply a request provider that responds to your request. In other words, it is an intermediate that allows communication between two applications. Google Maps, Twitter, Amazon S3, Weather API are some popular APIs in the industry.

The two widely used Web services are SOAP and API. In this tutorial, you will be learning to use the  REST API with Apache JMeter. Also, you will be able to check their performance with your test parameters.

REST API

Representational State Transfer API is a collection of architectural principles. It has a simple interface that can manipulate and identify the resource easily within the request. We are using Weather API for this tutorial.

Sign up to Weather API in order to get your API key with this link. Your API will be received through email, after the verification of the account. Let’s create a REST API request using JMeter to get the weather data of London, UK with HTTP Request Sampler.

Add HTTP Request Sampler

Step 1: Create a Thread Group.

I have named it as API Testing. Leave the rest of the settings as it is for now.

Step 2: Create an HTTP Request

Create an HTTP Request by following these steps. Right-click on the Thread Group name and click Add > Sampler > HTTP Request.

Step 3: Navigate to ‘OpenWeather’ site

We are using the Current Weather Data API to get current weather data of a certain location. Go to the API page in the Weather API and click “API doc” under the “Current Weather Data” API.

Step 4: Get the API call details.

Get the API call under the “By City Name” Section. Select the first one with the City Name. You can check your city name by searching in the UI search bar of the website. If the name is spelled correctly it will show the weather details as below.

api.openweathermap.org/data/2.5/weather?q={city name}&appid={your api key}

Let’s examine each part of the API call.

“api.openweathermap.org”: IP of the web service.
“/data/2.5/weather”: Path of the web service.
“q”: City Name which you like to get the weather data.London in my case.
“appid”: Your API Key.

Step 5: Fill-in required fields and set ‘Method’.

Complete the required fields in the HTTP Request sampler. Fill the IP and Path with the details above. Select the “Method” as a “Get” Method

Step 6: Fill-in the API Key parameters.

Add City Name and API Key as parameters in the Parameter section. Click the Add button to insert the parameters. Name is the parameter name and value is the parameter value. Check the image below to insert it correctly. I have added “London” to my city.

Run and Validate

Step 7: Add a listner to view response.

You have to add a listener to see the response from the server. Right-click on the thread name and click Add > Listener > View Results Tree to add Results Tree Listener.

Step 8: Run your script.

Run your script by clicking the “Start” button on the menu bar and observe the results in the “View Results Tree Listener”.

Step 9: View the response data.

View received weather data from the “Response Data” section. You can see the weather data in London as requested above.

Step 10: Increase load for re-run.

Increase the load to the API and test the results. Go to Thread Group and Increase the “Number of threads” to 15.

Step11: Clear results and Re-Run new load.

Clear the previous results in the listener by “Clear all” button in the menu bar and re-run the test by “Start” button. You can see the server response to the load that applied. It is responding well. Increase the other thread properties and check the responses as you needed.

Now you can Test an API using Apache JMeter.Use some other web services APIs and test the results. You can check the performance of your API services in that way. Cheers.

Exit mobile version