Postman:单接口测试需要知道什么?

代理抓包–APP

在这里插入图片描述
使用同charles等代理工具

Postman客户端捕获Chrome浏览器接口

下载:Postman Interceptor
安装到扩展程序,保证开启
在这里插入图片描述
Postman客户端开启:
在这里插入图片描述
使用抓包代理工具,将请求导入postman
1.在charles右键请求,copy cUrl request
2.保存到文件
3.在Postman 导入操作
在这里插入图片描述

环境管理–开发环境、测试环境、生产环境

变量范围

  • 全局变量:访问集合、请求、测试脚本和环境之间的数据。
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
pm.globals.set("variable_key", "variable_value");
pm.globals.get("variable_key");
pm.globals.unset("variable_key");
  • Collection集合变量:在集合中的整个请求中使用,并且与环境无关,因此不会根据选定的环境进行更改。
    集合…点击Edit进入编辑界面,如下图
    在这里插入图片描述
pm.collectionVariables.set(variableName:String, variableValue:String);//设置集合变量
pm.collectionVariables.get(variableName:String);//获取集合变量
pm.collectionVariables.unset(variableName:String);//清除集合变量
  • Environment 环境变量:允许根据不同的环境定制处理,例如本地开发与测试或生产。 一次只能有一个环境是活动的。
    在这里插入图片描述
    在这里插入图片描述
    将嵌套对象设置为环境变量
var array = [1, 2, 3, 4];
pm.environment.set("array", JSON.stringify(array, null, 2));

var obj = { a: [1, 2, 3, 4], b: { c: 'val' } };
pm.environment.set("obj", JSON.stringify(obj));

获得环境变量

var value = pm.environment.get("variable_key");
//值是一个字符串话的JSON
var array = JSON.parse(pm.environment.get("array"));
var obj = JSON.parse(pm.environment.get("obj"));

清理环境变量

pm.environment.unset("variable_key");

协同合作-共享环境
在这里插入图片描述
可以复制、删除、下载 / 导入环境 JSON,并与 Manage environment 中的协作者共享环境。 共享环境允许其他人对相同的数据集运行您的请求。
要安全地共享环境,首先创建一个副本并删除任何敏感数据,如 auth 值。 当您的合作者导入环境时,他们可以输入自己的凭据。 您对共享环境所做的任何更改都将反映在具有访问权限的整个团队中。

  • Data数据变量:来自外部 CSV 和 JSON 文件,用于定义可以通过 Newman 或 Collection Runner 运行集合时使用的数据集。
  • Local局部变量:本地变量是临时的,只能在请求脚本中访问。 局部变量值的作用域为单个请求或集合运行,并且在运行完成时不再可用。
    同名变量,取相对较小范围的变量
var value = pm.variables.get("variable_key");//跨全局和环境搜索变量

变量传递

//校验状态码200
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});
//将响应json存入data
var data = JSON.parse(responseBody);
//利用jsonpath,提取字段至集合变量“addxjsqId”
pm.collectionVariables.set("addxjsqId", data.data);

相应的:

//也可以提取字段至环境变量或全局变量
pm.environment.set("variable_key", "variable_value");
pm.globals.set("variable_key", "variable_value");

使用变量:

{{addxjsqId}}

Scripts脚本

Postman脚本语言是:js
在这里插入图片描述
单个请求执行顺序
在这里插入图片描述
集合内执行顺序
在这里插入图片描述
集合与文件夹的脚本编写入口:…–>Edit–>pre-request scripts、test
打印日志:

console.log("test");

日志结果框:
在这里插入图片描述
在这里插入图片描述
编写脚本做断言、数据传递、数据逻辑预处理

//验证响应码是否200
pm.test("Name of the second test", function () {
    // make as many assertions as you'd like as part of this test
    // if any assertion throws an error, this test will fail
    pm.response.to.have.status(200);
    pm.response.to.be.ok;
    pm.response.to.be.json;
});

响应是否包含字符串

pm.test("Body matches string", function () {
    pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});

响应体是否等于字段串

pm.test("Body is correct", function () {
    pm.response.to.have.body("response_body_string");
});

