Postman Script realizes the use of automation functions in detail

The postman Script function supports native JS, so JS can be used to solve some problems of interface automation. Here is an introduction to the two modules, Pre-Request Script and Tests. It supports dynamic preparation of test data by embedding scripts, and Design test cases based on business requirements.

The running order of the postman interface: before each request, run the method in the Pre-request Script, then call the interface, and finally run the method in the Tests, that is (Pre-request Script->Interface Request->Tests)

一、Pre-request Script

Before sending the request, write a pre-request script to customize the request. Here is an example. Before each call to the interface, get the date beginTime and endTime, save them as environment variables, and provide them to the interface to pass parameters, as shown in the following figure:

Each time the interface is run, the beginTime and endTime values ​​of the environment variables are updated;

For a detailed explanation of how to get dates in JS

二、Test Script

After receiving the response, use the test script to process the returned data. This component is mainly used to handle interface dependencies and implement interface assertions;

1. Interface parameter dependency: By obtaining the value in the returned data and saving (updating) it as an environment variable, the problem of interface parameter dependency is solved;

2. Assertion: to check whether the interface returns normal or not by judging whether the data returned by the interface contains each specific value;

Here are two examples, as shown below:

Get the gameCode parameter and save it to the environment variable, and provide it to the following interface for calling;

 

3. Random parameters:

In addition to providing environment variables and global variables, PostMan also provides three kinds of random numbers.
{{$guid}}
: add a V4 style GUID

{{$timestamp}}
: the current timestamp, accurate to seconds

{{$randomInt}}
: add a random integer between 0 and 1000

 

Here is an example that needs to pass a non-repeatable order number:

"OrderCode":"cross-{{$timestamp}}"

 

For more introductions to postman, you can browse his articles on the blog.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326115651&siteId=291194637