postman+newman脚本化接口测试

脚本

脚本采用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

打开开发者工具, 调试, 或者查看console.log的输出

View-->Developer->Show DevTools(current view)

Ctrl+Shift+I

SSL

(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;

npm init 新建工程

新建index.js

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!

posted on 2018-07-21 20:22  reeser  阅读(1132)  评论(0编辑  收藏  举报