• PostMan Test 的脚本scripts编写方法


    1 设置环境变量
      2 pm.environment.set("variable_key", "variable_value");
      3  
      4 将一个嵌套的对象设置为一个环境变量
      5 var array = [1, 2, 3, 4];
      6 pm.environment.set("array", JSON.stringify(array, null, 2));
      7  
      8 var obj = { a: [1, 2, 3, 4], b: { c: 'val' } };
      9 pm.environment.set("obj", JSON.stringify(obj))
     10  
     11 获得一个环境变量
     12 pm.environment.get("variable_key");
     13  
     14 获得一个环境变量(其值是一个字符串化的对象)
     15 // These statements should be wrapped in a try-catch block if the data is coming from an unknown source.
     16 var array = JSON.parse(pm.environment.get("array"));
     17 var obj = JSON.parse(pm.environment.get("obj"));
     18  
     19 清除一个环境变量
     20 pm.environment.unset("variable_key");
     21  
     22 设置一个全局变量
     23 pm.globals.set("variable_key", "variable_value");
     24  
     25 获取一个全局变量
     26 pm.globals.get("variable_key");
     27  
     28 清除一个全局变量
     29 pm.globals.unset("variable_key");
     30  
     31 获取一个变量
     32 该函数在全局变量和活动环境中搜索变量
     33 pm.variables.get("variable_key");
     34  
     35 检查响应主体是否包含字符串
     36 pm.test("Body matches string", function () {
     37     pm.expect(pm.response.text()).to.include("string_you_want_to_search");
     38 });
     39  
     40 检查响应体是否等于字符串
     41 pm.test("Body is correct", function () {
     42     pm.response.to.have.body("response_body_string");
     43 });
     44  
     45 检查JSON值
     46 pm.test("Your test name", function () {
     47     var jsonData = pm.response.json();
     48     pm.expect(jsonData.value).to.eql(100);
     49 });
     50  
     51 Content-Type 存在
     52 pm.test("Content-Type is present", function () {
     53     pm.response.to.have.header("Content-Type");
     54 });
     55  
     56  
     57 返回时间少于200ms
     58 pm.test("Response time is less than 200ms", function () {
     59     pm.expect(pm.response.responseTime).to.be.below(200);
     60 });
     61  
     62 状态码是200
     63 pm.test("Status code is 200", function () {
     64     pm.response.to.have.status(200);
     65 });
     66  
     67 代码名包含一个字符串
     68 pm.test("Status code name has string", function () {
     69     pm.response.to.have.status("Created");
     70 });
     71  
     72 成功的POST请求状态码
     73 pm.test("Successful POST request", function () {
     74     pm.expect(pm.response.code).to.be.oneOf([201,202]);
     75 });
     76  
     77 为JSON数据使用TinyValidator
     78 var schema = {
     79  "items": {
     80  "type": "boolean"
     81  }
     82 };
     83 var data1 = [true, false];
     84 var data2 = [true, 123];
     85  
     86 pm.test('Schema is valid', function() {
     87   pm.expect(tv4.validate(data1, schema)).to.be.true;
     88   pm.expect(tv4.validate(data2, schema)).to.be.true;
     89 });
     90  
     91 解码base64编码数据
     92 var intermediate,
     93     base64Content, // assume this has a base64 encoded value
     94     rawContent = base64Content.slice('data:application/octet-stream;base64,'.length);
     95  
     96 intermediate = CryptoJS.enc.Base64.parse(base64content); // CryptoJS is an inbuilt object, documented here: https://www.npmjs.com/package/crypto-js
     97 pm.test('Contents are valid', function() {
     98   pm.expect(CryptoJS.enc.Utf8.stringify(intermediate)).to.be.true; // a check for non-emptiness
     99 });
    100  
    101 发送异步请求
    102 此函数可作为预请求和测试脚本使用
    103 pm.sendRequest("https://postman-echo.com/get", function (err, response) {
    104     console.log(response.json());
    105 });
    106  
    107 将XML主体转换为JSON对象
  • 相关阅读:
    C# Socket 实现WebSocket服务器端
    Linux Vi 的使用
    Microsoft Sql Server 2016安装在CentOS7下
    通过反射获取所有继承了某一接口的类
    Windows下常用的100个CMD指令以及常见的操作
    CentOS系统安装遇到的一些问题
    SQL Server 2016最值得关注的10大新特性
    用注册表禁止windows添加新用户
    ASP.NET MVC学习之模型验证详解
    (转)RBAC权限管理
  • 原文地址:https://www.cnblogs.com/linyu51/p/13348203.html
Copyright © 2020-2023  润新知