接口项目
什么是HTTP请求:是指从客户端到服务端的请求消息,由请求行,请求头,请求体组成
网页按F12,可查看接口信息
请求类型:
get:向指定资源发出请求
post:向指定资源提交数据进行处理请求
put:向指定资源位置上次其最新的内容
delete:请求服务器删除request-url所标识的资源
track:回显服务器收到的请求,主要用于测试或诊断
head:向服务器索要与get请求相一致的响应,只不过响应体将不会被返回
options:返回服务器针对特定资源所支持的http请求方法
connect:http/1.1协议中预留给能够将连接改为管道方式的代理服务器
http响应:响应行,响应头,响应体
常见状态码,100-199,200-299,300-399,400-499,500-599
接口就是前后端间的联通
接口测试:
拿到接口的url,
查看接口请求方式,
添加请求头请求体,
发送后查看返回结果并验证返回结果是否正确
下载postman:https://www.postman.com/downloads/
学习postman:https://learning.postman.com/docs/getting-started/introduction/
快捷键:
Control + SHIFT + F.Find and replace
Control + T Tabs
CTRL +
get参数 ?id=1&type=new :id
variables {{my_variable}}
变量范围:Global/Collection/Environment/Data/Local
对应写法:pm.globals/edit Collection/pm.environment/data file/pm.variables
Defining variables in scripts:
pm.globals.set("variable_key", "variable_value");
pm.collectionVariables.set("variable_key", "variable_value");
pm.environment.set("variable_key", "variable_value");
pm.iterationData.get("variable_name").
pm.variables.set("variable_key", "variable_value");
pm.environment.unset("variable_key");
console.log(pm.variables.get("my_variable"))
{{$guid}} : A v4 style guid
{{$timestamp}}: The current timestamp (Unix timestamp in seconds)
{{$randomInt}}: A random integer between 0 and 1000
Visualizing response data
pm.visualizer.set()
Creating a cookie jar
// create a cookie jar
const cookieJar = pm.cookies.jar();
// create a PostmanCookie
cookieJar.set(URL, { name: cookie name, value: cookie value, httpOnly: true }, callback (error, cookie));
Capturing HTTP requests
Scripting in Postman
pm.test("Status code is 200",function(){
pm.response.to.have.status(200);
})
pm.test("Status code is 200", () => {
pm.expect(pm.response.code).to.eql(200);
});
snippet语法
pm.expect(pm.environment.get("env")).to.equal("production");
pm.response.to.be.ok;
pm.response.to.be.withBody;
pm.response.to.be.json;
pm.response.to.not.be.error;
pm.response.to.have.jsonBody("");
pm.response.to.not.have.jsonBody("error");
pm.expect(pm.response.text()).to.include("customer_id");
pm.response.to.have.body("whole-body-text");
pm.expect(pm.response.code).to.be.oneOf([201,202]);
pm.expect(pm.response.headers.get('Content-Type')).to.eql('application/json');
pm.expect(pm.cookies.has('JSESSIONID')).to.be.true;
pm.expect(pm.response.responseTime).to.be.below(200);
pm.expect(pm.response.json().name).to.eql(pm.environment.get("name"));
const jsonData = pm.response.json();
pm.test("Test data type of the response", () => {
pm.expect(jsonData).to.be.an("object");
pm.expect(jsonData.name).to.be.a("string");
pm.expect(jsonData.age).to.be.a("number");
pm.expect(jsonData.hobbies).to.be.an("array");
pm.expect(jsonData.website).to.be.undefined;
pm.expect(jsonData.email).to.be.null;
});
pm.test("Object is contained", () => {
const expectedObject = {
"created": true,
"errors": []
};
pm.expect(pm.response.json()).to.deep.include(expectedObject);
});
Parsing response body data
const responseJson = pm.response.json();
const responseJson = xml2Json(pm.response.text());
const parse = require('csv-parse/lib/sync');
const responseJson = parse(pm.response.text());
const $ = cheerio.load(pm.response.text());
console.log($.html());
Validating response structure
Automating collection runs
使用link运行
https://documenter.getpostman.com/view/12791454/TVKD2xBJ
https://explore.postman.com/DCZpiLvcpaS98CC
使用命令运行
安装npm install -g newman
$ newman run mycollection.json
$ newman run -h
Scheduling runs with monitors
Building request workflows
postman.setNextRequest("request_name");
待续...