Paw调用Javascript示例
2019-10-31 本文已影响0人
OF656
Paw 是一款Mac上原生界面的HTTP客户端测试工具,最让人称道的是强大的Dynamic Values
机制,而且内置了大量的Dynamic Values。一般我们在进行接口测试时,经常会遇到一些需要动态计算的值,比如唯一的订单号、签名、加密计算等,可用它内置的Dynamic Values
来完成。
顺便提一下,内置的Dynamic Values如果是来源于其它请求的Request
或Respone
时,会自动形成执行时的依赖链。在执行有依赖的请求,会先依次执行所有的被依赖请求。这点在测试中也非常的好用。
现实中也存在一些更复杂的场景,动态计算的情况不能用内置的或插件中的Dynamic Values
来完成。这时,还可以先安装JS Script
这个插件,就可以用Javascript调用Paw提供的一些Api接口来定制Dynamic Values。下面举两个示例。
- 定制计算请求体的内容
function evaluate(context){
var request = context.getCurrentRequest();
// A1156797
var uin = getParameterFinallyValueByName(request, '_uin');
var head_str = '<head><uin>' + uin + '</uin><sversion>10000</sversion><time>' + getDateFormatString() + '</time><sign_type>HMAC_SHA512</sign_type></head>';
return head_str ;
}
function getParameterFinallyValueByName(request, paramName) {
var dynamicStringObj = request.getUrlParameterByName(paramName, true);
var component = dynamicStringObj.getComponentAtIndex(0);
return (typeof component == 'string') ? component : component.getEvaluatedString();
};
// 这个版本的Paw定制返回时间串有bug
function getDateFormatString() {
var d = new Date();
var year = d.getMonth() + 1;
var day = d.getDate();
var month = d.getMonth() + 1;
var hour = d.getHours();
var min = d.getMinutes();
var sec = d.getSeconds();
var date_str = d.getFullYear() + '-' + (month < 10 ? '0' + month : month) + '-' +(day < 10 ? '0' + day : day) + ' ' + (hour < 10 ? '0' + hour : hour) + ':' + (min < 10 ? '0' + min : min) + ':' + (sec < 10 ? '0' + sec : sec) ;
return date_str;
}
- 在请中再发起另一个附加请求
function evaluate(context){
var requestUuid = context.getCurrentRequest().id;
var baseUrl = dynamicValue(requestUuid, "_urlBase");
var callback_method = dynamicValue(requestUuid, "_callbackMethod");
var supUid = dynamicValue(requestUuid, "_supuid");
var rawOrderIds = dynamicValue(requestUuid, '_orderidArray');
var totalCount = dynamicValue(requestUuid, '_totalCount');
console.log(rawOrderIds);
if(rawOrderIds == "[]" | totalCount == "0") { return ""; }
var orderIds = rawOrderIds.replace(/[\[\]"]/g, '');
console.log(orderIds);
var arrOrders = orderIds.split(",");
var result_str = "";
for(var i = 0; i < arrOrders.length; ++i) {
var callbackUrl = baseUrl + callback_method + "?limittype=1&outorderid=NT&remark=KARA&card_no=SB&supuid=" + supUid + "&orderid=" + arrOrders[i]
console.log(callbackUrl);
var tmp = execute_callback(callbackUrl);
result_str += arrOrders[i] + ": " + tmp + "\n";
}
///////////////////////////////// notify stub
var notifyUrl = dynamicValue(requestUuid, "_notifyUrl");
var bodystr = "result: [ " + result_str + " ]";
result_str = notify_stub(notefyUrl, bodystr);
return result_str;
}
var dynamicValue = function(requestUuid,param){
return new DynamicValue('com.luckymarmot.RequestURLParamDynamicValue', {
request: requestUuid,
param: param
}).getEvaluatedString();
}
function execute_callback(url){
const http_request = new NetworkHTTPRequest();
http_request.requestUrl = url;
http_request.requestMethod = "GET";
http_request.requestTimeout = 10000;
http_request.setRequestHeader("Content-Type", "text/plain text; charset=utf-8");
// http_request.requestBody = requestBodyStr;
http_request.send();
return http_request.responseBody;
};
function notify_stub(url, bodystr){
const http_request = new NetworkHTTPRequest();
http_request.requestUrl = url;
http_request.requestMethod = "POST";
http_request.requestTimeout = 10000;
http_request.setRequestHeader("Content-Type", "text/plain text; charset=utf-8");
http_request.requestBody = bodystr;
http_request.send();
return http_request.responseBody;
};