Postman-05-编写脚本

目录

1. 简介

2. 脚本运行顺序

2.1单个请求

2.2集合中

3. 请求前脚本

4. 请求后脚本

5. 脚本示例

5.1解析响应数据

5.2 断言类型

5.3断言响应时间

5.4断言响应头

5.5 断言响应主体

5.6断言状态码

5.7 断言Cookie

5.8使用多个断言   


1.简介

Postman基于Node.js编写,运行时可让您向请求和集合中添加动态行为。这样,您就可以编写测试套件,构建可以包含动态参数的请求,在请求之间传递数据等等。您可以添加JavaScript代码以在2个事件期间执行:

  1. 一个请求之前被发送到服务器,作为 预请求脚本 的 re-request script  标签。
  2. 收到响应后,作为  test script 选项卡下的 测试脚本

2.脚本运行顺序

2.1单个请求

  • 与请求关联的预请求脚本将在发送请求之前执行
  • 发送请求后,将执行与请求关联的测试脚本

                     

       

2.2集合中

对于集合中的每个请求,脚本将按以下顺序执行:

  • 与集合关联的预请求脚本将在集合中的每个请求之前运行。
  • 与文件夹关联的预请求脚本将在文件夹中的每个请求之前运行。
  • 与集合关联的测试脚本将在集合中的每个请求之后运行。
  • 与文件夹关联的测试脚本将在文件夹中请求后运行。

3.请求前脚本

Pre-request Script : 在将该值传递给第二个请求之前,需要对其进行处理

                                 

4.请求后脚本

   

验证请求返回的数据,可以使用pm.response对象。

使用pm.test函数定义测试,并提供一个名称和函数,该函数返回一个布尔值(truefalse)来指示测试是通过还是失败。

// 断言响应状态码
pm.test("Status test", function () {
    pm.response.to.have.status(200);
});
// 使用适合于响应数据格式的语法来确定请求响应的有效性
pm.test("response must be valid and have a body", function () {
     pm.response.to.be.ok;
     pm.response.to.be.withBody;
     pm.response.to.be.json;
});
// 访问环境
pm.test("environment to be production", function () {
    pm.expect(pm.environment.get("env")).to.equal("production");
});

5.脚本示例

5.1解析响应数据

// json格式
const responseJson = pm.response.json();
// xml格式
const responseJson = xml2Json(pm.response.text());
const $ = cheerio.load(pm.response.text());
//output the html for testing
console.log($.html());

5.2 断言类型

// 断言类型
const jsonData = pm.response.json();
pm.test("Test data type of the response", () => {
  pm.expect(jsonData).to.be.an("object");
  pm.expect(jsonData.name).to.be.a("string");
  pm.expect(jsonData.age).to.be.a("number");
  pm.expect(jsonData.hobbies).to.be.an("array");
  pm.expect(jsonData.website).to.be.undefined;
  pm.expect(jsonData.email).to.be.null;
});

5.3断言响应时间

pm.test("Response time is less than 200ms", () => {
  pm.expect(pm.response.responseTime).to.be.below(200);
});

5.4断言响应头

// 响应头字段
pm.test("Content-Type header is present", () => {
  pm.response.to.have.header("Content-Type");
});

// 响应头字段对应的值
pm.test("Content-Type header is application/json", () => {
  pm.expect(pm.response.headers.get('Content-Type')).to.eql('application/json');
});

5.5 断言响应主体

pm.test("Person is Jane", () => {
  const responseJson = pm.response.json();
  pm.expect(responseJson.name).to.eql("Jane");
  pm.expect(responseJson.age).to.eql(23);
});

5.6断言状态码

// 断言状态码是否在数组中
pm.test("Successful POST request", () => {
  pm.expect(pm.response.code).to.be.oneOf([201,202]);
});
// 断言状态码值
pm.test("Status code is 201", () => {
  pm.response.to.have.status(201);
});

5.7 断言Cookie

// 是否存在
pm.test("Cookie JSESSIONID is present", () => {
  pm.expect(pm.cookies.has('JSESSIONID')).to.be.true;
});
// 断言cookie 值
pm.test("Cookie isLoggedIn has value 1", () => {
  pm.expect(pm.cookies.get('isLoggedIn')).to.eql('1');
});

5.8使用多个断言   

