keywords:
Environment
Globals
Tests
Pre-request Script
预定义好各个环境,变量的使用,例如 ,通过切换环境来切换 URL 对应的值;
全局变量定义在 Globals 中,例如 ,不受切换环境影响;
在 Tests 中编写 Script 处理 response 的数据或者执行断言;
在 Pre-request 中编写 Script 使得脚本前置执行;
使用 Runner 自动化或者循环执行;
左下角 Console 可以输出信息。
一些 Tests Script 示例:
1.设置 Globals 中的 Token,给其他请求使用,
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
var data = JSON.parse(responseBody);
pm.globals.set("TOKEN", data.access_token);
2.获取商品数量并进行通知
jsonData = pm.response.json();
console.log(jsonData.data.itemAmount);
if(jsonData.data.itemAmount != 0) {
pm.sendRequest({
url: 'http://127.0.0.1:3001/?title=' + jsonData.data.itemName + '&message=' + jsonData.data.itemAmount,
method: 'GET'
});
}
对应的 Windows 桌面通知脚本,
const http = require('http');
const notifier = require('node-notifier');
const path = require('path');
const iconPath = path.join(__dirname, 'arch.ico');
const hostname = '0.0.0.0';
const port = 3001;
const server = http.createServer((req, res) => {
// 处理GET请求
if (req.method === 'GET') {
const url = new URL(req.url, `http://${req.headers.host}`);
const title = url.searchParams.get('title');
const message = url.searchParams.get('message');
if (title && message) {
// 发送桌面通知
sendDesktopNotification(title, message);
// 发送响应
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Send');
} else {
res.statusCode = 400;
res.setHeader('Content-Type', 'text/plain');
res.end('Fail');
}
} else {
res.statusCode = 404;
res.setHeader('Content-Type', 'text/plain');
res.end('Not Found');
}
});
server.listen(port, hostname, () => {
console.log(`http://${hostname}:${port}/`);
});
function sendDesktopNotification(title, message) {
notifier.notify({
title: title,
message: message,
sound: true,
wait: false,
icon: iconPath
});
}