0

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

Anupama
  • 391
  • 4
  • 16
  • Hi Peter, with the link you provided i was not able to resolve issue. Kindly provide some more details regarding conditional wait. My scenario is ' If DELETE call fails i don't wants fail the test case. I wants to wait until my GET call returns true' – Anupama Oct 02 '18 at 01:04
  • I accepted my previous question now. Can you please provide resolution for this question. – Anupama Oct 02 '18 at 01:10
  • I will try to explain more. line 10 When method delete, line 11 * call waitUntil 200.. Here line number 11 executes only when the line number 10 passes. My question to you is, I wants to call line number 11 if line number 10 fails also. Hope I cleared you doubt. – Anupama Oct 02 '18 at 01:23
  • I tried all my best to understand the conditional wait in the doc. But I am not clear Peter. – Anupama Oct 02 '18 at 01:57
  • I am to resolve issue by adding karate.configure('readTimeout', 60000); in karate.config file. But for conditional wait ,still I have doubt and I will see if anyone helps here. Thank You Peter – Anupama Oct 02 '18 at 02:11
  • for conditional logic, you are basically asking a JS question not a karate question. please take the help of someone who knows how to code. – Peter Thomas Oct 02 '18 at 02:12

1 Answers1

4

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.

Babu Sekaran
  • 4,129
  • 1
  • 9
  • 20
  • 2
    Hi Babu, Answer1 is what exactly I wanted. Answer1 is working perfectly fine and you understood problem space correctly. Thank you for accurate reply :) – Anupama Oct 05 '18 at 08:16
  • 1
    @Anupama if you had pasted the error message in the first place, others would have "understood problem space" :P glad you got the answer thanks to Babu Sekaran - so I can be less active over here – Peter Thomas Oct 08 '18 at 05:10
  • 1
    Even I was little skeptical about the question that's why 2 answers :P, @PeterThomas I got this covered ;) – Babu Sekaran Oct 08 '18 at 16:00