Postman API Testing

Posted on Nov 18, 2020

Example Postman Tests

Notes

Here is the detailed explanation of above script.

The Example below shows a Postman test-cript for automation the process of authentication(Basic Auth)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
let tokenUrl = 'https://test-gateway.tulaa.io/uaa-server/oauth/token'
let clientId='<client_id>'
let clientSecret='<client_secret>'

let getTokenRequest = {

    method:'POST',
    url:tokenUrl,
    auth: {
        type:'basic',
        basic: [
            {key:'username',value:clientId},
            {key:'password',value:clientSecret}

            ]
    },
    body: {
        mode:'urlencoded',
        urlencoded: [
            {key: 'username', value: '<USERNAME>'},
            {key: 'password', value: '<PASSWORD>'},
            {key: 'grant_type', value: 'password'}
            ]
    }
}

pm.sendRequest(getTokenRequest,(err,response) => {
    let jsonResponse = response.json(),
    newAccessToken = jsonResponse.access_token

    console.log({err,jsonResponse,newAccessToken})

    pm.globals.set('tulaa_token',newAccessToken)
});

Another example using JSON payload

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
let tokenUrl='<url>';

let getTokenRequest = {
    method:'POST',
    url:tokenUrl,
    header: 'Content-Type:application/json',
    body:{
        mode:'application/json',

       	raw: JSON.stringify({
 			username: '<USERNAME>',
 			password: '<PASSWORD>'
 		})
  }
};
pm.sendRequest(getTokenRequest,(err,response)=> {
    let jsonResponse = response.json(),
    newAccessToken = jsonResponse.data;

    console.log({err,jsonResponse,newAccessToken})

    pm.globals.set('accessToken',newAccessToken);
});