postman内置脚本

1. 清除一个全局变量

Clear a global variable

    对应脚本:

    postman.clearGlobalVariable("variable_key");

    参数:需要清除的变量的key


2.清除一个环境变量

Clear an environment variable

    对应脚本:

    postman.clearEnvironmentVariable("variable_key");

    参数:需要清除的环境变量的key


3.得到一个环境变量

Get  a global variable

对应脚本:

pm.environment.get(“variable_key”);

获取一个环境变量(其值是一个stringified对象)


4.得到一个变量

Gat a variable

对应脚本:

pm.variables.get(“variable_key”);


5.设置环境变量:

Set an environment variable

      对应脚本:

      postman.setEnvironmentVariable("variable_key", "variable_value");

      参数:环境变量的键值


6.设置全局变量:

Set an global variable

      对应脚本:

      postman.setGlobalVariable("variable_key", "variable_value");

      参数:全局变量的键值


7.response包含内容

  Response body:Contains string

    对应脚本:

    tests["Body matches string"] =responseBody.has("string_you_want_to_search");

    参数:预期内容


8.将xml格式的response转换成json格式

Response body:Convert XML body to a JSON Object

    对应脚本:

    var jsonObject = xml2Json(responseBody);

    参数:(默认不需要设置参数,为接口的response)需要转换的xml


9.response等于预期内容

Response body:Is equal to a string

    对应脚本:

    tests["Body is correct"] = responseBody === "response_body_string";

    参数:预期response


10.json解析key的值进行校验

  Response body:JSON value check

    对应脚本:

    tests["Args key contains argument passed as url parameter"] = 'test' in responseJSON.args

    参数:test替换被测的值,args替换被测的key


11.检查response的header信息是否有被测字段

Response headers:Content-Type header check

    对应脚本:

    tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");

    参数:预期header


12.响应时间判断

Response time is less than 200ms

    对应脚本:

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

    参数:响应时间


13.判断状态码

Status code:Code is 200

      对应脚本:

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

      参数:状态码


14.检查code name 是否包含内容

Status code:Code name has string

      对应脚本:

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

      参数:预期code name包含字符串


15.成功的post请求

Status code:Successful POST request

      对应脚本:

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


16.微小验证器

      Use Tiny Validator for JSON data

      对应脚本:

        var schema = {

        "items": {

        "type": "boolean"

            }

        };

        var data1 = [true, false];

        var data2 = [true, 123];

        console.log(tv4.error);

        tests["Valid Data1"] = tv4.validate(data1, schema);

        tests["Valid Data2"] = tv4.validate(data2, schema);

        参数:可以修改items里面的键值对来对应验证json的参数


17.检查response的body是都为一个字符串:

tests["Body is correct"] = responseBody === "response_body_string";


18.检查JSON的值:

var data = JSON.parse(responseBody);

tests["Your test name"] = data.value === 100;


19.内容类型存在(检查不区分大小写)

tests["Content-Type is present"] = postman.getResponseHeader("Content-Type");

//如果方法getResponseHeader() 存在的话,返回header value


20.内容类型存在(区分大小写):

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


21.response的响应时间小于200ms:

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


22.状态码为200:

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. 清除一个全局变量 Clear a global variable 对应脚本: postman....
    一丝风L阅读 4,799评论 0 0
  • 内置脚本说明 1.清除一个全局变量 Clear a global variable 对应脚本: postman.c...
    MR_LiY阅读 5,903评论 0 1
  • 一、 安装与更新 1、安装的方式 方式1:chrome插件版本:chrome--->设置--->扩展程序; 方式2...
    MR_LiY阅读 13,463评论 3 3
  • 【Tests】例子 在发送请求并从服务器收到响应后运行测试脚本。 让我们看一下Postman-Tests模块的一些...
    小娟_bb93阅读 4,073评论 0 0
  • 1.URL Schema 分享到第三方平台,或者是跳转到第三方平台支付等场景使用的是URL Schema。比如说从...
    8fe8946fa366阅读 4,344评论 1 0

友情链接更多精彩内容