• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Postman(六): postman定义公共函数

武飞扬头像
阿里测试君
帮助1

Postman(11): postman定义公共函数

postman定义公共函数

在postman中,如下面的代码:

1、返回元素是否与预期值一致

var assertEqual=(name,actual,expected)=>{tests[`${name}:实际结果: ${actual} , 期望结果:${expected}`]=actual===expected;};

2、返回元素是否与预期值不一致

var assertNotEqual=(name,actual,expected)=>{tests[`${name}:实际结果: ${actual} , 期望结果:${expected}`]=actual!==expected;};

以上都是自定义的断言函数,每次在使用的时候都需要把代码进行复制粘贴,很不方便,可以使用公共函数来实现。

eval() 函数作用

可以接受一个字符串str作为参数,并把这个参数作为脚本代码来 执行。

举例如下:把代码转成字符串并做一个变量,然后通过eval(变量名)函数转成代码直接调用

//定义s为变量
var s = 'var a = 10; var b = 20;'
eval(s)  //把s变量的值转成代码;即a = 10;b = 20
tests[a b] = true  //输出a   b = 30

//做断言,实际结果和预期结果一致
var common_function = 'var assertEqual=(name,actual,expected)=>{tests[`${name}:实际结果:${actual} , 期望结果:${expected}`]=actual===expected;};'
eval(common_function)
assertEqual("验证title是否正确",title,"猫_百度搜索")

如下操作完成公共函数的编写:

备注:只能使用var定义的函数,不能用const

1、把定义好的函数作为字符串存放在变量中;如下图的common_function变量

学新通

2、把上一个定义的变量设置到全局变量中;

学新通

3、在需要使用函数的地方,使用eval()函数把全局变量中的值转换为代码进行执行 。

学新通

 4、查看执行结果

学新通

通过上面的方式知道可以把函数代码放到全局变量中,那么可以把经常用到的函数代码一起放到全局变量中;

示例:

学新通

//获取当前时间戳 毫秒
var now_time = Date.now()
pm.globals.set("now_time",now_time)

//guid实现
const guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
.replace(/x/g, () => (Math.floor(Math.random() * 16)).toString(16))
.replace(/y/g, () => (Math.floor(Math.random() * 4   8)).toString(16));
pm.globals.set("guid_value",guid)

//随机整数实现
const randomInt = (min, max) => Math.floor(Math.random() * (max - min   1))   
min
pm.globals.set("randomInt_num",randomInt(8,15))

//从多个选项中选择实现
const randomInt = (min, max) => Math.floor(Math.random() * (max - min   1))   
min
const getRandomValue = list => list[randomInt(0, list.length - 1)];
const charsInName = ['王','李','张']
pm.globals.set("people_name",getRandomValue(charsInName))


//随机手机号实现
const randomInt = (min, max) => Math.floor(Math.random() * (max - min   1))   
min
var mobile_num = `18${randomInt(100000000, 999999999)}`;
pm.globals.set("mobile_num",mobile_num)

//同步等待实现:等待5秒后开始执行
const sleep = (milliseconds) => {
const start = Date.now();
while (Date.now() <= start   milliseconds) {}
}
sleep(5000)
学新通

把上方的代码拼到一起如下:

var now_time = Date.now();var guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';var randomInt = (min, max) => Math.floor(Math.random() * (max - min   1))   min;var randomInt = (min, max) => Math.floor(Math.random() * (max - min   1))   min;var getRandomValue = list => list[randomInt(0, list.length - 1)];var mobile_num = `18${randomInt(100000000, 999999999)}`;var sleep = (milliseconds) => {const start = Date.now();while (Date.now() <= start   milliseconds) {}};var assertEqual=(name,actual,expected)=>{tests[`${name}:实际结果:${actual} , 期望结果:${expected}`]=actual===expected;};

在全局变量中定义一个公共函数common_function,变量值为拼接的函数

学新通

 在Pre-request Script和tests页签中调用common_function中的函数

以随机手机号举例:

1、在Pre-request Script页签中调用公共函数common_function,并再设置一个随机手机号的全局变量;如下图

学新通

学新通

 2、在tests页签中做断言

学新通

 3、查看执行结果

学新通

重点:学习资料  

600g的学习资料懂的都懂

学新通

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfjhga
系列文章
更多 icon
同类精品
更多 icon
继续加载