The difference between postman global variables and environment variables

1. Global and environment variables can be set in postman (so that some data such as url, token value, test parameters, etc. can be stored)

1. The difference between postman global variables and environment variables:

(1) Global variables: Global variables are globally unique and cannot be defined repeatedly

(2) Environment variables: A variable can only belong to a certain environment, and variables cannot be defined repeatedly in a certain environment; repeated variables can be defined between environments; an environment can contain multiple environment variables; common environment classification: Development environment, test environment, production environment.

2. Variable setting

(1) Variables are stored in the form of key-value.

For example: url: http://test.baidu.com

(2) Use of variables:

Call the keyword to get the value by { {variable key}}

For example: { {url}} can be placed anywhere where parameters need to be filled.

(3) How to create variables:

  Click the Environment quick look in the upper right corner of the postman interface, and in the pop-up window, you can choose to create global variables, import variable files, and create environment variables.

Add environment variables or global variables

After the global variable is created, it can be obtained at any time, while the environment variable can only obtain the variables in this environment when it is set to the current environment. This is set as a global variable or an environment variable as needed.

(4) References to variables:

As shown in the figure, it is the setting and reference of an environment variable. When the mouse hovers over the variable {{key}}   we referenced , the value of the current variable will be displayed.

2. Parameter passing and assertion

  There is a special code area in postman, and some custom operations can be realized through the js code language. In the Pre-request Script area, it is the code area before the request is sent. For example, if we need to encrypt the password with md5 and then send it, we can use the method in js to encrypt it first. In the Tests area, assertions can be made by obtaining the value of the return parameter, and the interface that fails the assertion will be displayed as a false result.

How to pass parameters:

  比如支付接口,需要上一个下单接口返回的订单号来作为入参。

  这个时候,首先要在下单接口的Tests区域中,获取到返回数据的订单号,然后存储为全局变量;

  接着在支付接口中,入参中的订单号值填写为我们设置的全局变量,这样就实现了一次参数传递。

三、在postman中对全局(环境)变量的操作方法

1.1、获取变量

(1)获取全局变量:

pm.globals.get(“variable_key”);

(2)获取当前环境变量:

pm.environment.get(“variable_key”);

(3)获取普通变量:

pm.variables.get(“variable_key”);

1.2、设置变量

(1)设置全局变量:

pm.globals.set(“variable_key”, “variable_value”);

(2)设置当前环境变量:

pm.environment.set(“variable_key”, “variable_value”);

1.3、清空变量值

(1)清空当前环境变量:

pm.environment.unset(“variable_key”);

(2)清空全局变量:

pm.globals.unset(“variable_key”);

2、返回数据的判断

这里需要注意的是,网上有一些教程比较老了,例如这种形式的写法:

tests[“Status code is 200”] = responseCode.code === 200;

这样的写法官方文档中已经表示不推荐使用,而是推荐统一的js书写格式。

(1)判断返回状态码

pm.test(“返回状态码为200”, function () {
  pm.response.to.have.status(200);
});

(2)判断返回header中是否有“Content-Type”字段

pm.test(“Content-Type字段存在”, function () {
  pm.response.to.have.header(“Content-Type”);
});

(3)判断接口返回内容匹配一个字符串

pm.test(“返回内容匹配字符串”, function () {
  pm.response.to.have.body(“response_body_string”);
});

(4)判断返回参数中包含某个字符串

pm.test(“字符串包含在返回值中”, function () {  pm.expect(pm.response.text()).to.include(“想要搜索的字符串”);});

(5)判断接口响应时间

pm.test(“接口响应时间小于200ms”, function () {  pm.expect(pm.response.responseTime).to.be.below(200);});

(6)把responseBody转为json字符串

var Jsondata =JSON.parse(responseBody);

(7)把xml格式返回转换为json格式

var jsonObject = xml2Json(responseBody);

(8)获取并判断返回参数的值

// 假设返回参数为:{“code” : 0}
pm.test(“断言code字段值是否为0”,function(){
  var jsonData = pm.response.json(); // 创建一个变量,值为返回参数(json格式)
  pm.expect(jsonData.code).to.eql(0); // 判断返回参数中code字段值为0
});

官方文档:https://learning.postman.com/docs/writing-scripts/script-references/test-examples/#making-assertions-on-the-http-response

Guess you like

Origin blog.csdn.net/sinat_33101665/article/details/128935572