请配合Chrome的F12食用
const moment = require('moment')
let date = moment().format("YYYY-MM-DD HH:mm:ss")
pm.environment.set("localTime", date)
pm.expect(pm.response.text()).to.not.include("哈哈哈");
Status code:Code is 200
可快速生成;{
"code": 200,
"msg": "新建成功!"
}
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
Response body:JSON value check
可快速生成;jsonData.
后;{
"total": 0,
"rows": ["哈哈哈","啦啦啦"],
"code": 200,
"msg": "查询成功"
}
pm.test("查询成功", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.msg).to.eql("查询成功");
});
Response body:Contains string
可快速生成;{
"code": 200,
"data": {
"total": 2,
"list": [
{
"id": "18",
"code": "1",
"name": "测试1"
},
{
"id": "16",
"code": "2",
"name": "哈哈哈"
}
],
"pageNum": 1
}
}
pm.test("包含【哈哈哈】", function () {
pm.expect(pm.response.text()).to.include("哈哈哈");
});
!
非会将值转换为布尔类型,即true/false,第二次!
会返回该值本来的布尔值;Response body:JSON value check
可快速生成;{
"code": 200,
"data": {
"total": 3,
"list": [
{
"id": "148",
"name": "测试1"
},
{
"id": "18",
"name": "哈哈哈"
},
{
"id": "16",
"name": "啦啦啦"
}
],
"pageNum": 1,
"pageSize": 10,
}
}
pm.test("id非空", function(){
//将响应体转换为json,保存为jsonData
var jsonData = pm.response.json();
//获取第三条数据的id,存至自定义变量id
var id = jsonData.data.list[2].id;
//校验id有值
pm.expect(!!(id)).not.to.eql(false);
// 将id打印到控制台,方便调试查看
console.log(id);
});
{{id}}
//将响应体转化为json,存入自定义变量body
var body = pm.response.json();
//提取所需的值,如code传入自定义变量code
var code = body.code;
//将code传入环境变量
pm.environment.set("code",code);
Get a global variable
可快速生成pm.environment.get("环境变量名")
{
"total": 1,
"rows": [
{
"Id": 12,
"noteName": "啦啦啦"
}
],
"code": 200,
"msg": "查询成功"
}
//返回体转化为json
var body = pm.response.json();
//F12右键复制响应体中提取字段路径,赋值给自定义的变量noteId
var noteId = body.rows[0].noteId;
//将noteId赋值给环境变量noteId,无则新建环境变量后赋值
pm.environment.set("noteId", noteId);
Ctrl+F
替换参数;noteId
替换为想提取的参数B;data.list
为想要提取的json路径,如:rows
;啦啦啦
为参数A。{
"code": 200,
"data": {
"total": 4,
"list": [
{
"id": "1",
"code": "1",
"name": "哈哈哈"
},
{
"id": "2",
"code": "2",
"name": "哇哇哇"
},
{
"id": "3",
"code": "3",
"name": "啦啦啦"
},
{
"id": "4",
"code": "4",
"name": "呃呃呃"
}
],
}
}
//响应体转化为json,保存至变量body
var body = pm.response.json();
//定义流程id为noteId
var noteId = "";
//采用for循环,遍历list,如果name为啦啦啦,将此数据的id赋值给noteId后终止
for(var i=0; i<body.data.list.length; i++){
if(body.data.list[i].name == "啦啦啦"){
noteId = body.data.list[i].id;
//将获取的noteId打印到Console中,便于调试
console.log(noteId);
break;
}
}
//将noteId传入环境变量
pm.environment.set("noteId", noteId);
**Clear an environment variable**
快速生成pm.environment.unset("id");
function cleanup(){
// 修改clean的列表,将要删除的变量名加进去
const clean = ['sId','sName','cName']
for(let i = 0; i < clean.length; ++i){
pm.environment.unset (clean[i])
}
}
cleanup();
{{pageNum}}
引用csv文件中的值,参数名与CSV文件中的字段名保持一致。{{id}}
时,因为上游未执行,传入了空值,但返回结果仍为200删除成功;