美文网首页PostMan
Postman接口自动化

Postman接口自动化

作者: 醒醒Coco | 来源:发表于2017-03-14 17:42 被阅读0次

Postman官方blog
https://www.getpostman.com/docs/pre_request_scripts

发送请求

Postman界面分为两个部分,左边是工具栏,右边是请求编辑器


URL

一个URL就是一个接口,接口大致会分为一下几个部分


  • 请求协议:
    http --- 普通的http请求
    https --- 加密的http请求,传输数据更加安全
  • 请求IP:就是指提供接口的系统所部署的服务器地址
  • 请求端口:默认是80
  • 接口路径:指系统提供的接口在什么位置
  • 接口参数:参数在接口路径后,用“?”来表示路径地址完了,剩下的都是参数了,用“&”来区分参数个数
Params(参数)

点击URL Params按钮,会打开key-value编辑参数,这样比较直观


Headers

HTTP Headers是HTTP请求和相应的核心,它承载了关于客户端浏览器,请求页面,服务器等相关的信息。


Method

GET
POST

Request body

post请求参数在实体中


环境变量

我们可能需要在多个环境下对同一个接口进行测试。比如我们请求的域名,开发、测试、生产环境,请求参数。在地址栏、header、请求参数、外部数据文件里,用 {{变量名}} 获取环境变量的值
Postman 的环境变量分为 environment 和 global 2种

Environment
手动设置环境变量
  • 点击设置按钮
  • 选择 Manage Environment


  • 点击Add
  • 填写变量名和变量值


代码自动创建环境变量
  • 请求发起之前创建,在Pre-request Script标签里面添加代码
    postman.setEnvironmentVariable("variable_key", "variable_value");
  • 在某个请求发起之后创建,在Tests标签里面添加如下


变量的引用

引用的时候加上双花括号:{{变量名}}

动态请求参数

Postman有以下三种内建变量:

{{$guid}}  // 生成GUID
{{$timestamp}}  // 当前时间戳
{{$randomInt}}  // 0-1000的随机整数

在Pre-request Script里写代码处理,存为环境变量,参数里用{{变量名}}取值
例如

// 随机整数
const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;  
 // 随机选项
const getRandomValue = list => list[randomInt(0, list.length - 1)]; 

// 随机手机
environment.randomMobile = `18${randomInt(100000000, 999999999)}`;
// 随机设备名
environment.randomDevice = getRandomValue(['ios', 'android']);

//随机生成一个字符串作为用户名 
postman.setEnvironmentVariable("random_user", ("0000" + 
(Math.random()*Math.pow(36,4) << 0).toString(36)).slice(-4));

//发起请求之前获取当前的时间戳放在参数里:
postman.setEnvironmentVariable("unixtime_now", Math.round(new Date().getTime()/1000));

Test

Setting an environment variable
postman.setEnvironmentVariable("key", "value");

Getting an environment variable
postman.getEnvironmentVariable("key");

Set a global variable
postman.setGlobalVariable("key", "value");

Get a global variable
postman.getGlobalVariable("key");

Check if response body contains a string
tests["Body matches string"] = responseBody.has("string_you_want_to_search");

Convert XML body to a JSON object
var jsonObject = xml2Json(responseBody);

Check if response body is equal to a string
tests["Body is correct"] = responseBody === "response_body_string";

Check for a JSON value
var data = JSON.parse(responseBody); tests["Your test name"] = data.value === 100;

Content-Type is present (Case-insensitive checking)
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); //Note: the getResponseHeader() method returns the header value, if it exists.

Content-Type is present (Case-sensitive)
tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");

Response time is less than 200ms
tests["Response time is less than 200ms"] = responseTime < 200;

Status code is 200
tests["Status code is 200"] = responseCode.code === 200;

Code name contains a string
tests["Status code name has string"] = responseCode.name.has("Created");

Succesful POST request status code
tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;

Use TinyValidator for JSON data

var schema = {
 "items": {
 "type": "boolean"
 }
};
var data1 = [true, false];
var data2 = [true, 123];
tests["Valid Data1"] = tv4.validate(data1, schema);
tests["Valid Data2"] = tv4.validate(data2, schema);
console.log("Validation failed: ", tv4.error);

实例

//验证本次请求的状态码 
tests["Status code is 200"] = responseCode.code === 200;

tests["检查返回的数据是否有成功执行"] = responseBody.has("成功执行");

tests["响应时间不超过500ms"] = responseTime < 500;

//先声明一个变量jsonData,调用JSON.parse()方法将responseBody转换成json的标准格式并赋值给变量jsonData,将返回结果传给其他接口使用
//检测JSON中的某个值是否等于预期的值;
//JSON.parse()方法,把json字符串转化为对象。parse()会进行json格式的检查是一个安全的函数。
var jsonData = JSON.parse(responseBody);

var jsonObject = xml2Json(responseBody);

//检测errcode字段的值是否为0
tests["errcode is 0"] = jsonData.errcode === `0`;
检测errcode字段的长度是否是1
tests["errcode's length"] = jsonData.errcode.length ===1

tests["description is 成功执行"] = jsonData.description === '成功执行'
tests["description's length"] = jsonData.description.length ===4

//检测description的数据类型是否是String
tests["description 的数据类型为String"] = _.isString(jsonData.description)

//测试response Headers中的某个元素是否存在(如:Content-Type)
//getResponseHeader()方法会返回header的值,如果该值存在
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); 

//判断字段的类型
var schema = {
 properties:{
     errcode: {type: 'String'},
     description:{type:'String'}
 }
};
tests["响应实体的类型 "] = tv4.validate(responseBody, schema);

相关文章

网友评论

    本文标题:Postman接口自动化

    本文链接:https://www.haomeiwen.com/subject/mmucnttx.html