• info@helpingtesters.com
  • helpingtesters

JMeter Rest API Testing with Example

September 10, 2017 JMeter Tutorial
JMeter Rest API Testing, JMeter Rest Web Service Testing, JMeter Rest Service Example,

In this section, we are going to discuss how to create a test plan in order to JMeter Rest API Testing. REST or Representational State Transfer APIS are most popular.They are being commonly used as they are simple to use. API or Application programming interface is a method or a function that is exposed by an application to other applications. This enables communication between applications. They act as a bridge. API may be public or private. 

There are different methods that can be used with REST API such as Get, Post, Put and Delete. All the methods of an API are easily accessible using these methods. Let us now see how we can add a test plan using JMeter for JMeter Rest API Testing.

→Complete list of JMeter Tutorial Series

Steps to add Test Plan for JMeter Rest API Testing

  • Launch JMeter GUI.
  • Right Click on Test plan > Add > Threads > Thread group
  • Right Click on Thread Group > Add > Logic Controllers > Simple Controller
  • Right Click on Simple Controller > Add > Sampler > HTTP Request
  • Right Click on Thread Group > Add > Listener > View Results Tree

Steps to sign up for Public API

  • Let’s take example of a public API – https://openweathermap.org/api
  • Click on “Subscribe” button for API that you would like to use say “Current Weather Data” API.
  • Sign up on this website to access API using API key.
  • User will be navigated to https://home.openweathermap.org/
  • Click on the tab – “API keys” to access key.
  • API doc will have all the details regarding the different sample URLs to be used.

For Example, Below URL is used to fetch current weather of London. AppID is the API Key that will be unique for each user. URL http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=c796d910805df812590c0c2617088ad4

Steps to create HTTP Request for JMeter Rest API Testing

Once, all the details are captured we can fill up information our HTTP request as below

 

  • Protocol: HTTP
  • Server Namehttp://samples.openweathermap.org
  • Method: GET
  • Path : /data/2.5/weather
  • Parameters :
    • q: London, uk (This can be changed to any other location)
    • AppID: API key as discussed above

The test plan is saved and results are analyzed in the listener.

Response Data

{“coord”:{“lon”:-0.13,”lat”:51.51},”weather”:[{“id”:300,”main”:”Drizzle”,”description”:”light intensity drizzle”,”icon”:”09d”}],”base”:”stations”,”main”:{“temp”:280.32,”pressure”:1012,”humidity”:81,”temp_min”:279.15,”temp_max”:281.15},”visibility”:10000,”wind”:{“speed”:4.1,”deg”:80},”clouds”:{“all”:90},”dt”:1485789600,”sys”:{“type”:1,”id”:5091,”message”:0.0103,”country”:”GB”,”sunrise”:1485762037,”sunset”:1485794875},”id”:2643743,”name”:”London”,”cod”:200}

 

The response data returns expected data i.e. current weather of London. Green color indicates that request has passed successfully. In case the test had failed then it would have been indicated by Red color. We can add some assertions also to this particular request like Response Assertion. In order to add assertion, go to Thread group > Add > Assertions > Response Assertion

Select response to asserted based on response text. In this manner in case the response data does not expect text, the assertion will fail in that case. For example, as per above, the valid response must contain “coord”.

{“coord”:{“lon”:-0.13,”lat”:51.51},”weather”:[{“id”:300,”main”:”Drizzle”,”description”:”light intensity drizzle”,”icon”:”09d”}],”base”:”stations”,”main”:{“temp”:280.32,”pressure”:1012,”humidity”:81,”temp_min”:279.15,”temp_max”:281.15},”visibility”:10000,”wind”:{“speed”:4.1,”deg”:80},”clouds”:{“all”:90},”dt”:1485789600,”sys”:{“type”:1,”id”:5091,”message”:0.0103,”country”:”GB”,”sunrise”:1485762037,”sunset”:1485794875},”id”:2643743,”name”:”London”,”cod”:200}

So, we can add assertion based on that.

In order to check that assertion is working correctly, let’s do the negative test and fail the run. One way to do it is using invalid API. The response will not be returned and the test case will fail as shown below

Ankita has worked on different types of testing like Manual, Automation, Performance testing.  She likes to learn about new tools in market that help to make testing easier and interesting. Testing is her passion due to versatile areas it offers.

About the author

Ankita Dawar author

Ankita has worked on different types of testing like Manual, Automation, Performance testing.  She likes to learn about new tools in market that help to make testing easier and interesting. Testing is her passion due to versatile areas it offers.

Leave a Reply

Your email address will not be published.