如何在空手道中添加条件等待响应?
How to add conditional wait for a response in karate?
我们的一个 DELETE 请求花费的时间超过 30 秒。如果花费超过 30 秒,有时测试会失败。我需要添加等待响应,直到某些 GET 调用通过。
我试过下面的代码。
但是我想检查 GET 调用中的某些条件,然后我想断言 DELETE 调用。
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' 仅在 'delete' 调用通过时被调用。
但我只想在 DELETE 调用响应 failed/took 超过 30 秒时调用 'waituntil'
我关注了
但帮助不大
根据你的问题,我认为你正在尝试进行 DELETE 调用(用于删除某些记录),然后进行 GET 调用(用于验证记录是否已删除)。
从你的例子来看:
删除 'MyKarateSoap' 记录并验证 'MyKarateSoap' == null
答案一:
如果您的删除服务需要更多时间来响应,您可以通过将此添加到您的删除调用来本地调整连接和读取超时,
* configure connectTimeout = 30000
* configure readTimeout = 30000
此配置会让空手道等待 30 秒,然后再抛出任何故障。
(仅当您提到的失败是由请求的连接或响应超时引起的)
通过反复试验选择最佳超时(或从 POSTMAN 或您的浏览器中执行相同的操作并获取平均响应时间)
答案2:
您的删除服务可能会按预期响应,但有时系统中可能会出现延迟以反映您的删除,这可能会导致您的 GET 调用失败,如果是这种情况,您可以使用类似下面的逻辑进行池化
* 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 秒汇集一次获取服务,直到超过您输入的时间。
我们的一个 DELETE 请求花费的时间超过 30 秒。如果花费超过 30 秒,有时测试会失败。我需要添加等待响应,直到某些 GET 调用通过。 我试过下面的代码。 但是我想检查 GET 调用中的某些条件,然后我想断言 DELETE 调用。
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' 仅在 'delete' 调用通过时被调用。
但我只想在 DELETE 调用响应 failed/took 超过 30 秒时调用 'waituntil'
我关注了
但帮助不大
根据你的问题,我认为你正在尝试进行 DELETE 调用(用于删除某些记录),然后进行 GET 调用(用于验证记录是否已删除)。
从你的例子来看: 删除 'MyKarateSoap' 记录并验证 'MyKarateSoap' == null
答案一: 如果您的删除服务需要更多时间来响应,您可以通过将此添加到您的删除调用来本地调整连接和读取超时,
* configure connectTimeout = 30000
* configure readTimeout = 30000
此配置会让空手道等待 30 秒,然后再抛出任何故障。 (仅当您提到的失败是由请求的连接或响应超时引起的)
通过反复试验选择最佳超时(或从 POSTMAN 或您的浏览器中执行相同的操作并获取平均响应时间)
答案2:
您的删除服务可能会按预期响应,但有时系统中可能会出现延迟以反映您的删除,这可能会导致您的 GET 调用失败,如果是这种情况,您可以使用类似下面的逻辑进行池化
* 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 秒汇集一次获取服务,直到超过您输入的时间。