postman自动化,测试脚本

Posted chineseliao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了postman自动化,测试脚本相关的知识,希望对你有一定的参考价值。

//获取当前时间的时间戳
function getTimestamp(len=10) {
//  如果需要自动获取则将此处代码放开
//  var tmp = Date.parse( new Date() ).toString();
 // tmp = tmp.substr(0,len);
  //return tmp;
     return ‘1546571642‘
}
//时间戳换算时间
function timestampToTime(timestamp) {
	//时间戳为10位需*1000,时间戳为13位的话不需乘1000
   let date = new Date(timestamp * 1000);
   let Y = date.getFullYear() + ‘-‘;
   let M = (date.getMonth()+1 < 10 ? ‘0‘+(date.getMonth()+1) : date.getMonth()+1) + ‘-‘;
   let D = date.getDate() > 10 ? date.getDate():‘0‘+date.getDate();
   return Y+M+D;
}

// 秘钥ID
let SecretId = ‘XXXXXXXXXXX‘;
// 秘钥key
let SecretKey = ‘XXXXXXXXXXXXX‘;
// 服务器地址
let host = ‘timatrix.tencentcloudapi.com‘;
// 产品名
let service = ‘timatrix‘;
// 时间戳
let RequestTimestamp = getTimestamp(10);
//提交方式
let HTTPRequestMethod = request.method;
// Content-Type数据类型
let contentType = ‘multipart/form-data‘
let CanonicalURI = ‘/‘;
let CanonicalQueryString = ‘‘;
let CanonicalHeaders ;
if(HTTPRequestMethod == "POST" || HTTPRequestMethod == "post"){
//   contentType = ‘multipart/form-data‘
//   CanonicalHeaders = ‘content-type:multipart/form-data
host:‘+host+‘
‘;
//   上面是文件
  contentType = ‘application/json‘
  CanonicalHeaders = ‘content-type:application/json
host:‘+host+‘
‘;
} else if (HTTPRequestMethod == "GET" || HTTPRequestMethod == "get"){
  contentType = ‘application/x-www-form-urlencoded‘
  CanonicalHeaders = ‘content-type:application/x-www-form-urlencoded
host:‘+host+‘
‘;
} else {
  contentType = ‘application/json‘
  CanonicalHeaders = ‘content-type:application/json
host:‘+host+‘
‘;
}
let SignedHeaders = ‘content-type;host‘;
let RequestPayload = ‘‘;
let HashedRequestPayload = CryptoJS.SHA256(encodeURIComponent(RequestPayload));
let CanonicalRequest =
  HTTPRequestMethod + ‘
‘ +
  CanonicalURI + ‘
‘ +
  CanonicalQueryString + ‘
‘ +
  CanonicalHeaders + ‘
‘ +
  SignedHeaders + ‘
‘ +
  HashedRequestPayload;
let Algorithm = ‘TC3-HMAC-SHA256‘;

let CredentialScope = timestampToTime(RequestTimestamp)+‘/‘+service+‘/tc3_request‘;
let HashedCanonicalRequest = CryptoJS.SHA256(CanonicalRequest);
let StringToSign =
  Algorithm + ‘
‘ +
  RequestTimestamp + ‘
‘ +
  CredentialScope + ‘
‘ +
  HashedCanonicalRequest;
let SecretDate = CryptoJS.HmacSHA256(timestampToTime(RequestTimestamp),"TC3" + SecretKey);
let SecretService = CryptoJS.HmacSHA256(service,SecretDate);
let SecretSigning = CryptoJS.HmacSHA256("tc3_request",SecretService);
let Signature = CryptoJS.enc.Hex.stringify(CryptoJS.HmacSHA256(StringToSign,SecretSigning));
Signature = Signature.toLowerCase()
let Authorization =
  Algorithm + ‘ ‘ +
  ‘Credential=‘ + SecretId + ‘/‘ + CredentialScope + ‘, ‘ +
  ‘SignedHeaders=‘ + SignedHeaders + ‘, ‘+
  ‘Signature=‘ + Signature
postman.setEnvironmentVariable(‘host‘, host);
postman.setEnvironmentVariable(‘contentType‘, contentType);
postman.setEnvironmentVariable(‘RequestTimestamp‘, RequestTimestamp);
postman.setEnvironmentVariable(‘Authorization‘, Authorization);

  

以上是关于postman自动化,测试脚本的主要内容,如果未能解决你的问题,请参考以下文章

postman 自动生成 curl 代码片段

postman 自动生成 curl 代码片段

postman+newman+jenkins持续集成接口自动化测试脚本

postman接口测试07_导出测试脚本

自动化脚本测试,postman使用沉淀

postman测试实例--断言