apipost uses pre-execution scripts to generate different test data

apipost uses pre-execution scripts to generate random test data

complete process

  1. Write pre-executed scripts using JavaScript
  2. Enter the automated test function interface
  3. Create a new automated test and add an interface to it
  4. Configure execution times and intervals
  5. Finish

a brief introdction

Apipost provides a powerful interface testing function, which is convenient for us to perform various operations of interface testing

Specific steps

1. Write pre-execution scripts using JavaScript

Here are a few simple and commonly used scripts for reference


Randomly generate a date in json format from the current day

function GetUTCDate(i){
    
    
  //i表示时区,如东八区为8,西五区为-5
  // index 表示计算输入失去与UTC相差的毫秒数 
  var index = i*3600000
  var date = new Date()
  var len  = date.getTime()
  //获取从1970年一月一日以来的毫秒数
  var offset = date.getTimezoneOffset()
  //获取UTC与本地时间的偏差毫秒数
  var utcdate = new Date(date.setTime(len+index+offset))
  //用计算出来的毫秒数创建新的时间对象
  return utcdate
}
function RandomTimeZoom(i){
    
    
  var j = Math.floor(Math.random()*1296000000+1296000000)
  var l = GetUTCDate(i).getTime()
  return new Date(l+j)
}//以当前日期为准,随机获取往后15-30天的日期


/*
//测试代码
console.log( GetUTCDate(8))
*/
var my_date = RandomTimeZoom(8);
var my_year = my_date.getFullYear();
//获取年
var my_month = my_date.getMonth() + 1;
//获取月
var my_day = my_date.getDate();
//获取日
function formatTime(i) {
    
    
  return i < 10 ? ('0' + i) : i
}//在个位数前面添加一个0
var today_date = my_year + '-' + formatTime(my_month) + '-' + formatTime(my_day)
//将获得的年月日拼接成年-月-日的格式



//测试代码
console.log(GetUTCDate(8))
console.log(today_date)


Random value - character or number

/*
在两个字符串中随机取值
*/

var group = [
    "在建",
    "完工"
];
let index = Math.floor(Math.random()*2);
var value_1 = group[index];
/*
//js测试代码
console.log(value_1);
console.log(index);
*/

Randomly generate different characters

/*

自动生成生成不同的内容

 */
function randomString(len) {
    
    
  len = len || 32;
  var $chars = '等会问饿哦发布饿哦in噢VB不舒服和飞机欧式大部分是评分表反驳无诶如图签完后日后IQ围殴大家哦给问问普洱';//这边是取值范围
  var maxPos = $chars.length;
  var pwd = '';
  for (i = 0; i < len; i++) {
    
    
    pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
  }
  return pwd;
}
/*

let my_value = Math.floor(Math.random() * 1888 + 1);

*/

var my_value = randomString(30);

/*
//apipost请求
apt.setRequestBody
  ("data",
    `{\n
   \"table_date\":\"2023-03-16\",\n
   \"content\":\"工作动态 ${my_value} \"\n}`);
*/




2. Enter the automated test function interface

insert image description here

3. Create a new automated test and add an interface to it

Add pre-execution scripts to the interface

4. Configure execution times and intervals

insert image description here

5. finish

Create a new automated test and add an interface to it

Add pre-execution scripts to the interface

insert image description here

Guess you like

Origin blog.csdn.net/ekcchina/article/details/130505007