51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 3617|回复: 1
打印 上一主题 下一主题

[原创] postman_操作 《三》

[复制链接]
  • TA的每日心情
    开心
    2022-11-16 14:46
  • 签到天数: 5 天

    连续签到: 1 天

    [LV.2]测试排长

    跳转到指定楼层
    1#
    发表于 2021-4-20 16:08:11 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    三、示例
    最后完整的看一下我用的例程。这个例子是一个非常简单的小系统,用户可以注册并登录,然后在系统里新建充值卡,并给这张卡充值。整个流程如下:
    1. 注册
    生成一个随机字符串作为用户名和昵称
    postman.setEnvironmentVariable("random_username", ("0000" + (Math.random()*Math.pow(36,4) << 0).toString(36)).slice(-4));
    发起请求
    POST /index.php/users HTTP/1.1Host: postmanexample.bayes.cafeCache-Control: no-cachePostman-Token: 76791813-aac2-71fb-cad4-3e737f37c4d0Content-Type: application/x-www-form-urlencoded username=2mjk&password=123456&nickname=2mjk
    运行测试、检查结果
    tests["Status code is 201"] = responseCode.code === 201;
    2. 登录
    直接用刚才生成的环境变量发起请求
    POST /index.php/authentication HTTP/1.1Host: postmanexample.bayes.cafeCache-Control: no-cachePostman-Token: aac7d0ac-e0e3-ecf2-39da-b8dca672e3d7Content-Type: application/x-www-form-urlencoded username=2mjk&password=123456
    运行测试、检查结果,并将返回的token记录下来
    tests["Status code is 200"] = responseCode.code === 200; var data = JSON.parse(responseBody);postman.setEnvironmentVariable("token", data.token);
    3. 添加一张卡
    先生成一个卡号和卡名
    postman.setEnvironmentVariable("random_cardno", Math.round(Math.random()*9999999)); postman.setEnvironmentVariable("random_cardname", ("0000" + (Math.random()*Math.pow(36,4) << 0).toString(36)).slice(-4));
    然后发起请求,这里调用了刚才获取到的Token,放在header的自定义字段里作为鉴权(SAE不能用Authorization这个字段,不清楚原因)
    POST /index.php/cards HTTP/1.1Host: postmanexample.bayes.cafeX-Authorization: d4c4a0b7b36c73e7a13b7e24a596093bCache-Control: no-cachePostman-Token: d44d573f-f17a-366c-2cd7-1d5b8b709233Content-Type: application/x-www-form-urlencoded cardno=1385526&desc=2mo8
    运行测试
    tests["Status code is 200"] = responseCode.code === 200;
    4. 查询刚才生成的卡
    发起请求,调用了刚才生成的卡号
    GET /index.php/cards/1385526 HTTP/1.1Host: postmanexample.bayes.cafeCache-Control: no-cachePostman-Token: 1e5aca57-c3bb-7404-2791-c639cd60b5c8
    运行验证,和刚才生成的卡名对比,并记录新卡的ID
    var data = JSON.parse(responseBody);tests["check cardname"] = data.desc === environment.random_cardname; postman.setEnvironmentVariable("new_card_id", data.id);
    5. 充值
    发起请求,使用了刚才获得的新卡ID
    POST /index.php/deposit HTTP/1.1Host: postmanexample.bayes.cafeX-Authorization: d4c4a0b7b36c73e7a13b7e24a596093bCache-Control: no-cachePostman-Token: 388c95e0-b5ce-9bbf-5816-084db7523384Content-Type: application/x-www-form-urlencoded cardid=1&amount=10
    运行验证(由于是新建的用户,没有余额,无法给卡片充值,故返回403 Forbidden)
    tests["Status code is 403"] = responseCode.code === 403;

    P.S.postmanexample.bayes.cafe这个网站是真实存在的,可以Import我上传的Collection(https://www.getpostman.com/collections/96b64a7c604072e1e4ee)到你自己的Postman中,并设置环境变量url为http://postmanexample.bayes.cafe,就能运行这个Collection看效果了。
    服务器端程序的代码已经在GitHub开源。



    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-5-5 06:34 , Processed in 0.063042 second(s), 22 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表