“This is the first day of my participation in the First Challenge 2022. For details: First Challenge 2022”

Near the end of the year, the company was not busy, so he took the opportunity to let me study PostMan’s script automation testing. As a front-end developer, to be honest, the PostMan operation is just a new request -> fill in the URL and parameters ->send and see what is returned. In fact, PostMan is so powerful that I did a lot of research and compiled a detailed document

Understanding PM objects

PM object – a very core object in PostMan. Contains all information about the script being executed and allows access to a copy of the request being sent or the response received, as well as to get and set environment variables and global variables

Core functions (attributes) inside a PM object

  • Pm.info — Info object contains information about the script being executed (request name, request ID, number of requests….)
  • PM. The variables/PM. The environment/PM. Global management – variables
  • Pm.request – Request for the current script
  • Pm.response – The result of the response to the request on which the current script resides
  • Pm.cookies — Contains a list of cookies associated with the request domain
  • Pm.test — The test function is used to write test scripts
  • Pm. expect — predicate functions

PM on the method (this part does not need to be memorized, when you need to find ok)

PM. The info object

methods describe The result type
pm.info.eventName In which script is the output script executed String
pm.info.iteration The number of iterations currently run Number
pm.infi.iterationCount The total number of iterations planned to run Number
pm.info.requestName The request of String
pm.info.requestId Request ID String

PM. Globals object

methods describe
Pm.globals. has(‘ variable name ‘) Determines whether the global variable exists
Pm.globals.get (‘ variable name ‘) Gets the specified global variable
Pm.globals.set (‘ variable name ‘,’ variable value ‘) Setting global variables
Pm.globals.unset (‘ variable name ‘) Clears the specified global variable
pm.globals.clear() Clear all global variables
pm.globals.toObject() Output all global variables as an object

PM. The environment object

methods describe
Pm.environment. Has (‘ variable name ‘) Determines whether the environment variable contains a value
Pm.environment.get (‘ variable name ‘) Gets the specified environment variable
Pm.environment. Set (‘ variable name ‘,’ variable value ‘) Setting environment Variables
Pm.environment.unset (‘ variable name ‘) Clears specified environment variables
pm.environment.clear() Clear all environment variables
pm.environment.toObject() Output all environment variables as an object

PM. The object of the variables

methods describe
Pm.variable. get(‘ variable name ‘) Gets the specified value in a variable

PM. The request object

methods describe
pm.request Gets all headers for the currently initiated request
pm.requset.url Gets the URL for the current request
pm.request.headers Returns the header information in the current request as an array

PM. The response object

methods describe
pm.response.code The status code returned by the current request is 200, 404, 500…
pm.response.reason() The current request returns Ok on success
pm.response.headers Returns an array of successful request headers for the current request
pm.response.responseTime Obtain the duration of this request, in ms
pm.response.text() Get the body content of the response as text
pm.response.json() Parse the contents of the body into json objects

The Test in the PostMan

The main functions used for testing and asserting in Postman are

methods describe
pm.test(“testName”,specFunction) Test method, the second method to fill in the test content
pm.expect(assertion:*) assertions

Understand the variables

  • Global variable, once declared global variable, global valid. This variable can be used in any collection in Postman, in any request. Maximum scope
  • Collection variables, collection variables are for collections, declared variables must be based on a collection, use scope is only valid for a collection
  • Environment variables, declare environment variables, first create the environment, then create variables in the environment, if you want to use the environment variable, must first select the environment. Multiple environments can be created, and each environment can have multiple variables

The above figure shows the relationship between variables in postman, from small to large: data->local->-environment->clooection->global. Local has changed and I haven’t studied it yet, but I’ll talk about data objects in a second

Define variables (environment variables & global variables)

Define variables (set variables)

Use the variable

1. Use variable {{variable name}} in request parameters, including URL, Params, Authorization, Headers, and Body

2. Obtain variables in Tests or pre-request Script

  • Get global variables -pm.globals. Get (” variable name “)
  • Get set variable – PM. CollectionVariables. Get (” variable name “)
  • Get environment variables -pm.environment. Get (” variable name “)

External files to parameterize data (import CSV files)

Implementation steps

2. Replace the parameter variables in the request,{{}} or data. XXX (the fields in the CSV file we imported can be accessed directly through the data object) 3. Select collection -> Click Run-> select the corresponding data fileCopy the code

assertions

    //pm.expect is a generic assertion function, used mainly in conjunction with pm.test. The common uses are as follows
    pm.test("testName".function(){
        pm.expect('xxx').to.include('xxx');
    })
    // pm.expect() expects an assertion (the actual result, usually the result returned by response)
    //.to is a concatenator used to join assertions and judgments. These concatenators have no effect on the result, but simply make the assertion statement check the English syntax. Connectives include to, be, been, is, that, which, and, has, have, with, at, of, same
    //.include() is an assertion that receives expected results
Copy the code

Common assertion methods

Include (value)/. Contains (value)– indicates whether the actual result contains the expected result. Ok /.true– asserts that the target is true

Newman

introduce

Newman is a Node.js-based postman running tool that can run and test postman collections directly from the command line

The installation

NPM install -g Newman – Install Newman NPM intall -g newman-reporter-html – Install plug-ins needed to generate HTML reports

grammar

newman run xxx.json -d xxx.json/xxx.csv

  • Run XXx. json – Identifies the Postman script to execute, which is the collection we exported
  • -d xxx.json/xxx.csv – Identifies the data to be executed. Data is our customized parameter file

Generate the report in HTML format

newman run xxx.json -d xxx.json/xxx.csv -r html –reporter-html-export /Users/lz01/Desktop/tdtest.html By default, the HTML report is saved in the current directory. If you want to save the HTML report to a specified directory, add a path before the file name

At the end

The above is the application of Postman and Newman. We can import the required parameters and expected states of each interface into CSV files or JSON, and then directly test the results of each interface through commands, saving a lot of testing time. As for the integration into Jenkins to achieve a real automated test, this is still our boss to do, limited ability is not much to introduce, interested partners can check