pm.test("The response has all properties", () => {
    //parse the response json and test three properties
    const responseJson = pm.response.json();
    pm.expect(responseJson.type).to.eql('vip');
    pm.expect(responseJson.name).to.be.a('string');
    pm.expect(responseJson.id).to.have.lengthOf(1);
});
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Postman pre-script脚本编写是指在发送请求之前执行的脚本编写。这些脚本可以用来设置请求头、请求参数、请求体等信息,也可以用来进行数据验证、数据处理等操作。在编写pre-script脚本时,需要使用Postman提供的JavaScript语言进行编写。常见的pre-script脚本包括: 1. 设置请求头信息: 在pre-script脚本中,可以使用postman.setHeaders()方法来设置请求头信息。例如: postman.setHeaders({ 'Content-Type': 'application/json', 'Authorization': 'Bearer xxxxxxxx' }); 2. 设置请求参数: 在pre-script脚本中,可以使用postman.setQueryParams()方法来设置请求参数。例如: postman.setQueryParams({ 'page': 1, 'limit': 10 }); 3. 设置请求体: 在pre-script脚本中,可以使用postman.setRequestBody()方法来设置请求体。例如: postman.setRequestBody({ 'name': '张三', 'age': 18 }); 4. 数据验证: 在pre-script脚本中,可以使用断言(assertion)来进行数据验证。例如: pm.test("响应状态码为200", function () { pm.response.to.have.status(200); }); 5. 数据处理: 在pre-script脚本中,可以使用JavaScript语言进行数据处理。例如: var jsonData = pm.response.json(); var token = jsonData.token; pm.environment.set("token", token); 以上是Postman pre-script脚本编写的一些常见用法,可以根据实际需求进行编写。 ### 回答2: Postman是一款强大的API测试工具,它可以帮助开发人员简化API测试的流程。其中,Postman pre-script脚本可以帮助用户在发送请求前执行一些自定义的JavaScript代码,用于设置或修改请求参数、请求头等内容,从而实现更高效、准确的测试。 以下是Postman pre-script脚本编写的具体步骤: 1. 打开Postman并创建一个请求。 2. 在请求页面中,点击“Pre-request Script”选项卡。 3. 在文本编辑框中输入JavaScript代码,这些代码将会在每次发送请求之前执行。 在pre-script脚本编写中,可使用各种JavaScript API,如fetch、Promise、console等,以及Postman自带的内置变量和函数,如pm.request、pm.environment、pm.sendRequest等。 例如,可以在pre-script脚本中设置请求头、请求参数等: ```javascript // 设置请求头 pm.request.headers.add({ key: 'Authorization', value: 'Bearer ' + pm.environment.get('access_token') }); // 修改请求参数 pm.request.body.urlencoded.update({ key: 'name', value: 'tom' }); ``` 除了基本的设置和修改,还可以在pre-script脚本中进行数据处理、加密解密、判断逻辑等操作,以实现更复杂的测试场景。 需要注意的是,尽管Postman在测试中提供了很多便利,但也需要开发人员具备基本的编程能力和测试思路,合理运用pre-script脚本,才能发挥Postman的最大价值。 ### 回答3: postman pre-script是一种在请求发送之前运行的JavaScript脚本,可以用来设置请求的变量、设置头文件、修改请求体、进行数据转换等一系列操作。下面是使用postman pre-script脚本的详细步骤: 1. 打开postman并创建新的请求。 2. 选择想要使用的请求方式,如GET、POST等。 3. 在请求的Headers或Body等区域编写所需要的内容。 4. 在请求页面下方找到“Pre-request Scripts”按钮并点击打开。 5. 在代码编辑器中输入自己需要的JavaScript代码。此脚本可以被当成一个公用模块来使用,在其他请求中都可以引用。 6. 在代码编辑器右下角点击“Save”按钮来保存代码。 7. 测试请求,看到请求是否成功以及数据是否符合预期。 在pre-script脚本中,可以使用下列代码段进行常用操作: 1. 设置变量: postman.setEnvironmentVariable('variableName', 'value'); 2. 修改请求头: pm.request.headers.add({'key':'Content-Type', 'value':'application/json'}) 3. 修改请求体: pm.request.body.raw=JSON.stringify({ “foo”: “bar”}) 4. 进行加密/解密/编码/解码操作: let encryptedValue = CryptoJS.AES.encrypt('secretMessage', 'secretKey') let decryptedValue = CryptoJS.AES.decrypt(encryptedValue, 'secretKey'); let encodedValue = btoa('hello world'); let decodedValue = atob(encodedValue); 总之,postman pre-script脚本postman的用户提供了更加完备的自动化测试工具。它能够让用户更加方便地对请求进行设置和修改,从而更加方便地进行接口测试操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱学习de测试小白

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值