Search This Blog

Breaking

Monday, 24 August 2020

[Karate Framework] Polling Scenario in API

 There are times when we observe that, we are receiving 500 or 400 responses or any other response which was not expected one from our API endpoint.  At that time, we try to hit that API multiple times, and sometimes, do not get that response. Because on re-run, it returns 200 or the desired response. 


To find out the actual root cause, we need to replicate this scenario. For replication, we will have to at least hit this endpoint more than 10, 20 times. At that time, if we will have some automation script which we can utilize to do this activity, becomes very handy.


In one of my projects, We were using Karate for API automation. In Karate, there is a very nice feature of polling (retry-until) for such Use cases. 



To use this feature, we need to first set up the retry configuration as per our need:


* configure retry = { count: 5, interval: 5000 }


So with this configuration, we can hit the request 5 times with an interval of 5 sec.

Now we need to put the condition:

And retry until responseStatus == 500

That's It.

So, with this configuration, the API will be hit maximum 5 times, within an interval of 5 seconds.

If we get the response in 2nd attempt, it will not run further.

A complete scenario is here:

Feature: Test Retry Feature

  Background:

        Given url baseURL

        * configure retry = { count: 5, interval: 5000 }

  Scenario: Fetch all Employees

         Given path 'posts'

         And retry until responseStatus == 500

        When method GET


I liked this feature as sometimes, we just want to hit the endpoint until it gives some different response than expected. Moreover, if your setup is ready then it is very easy to use.


No comments:

Post a Comment