首页 > 其他分享 >Postman(六): postman定义公共函数

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

时间:2022-12-09 20:00:13浏览次数:39  
标签:postman actual 定义 min expected randomInt var Math 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变量

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


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

Postman(六): postman定义公共函数_全局变量_02


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

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


4、查看执行结果

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


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

示例:

//获取当前时间戳 毫秒
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,变量值为拼接的函数

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



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

以随机手机号举例:

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

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


Postman(六): postman定义公共函数_字符串_07


2、在tests页签中做断言

Postman(六): postman定义公共函数_全局变量_08


3、查看执行结果

Postman(六): postman定义公共函数_全局变量_09


重点:学习资料

600g的学习资料懂的都懂


Postman(六): postman定义公共函数_字符串_10



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



标签:postman,actual,定义,min,expected,randomInt,var,Math,Postman
From: https://blog.51cto.com/u_15876125/5926416

相关文章

  • 使用IKExpression自定义函数
    背景通过字符串匹配业务表每一条记录的部分字段,来对记录打上标签要求规则可以配置调整规则支持复合运算、逻辑运算开始实现将规则放到数据库中维护从数据库......
  • 自定义jar包供ERP使用
    功能要求:需要在ERP中调用其他web服务或者自身web服务(比如跨账套过账等)1.编写java程序,并将程序打包成jar包importorg.apache.http.HttpEntity;importorg.apache.http.......
  • HTTP/2 中的帧定义
    在HTTP/2的规范中定义了许多帧类型,每个帧类型由唯一的8位类型代码标识。每种帧类型在建立和管理整个连接或单个stream流中起到不同的作用。特定的帧类型的传输可以改......
  • 自定义分页样式
    一.基本安装和配置1.pipinstalldjango-pure-pagination2.settings.py中installapp中添加'pure_pagination',3.settings.py中添加参数PAGINATION_SETTINGS={......
  • WPF TextBox搜索框&自定义TextBox样式
    先看效果图咯:   前面的文章中,button样式告一段落。接下来分享几个TextBox样式。后续持续更新中~代码都在git上同步。有需要的可以下载查看。项目地址在之前的文......
  • Android 动画实现 从基础到自定义
    OverridetheentrypointofanimageIntroducedinGitLabandGitLabRunner9.4.Readmoreaboutthe extendedconfigurationoptions.Beforeexplainingtheav......
  • JavaScript:七大基础数据类型:布尔值boolean、空null、未定义undefined
    布尔值boolean没什么好说的,同其他编程语言一样,就两个值:true和false;空nullJS的null,和Java等编程语言的概念不一样,它不是一个“不存在的对象”的引用,不是一个“null”指......
  • 在项目中定义 i18n 结构化对象的简单思路
    简介本文介绍一种在项目定义i18n文本结构的简单思路,以及定义一些简单的全局函数,用于获取i18n文本。文件结构src├──i18n└──index.js├──zhCN├──i......
  • 调用自定义的包
    创建被调用的包mkdirtestcdtestgomodinitexample.com/testcattest/test.gopackagetestpackagetestimport"fmt"//func定义函数//Hello函数名//......
  • 第二章-线性表 1.线性表的定义和基本操作
    定义具有相关数据类型的n个数据元素的有限序列叫做线性表.术语:位序,表头元素,表尾元素,直接前驱,直接后继.线性表的基本操作基本记忆思路:创建销毁,增删改查.......