响应JSON中的值

pm.test("Your test name", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.value).to.eql(100);
});

header内是否存在Content-Type

pm.test("Content-Type header is present", function () {
    pm.response.to.have.header("Content-Type");
});

响应时间是否小于200ms

pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});

响应status是否是该字符串

pm.test("Status code name has string", function () {
    pm.response.to.have.status("Created");
});

是否是成功的POST请求状态代码

pm.test("Successful POST request", function () {
    pm.expect(pm.response.code).to.be.oneOf([201,202]);
});

验证响应结构https://learning.postman.com/docs/postman/scripts/test-examples/
tv4

var schema = {
 "items": {
 "type": "boolean"
 }
};
var data1 = [true, false];
var data2 = [true, 123];

pm.test('Schema is valid', function() {
  pm.expect(tv4.validate(data1, schema)).to.be.true;
  pm.expect(tv4.validate(data2, schema)).to.be.true;
});

ajv

var Ajv = require('ajv'),
    ajv = new Ajv({logger: console}),
    schema = {
        "properties": {
            "alpha": {
                "type": "boolean"
            }
        }
    };

pm.test('Schema is valid', function() {
    pm.expect(ajv.validate(schema, {alpha: true})).to.be.true;
    pm.expect(ajv.validate(schema, {alpha: 123})).to.be.false;
});

编解码base64

// Assume `base64Content` has a base64 encoded value
var rawContent = base64Content.slice('data:application/octet-stream;base64,'.length);

// CryptoJS is an inbuilt object, documented here: https://www.npmjs.com/package/crypto-js
var intermediate = CryptoJS.enc.Base64.parse(base64content);
pm.test('Contents are valid', function() {
  pm.expect(CryptoJS.enc.Utf8.stringify(intermediate)).to.be.true; // a check for non-emptiness
});

xml 转换成JSON对象

var jsonObject = xml2Json(responseBody);

发送异步请求

pm.sendRequest("https://postman-echo.com/get", function (err, response) {
    console.log(response.json());
});

严格比较与松散比较

//严格
const TEN = 10;
  pm.test('Check if number is equal to 10', function () {
      pm.expect(TEN).to.equal(10);
  });
  //松散
  pm.test("Our JSON is loosely equal to the provided JSON", function () {
   pm.expect(data1).to.deep.equal(data2);
  });

参数化

在这里插入图片描述

待处理问题
3.循环folder相关接口直到完成所有操作
4.上传文件接口待处理

Cookies

有一个Cookie管理器,以域的概念管理Cookie,设置完成,访问该域的接口时,会自动带上相关cookie
在这里插入图片描述
在这里插入图片描述
但是,切换用户之后,全局Cookie不会被替换,手动删除,以新用户的身份登录,得到新的cookie,似乎有点麻烦,有没有办法自动替换呢?
方案一:如果我每次请求登录接口前,执行删除cookie的操作,执行登录操作时再重新生成,看是否可行
方案二:从responseHeader中取出cookie,设置为环境变量

用于cookie编程访问的白名单域
在这里插入图片描述
使用编码方式对cookie做处理–未验证

const cookieJar = pm.cookies.jar();//创建cookieJar 
cookieJar.set(URL, cookie name, cookie value, callback(error, cookie));//将cookie放入cookieJar 
cookieJar.get(URL, cookie name, callback(error, cookie));//获取cookie
cookieJar.getAll(URL, callback(error, cookies));//获取所有的cookie
cookieJar.unset(URL , cookie name, callback (error));//删除cookie
cookieJar.clear(URL, callback (error));//删除所有的cookie

自动获取登录后的cookie并设置环境变量

postman.getResponseCookie('zp').value
pm.environment.set("zp",postman.getResponseCookie('zp').value);

在这里插入图片描述
以下这种方法没办法处理多个set-cookie的情况。

var cookies = postman.getResponseHeader('set-cookie') ;
cookies = cookies.split(";");
pm.environment.set("cookies",cookies[0]);

猜你喜欢

转载自blog.csdn.net/LittleGirl_orBoy/article/details/106077887