如何在空手道中添加条件等待响应? [英] How to add conditional wait for a response in karate?

查看:96
本文介绍了如何在空手道中添加条件等待响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我们的一个DELETE请求,所花费的时间超过30秒.有时,如果测试耗时超过30秒,则测试将失败.我需要添加等待响应,直到某些GET调用通过. 我尝试了下面的代码. 但是我想检查GET调用中的某些条件,然后想断言DELETE调用.

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

在上面的代码中,仅当传递删除"调用时才调用"waitUntil".

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

但是我只想在DELETE呼叫响应失败/花费30秒以上时才调用"waituntil"

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

我关注了 但没有太大帮助

推荐答案

从您的问题来看,我认为您正在尝试先执行DELETE调用(用于删除某些记录),然后再执行GET调用(以验证记录是否已删除) ).

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).

根据您的示例: 删除"MyKarateSoap"记录并验证"MyKarateSoap" == null

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

答案1: 如果您的删除服务需要更多时间来响应,您可以通过将连接超时和读取超时添加到您的删除呼叫中来进行本地调整,

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

此配置将使空手道等待30秒,然后再引发任何故障. (仅当您提到的失败是由请求的连接或响应超时引起的)

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)

通过反复试验选择最佳超时时间(或通过POSTMAN或浏览器执行相同的操作,并获取平均响应时间)

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

答案2:

您的删除服务可能会按预期方式响应,但有时系统中可能存在重新完成删除操作的延迟,如果您可以使用以下类似的逻辑进行池化,则可能导致GET调用失败

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

这应该每隔5秒将您的get服务汇总一次,直到超过您的输入时间为止.

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

这篇关于如何在空手道中添加条件等待响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