How to add conditional wait for a response in karate?

为君一笑 提交于 2019-12-24 21:52:01

问题


For one of our DELETE request time taken is more than 30 sec. Sometimes tests fails if it takes more than 30 sec. I need to add wait for response until certain GET call passes. I tried below code. But I wants to check some condition in GET call then I wants to assert for DELETE call.

 Feature:       

Background:
* def waitUntil = 
"""
function(x) {
  while (true) {

    var result = karate.call('classpath:ic/feature/soap/Common/getApplicationsList.feature');
    var res = result.response;
    karate.log('poll response in side java script', res.integration.serviceData.applicationsList.WmSOAPProvider['MyKarateSoap']);

    karate.log('Actual responseis in jacva script ---> ', res.integration.serviceData.applicationsList.WmSOAPProvider)
    var local = res.integration.serviceData.applicationsList.WmSOAPProvider['MyKarateSoap'];
    karate.log('local value is--->' +local)

    karate.log('res is ----->' +res)
    if (res.integration.serviceData.applicationsList.WmSOAPProvider['MyKarateSoap'] == null) {
      karate.log('condition satisfied, exiting');
      return;
    }
    karate.log('sleeping in else block');
    // uncomment / modify the sleep time as per your wish
    java.lang.Thread.sleep(3000);
  }
}
"""

Scenario: delete soap application

Given url appServer
And path '/integration/rest/application/WmSOAPProvider/' +'MyKarateSoap'

And header Accept = 'application/json'
And header Content-Type = 'application/json'
And header X-CSRF-TOKEN = lresult.response.csrfToken
* cookie JSESSIONID = lresult.responseCookies.JSESSIONID.value
* cookie route = lresult.responseCookies.route.value

When method delete

* call waitUntil 200

In the above code 'waitUntil' is called only when the 'delete' call is passed.

But I wants to call 'waituntil' only when the DELETE call response is failed/took more than 30 sec

I followed How to retry a request until i get a valid dynamically generated value in response using karate dsl

But not much helpful


回答1:


From your question, I believe that you are trying to make a DELETE call (for deleting some record) followed by a GET call (to validate that record is deleted).

From your example : Deleting 'MyKarateSoap' record and validating 'MyKarateSoap' == null

Answer 1: If your delete service is taking more time to respond you can locally adjust the connect and read timeout by adding this to your delete call,

* configure connectTimeout = 30000    
* configure readTimeout = 30000

this configuration would let karate wait for 30 secs before throwing any failure. (Only if the failure you mentioned was caused by connection or response timeout from the request)

choose an optimal timeout by trial and error (or Do the same from POSTMAN or your browser and take the average response time)

Answer 2:

Your delete service might respond as expected but sometimes there may be a latency in the system for refecting your deletion which may cause failure in your GET call if that is the case you can use a logic something like below for pooling

* def waitUntil =
"""
function(waitTime) {
    var poolTime = 5;
    var counter = 1;
    // should pool for every 5 seconds until it exceeds your input wait time
    while (true) {
        if( (counter*poolTime) > waitTime){
            karate.log('condition not satisfied for a long time, exiting');
            return false;
        }
        var result = karate.call('getApplicationsList.feature');
        var WmSOAPProvider = result.response.integration.serviceData.applicationsList.WmSOAPProvider
        if (WmSOAPProvider['MyKarateSoap']) {
            karate.log('condition satisfied, exiting');
            return true;
        }
        // pool every 5 seconds
        java.lang.Thread.sleep(poolTime*1000);
        counter++;
    }
};
"""
* def result = waitUntil(30)
* def assert result == true

This should pool your get service for every 5 seconds until it exceeds your input time.



来源:https://stackoverflow.com/questions/52596736/how-to-add-conditional-wait-for-a-response-in-karate

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!