项目每次接口测试请求postman需要添加header信息等,非常的繁琐,使用Tests和Prescript联合可以解决。
    登录接口Tests添加脚本

    1. // 打印用于调试
    2. // 把返回的字符串转换成json对象
    3. var jsobj = JSON.parse(responseBody);
    4. const sessionId = jsobj.data.authUser.sessionId;
    5. console.log('登录成功--------------- sessionId:' +sessionId);
    6. // 设置sessionid到环境变量
    7. pm.environment.set("sessionId", sessionId);

    测试接口目录下添加脚本

    1. const sessionId = pm.environment.get("sessionId");
    2. pm.request.headers.add({
    3. key: 'sessionId',
    4. value: sessionId
    5. });
    6. let requestUrl = pm.request.url;
    7. console.log('开始请求api,url: ' + requestUrl)
    8. if(!requestUrl.getPath().startsWith('/services/')){
    9. return;
    10. }
    11. const apiHost = pm.environment.get("api-host");
    12. requestUrl = apiHost + requestUrl.getPath();
    13. const apiToken = pm.environment.get("api-token");
    14. const apiSecret = pm.environment.get("api-secret");
    15. const requestMethod = pm.request.method;
    16. console.log('----------------- 开始获取 API header------------------------');
    17. var nowDate = new Date();
    18. var date = new Date(nowDate.valueOf() - 8 * 60 * 60 * 1000);
    19. var EEE = getEnglishWeek(date.getDay());
    20. var dd = formatNumber(date.getDate());
    21. var MMM = getEnglishMonth(date.getMonth());
    22. var yyyy = date.getFullYear();
    23. var mm = formatNumber(date.getMinutes());
    24. var ss = formatNumber(date.getSeconds());
    25. var zzz = "GMT";
    26. var HH = formatNumber(date.getHours());
    27. var authorizationDate = EEE + ", " + dd + " " + MMM + " " + yyyy + " " + HH + ":" + mm + ":" + ss + " " + zzz;
    28. var orderNo = yyyy + "" + (date.getMonth() + 1) + dd + HH + mm + ss;
    29. var line = '\n';
    30. var signedString = requestMethod + line + authorizationDate + line + requestUrl;
    31. var signSha1 = CryptoJS.HmacSHA1(signedString, apiSecret);
    32. var sign = CryptoJS.enc.Base64.stringify(signSha1);
    33. var authorization = "WallTech " + apiToken + ":" + sign;
    34. console.log('----------------- 获取 API header 成功------------------------');
    35. console.log('Authorization :' + authorization);
    36. console.log('X-WallTech-Date :' + authorizationDate);
    37. pm.request.headers.add({
    38. key: 'Authorization',
    39. value: authorization
    40. });
    41. pm.request.headers.add({
    42. key: 'X-WallTech-Date',
    43. value: authorizationDate
    44. });
    45. function getEnglishWeek(number){
    46. var week;
    47. if(number==0) week="Sun"
    48. if(number==1) week="Mon"
    49. if(number==2) week="Tue"
    50. if(number==3) week="Wed"
    51. if(number==4) week="Thu"
    52. if(number==5) week="Fri"
    53. if(number==6) week="Sat"
    54. return week;
    55. };
    56. function getEnglishMonth(number){
    57. var month;
    58. if(number==0) month="Jan"
    59. if(number==1) month="Feb"
    60. if(number==2) month="Mar"
    61. if(number==3) month="Apr"
    62. if(number==4) month="May"
    63. if(number==5) month="Jun"
    64. if(number==6) month="Jul"
    65. if(number==7) month="Aug"
    66. if(number==8) month="Sep"
    67. if(number==9) month="Oct"
    68. if(number==10) month="Nov"
    69. if(number==11) month="Dec"
    70. return month;
    71. }
    72. function formatNumber(number){
    73. if(number < 10){
    74. return "0" + number + "";
    75. }
    76. return number + "";
    77. }

    这里有个问题,pm.request.url 获取到的路径里面不能带变量参数,获取到的不是最终替换变量的路径,而是之前的,如果用到了需要用正则替换。