How to implement the retry logic in Karate framework

54 Views Asked by At

I am working on one service where we are using the Karate framework for automation testing for APIs. But some APIs are randomly giving error status codes, after multiple runs it is giving 200 status code.

So, to handle these APIs we wanted to write the retry logic, if we got error status code, it needs to run 3 to 4 times the same API, if still we didn't receive the positive status code means, API needs to fail.

To achieve this we tried multiple ways and written some code, but some or the other is blocking.

Here in the above image in method1 scenario am calling GenSleep scenario where we declared response1, I need to use that response1 in method1 scenario, however it is throwing response1 is not declared error.

And am using Karate 1.2.0.

How can I fix this error? Do I need to make any changes in Retry logic?

I tried by configuring retry in scenario like

configure retry = {count: 3, interval: 3000}

By giving retry until also it is not working, so I went for js function and shared the code also.

Is there is anyway we can implement retry and retry until at a time, actually I need a condition like responseStatus == 200 and it needs to retry for only 3 to 4 times if the limit exceeds it needs to fail.

Code:

Feature:

Scenario Outline: 

call read('Retry.feature@method1')

@abc @ignore
Scenario:

Given url 'abc.com'
And header Content-Type = 'application/json'
And header X-env = 'abc'
And header X-guid = '2345'

When method GET

@method1 @ignore
Scenario:
* def rtg = karate.call('Retry.feature@abc')
* def resStatus = rtg.responseStatus

* if(resStat != 200) karate.call('Retry.feature@GenSleep', {key: 3})

* print response1

@GenSleep @ignore
Scenario:

* def func =
"""
function(){

for (var i=0;i<3;i++){
var response = karate.call('Retry.feature@abc')
if(response.responseStatus == 200)
{
karate.log('input')
break;
}
}
return response
}
"""

* def response1 = func()
0

There are 0 best solutions below