标签:duration gis set 一个 拷贝 读取 脚本 请求 lob
脚本采用ECMAScript语言,最新版本支持ES6语法.
利用脚本可以降低联调工作量.
例如这样一个场景:读取第一个请求响应结果中的数据,构造下一个请求;如果人工拷贝,费眼,费劲,费神;而写成脚本,则省不少事.
全局变量只有一组,环境变量可以有多组;
postman先尝试从环境变量中获取变量,获取不到则尝试从全局变量中获取变量;
使用少的,建议使用全局变量;使用多的,建议使用环境变量;使用newman脚本时,建议使用环境变量;
凡是可以输入数据的地方,都可以使用变量,使用两个花括号将变量包起来.
{{foo}}
pm.environment.unset("foo");
pm.environment.set("foo", "gists");
pm.globals.unset("foo");
pm.globals.set("foo", "gists");
http请求前执行;用于设置参数;
http请求后执行;可以测试请求执行结果;可以解析结果后,设置变量,用于下一个请求;
(截图太麻烦,这是导出的脚本)
{
"info": {
"_postman_id": "1c956670-9c33-40a8-b881-34f0b5c73ca7",
"name": "github",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "GetUserInfo",
"event": [
{
"listen": "prerequest",
"script": {
"id": "9c67def9-8629-4fa1-8902-67098645c8df",
"type": "text/javascript",
"exec": [
"",
"pm.globals.unset(\"foo\");",
"",
"pm.globals.set(\"foo\", \"gists\");",
""
]
}
},
{
"listen": "test",
"script": {
"id": "7c627a4f-dddd-4936-877c-cf7a76107d4e",
"type": "text/javascript",
"exec": [
"pm.test(\"Status code is 200\", function () {",
" pm.response.to.have.status(200);",
" pm.globals.set(\"bar\", pm.response.json()[0].url);",
" ",
" console.log(pm.response.json()); ",
"});",
"",
""
]
}
}
],
"request": {
"method": "GET",
"header": [],
"body": {},
"url": {
"raw": "https://api.github.com/users/octocat/{{foo}}",
"protocol": "https",
"host": [
"api",
"github",
"com"
],
"path": [
"users",
"octocat",
"{{foo}}"
]
}
},
"response": []
}
]
}
View->Show Postman Console
Alt+Ctrl+C
View-->Developer->Show DevTools(current view)
Ctrl+Shift+I
(1) File->Settings->General->SSL certificate verification, 关闭之
(2) File->Settings->Certificates->Add Certificate
配置:
Host: IP地址 端口号(默认443, 取决于你要测哪个端口)
CRT file: 信任证书
KEY file: 个人证书
Passphrase: 个人证书的加密密码
https://github.com/postmanlabs/newman
略
npm install newman --save-dev;
let newman = require("newman"); // require newman in your project
// call newman.run to pass `options` object and wait for callback
newman.run(
{
collection: require("./github.postman_collection.json"),
reporters: "cli"
},
function(err) {
if (err) {
throw err;
}
console.log("collection run complete!");
}
);
执行结果
√ Status code is 200
┌─────────────────────────┬──────────┬──────────┐
│ │ executed │ failed │
├─────────────────────────┼──────────┼──────────┤
│ iterations │ 1 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ requests │ 1 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ test-scripts │ 1 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ prerequest-scripts │ 1 │ 0 │
├─────────────────────────┼──────────┼──────────┤
│ assertions │ 1 │ 0 │
├─────────────────────────┴──────────┴──────────┤
│ total run duration: 4s │
├───────────────────────────────────────────────┤
│ total data received: 13.7KB (approx) │
├───────────────────────────────────────────────┤
│ average response time: 3s │
└───────────────────────────────────────────────┘
collection run complete!
标签:duration gis set 一个 拷贝 读取 脚本 请求 lob
原文地址:https://www.cnblogs.com/reeser/p/9347856.html