d# postman基础教程
1、Tests教学使用
一、常见语法注解
二、变量详解与应用
postman自动化测试 - (Variables)变量的详解与应用
postman常见断言
1、Get an environment variable
pm.environment.get("var_workFlows");
1.取一个预定义的(环境变量):一旦一个变量被设置,你可以使用pm.environment.get("variable_key");
获取该变量=》(字符串、数字)
2、Get a global variable
pm.globals.get("var_workFlows");
1.预定义的(全局变量):可使用上述代码,获取该变量(字符串)
3、Get a variable
pm.variables.get("variable_key");
1.可以获取所有定义的变量
4、Get a collection variable
pm.collectionVariables.get("variable_key");
1.获取集合变量
5、Clear an environment variable
pm.environment.unset("variable_key");
1.清除预定义的环境变量
6、sead a request
pm.sendRequest("https://postman-echo.com/get", function (err, response) {
console.log(response.json());
});
const echoPostrequest = {
url:'url',
method:'POST',
header:'Content-Type:application/json',
body:{
mode:'raw',
raw:{
"json": "json"
}
}
};
pm.sendRequest(echoPostrequest, function (err, response) {
console.log(response.json());
});
7、status code; Code is 200
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
1.断言状态码
8、Response body:Contains string
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
1.断言响应内容是否包含某个字符串
//Cooie获取
var accessToken=JSON.parse(responseBody)["data"]["accessToken"]
var maxAge = 8511031;//失效时间
var sstoken = Math.floor(Date.now()) + "*" + maxAge + "*" + accessToken;
var loginToken = new Buffer(sstoken).toString('base64');
var Cookie='loginCookie="'+loginToken+'"'
pm.environment.get("Cookie");
pm.environment.set("Cookie",Cookie)
console.log("获取cookie成功")
9、Response body:JSON value check
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});
1.断言响应的对应值是否相等(检查json值)
10、Response body:is equal to a string
pm.test("Body is correct", function () {
pm.response.to.have.body("response_body_string");
});
1.检查响应主体是否等于一个字符串
11、Response headers:Content-Type headers check
pm.test("Content-Type is present", function () {
pm.response.to.have.header("Content-Type");
})
1.是否存在响应头的类型
12、Response time is less than 200ms
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});
1.断言响应时间小于200毫秒
13、status code:successful POST request
pm.test("Successful POST request", function () {
pm.expect(pm.response.code).to.be.oneOf([201, 202]);
});
1.成功的请求状态码
14、status code: Code name has string
pm.test("Status code name has string", function () {
pm.response.to.have.status("Created");
});
15、循环遍历结果集
for(i in var_workFlows){
}