Step by step testing with postman :: Part 1

We will automate the following API’s:

https://customer-test-api.herokuapp.com/customer/api/v1/login
https://customer-test-api.herokuapp.com/customer/api/v1/list
https://customer-test-api.herokuapp.com/customer/api/v1/get/110
https://customer-test-api.herokuapp.com/customer/api/v1/create
https://customer-test-api.herokuapp.com/customer/api/v1/update/110
https://customer-test-api.herokuapp.com/customer/api/v1/delete/110

Create Workspace

From workspace menu, click on “New Workspace” and give workspace name.

Click on Create workspace button. You will see new workspace has been created and showing on workspace list.

Create Collection:

Select workspace from list.

Click on the “Create Collection” button.

Give the collection name and press enter.

Add Request:

Click on “Add a request” link or right click on the collection and Click on Add request menu.

Give the request name as Customer Login

Create POST API

Now Enter request URL as:

https://customer-test-api.herokuapp.com/customer/api/v1/login

Select the HTTP request method from GET to POST

Now go to Body tab

Select radio button as raw and you will see a script writing panel has opened.

Write this json to the script:

{
    "username":"salman",
    "password":"salman1234"
}

Now go to headers and add this in header:

KEY: Content-Type
VALUE: application/json

Now click on Send button and you will see that you will get a token in the response

Create environment variable:

Click on the eye icon

Click on “Add” button and give Environment name as “Customer API Env”

Now set VARIABLE as baseUrl and INITIAL VALUE as https://customer-test-api.herokuapp.com

You will see that CURRENT VALUE will be set automatically after you set INITIAL VALUE

Now go to the collection again and replace the base url “https://customer-test-api.herokuapp.com” with {{baseUrl}}.

Important note: When you save something in an environment variable, then while using it, you have to put the variable name within a curly bracket.

Then, you will see the {{baseUrl}} color gets orange. If the color is not orange, there some spelling mistake or you don’t set the environment from the environment list.

So don’t forget to set the environment from the environment list.

When you will mouse hover over the {{baseUrl}}, then you will see showing the link you just set the link tho the environment variable.

Now click on save button.

Now again click on Send button and you will see the same result is showing on response.

If not, read the article again and try to find the error.

Now go to Tests tab and copy paste following code:

var jsonData = pm.response.json();
pm.environment.set("token", jsonData.token);

This code means, the response we have saved in a variable named jsonData and then retrieve token from jsonData response and set to the environment named as “token”. Now click on Send button.

After clicking on send button, token will generate and set to the environment variable.

Calling GET API using Authentication:

Create another request named as “Customer List”

Now add this URL to the request URL field

{{baseUrl}}/customer/api/v1/list

Make sure, HTTP method is selected as “GET”

Now go to Headers tab

Set KEY as Authorization and VALUE as {{token}}

Now click on Send button

You will see response will be shown.

Now got to Tests tab and add following lines:

pm.test("Check customer data", function () {
    var response = pm.response.json();
    pm.expect(response.Customers[0].id===101).to.equals(true);
});

These code means, if the customer id 101 has been matched with 101, then expected result is passed. Otherwise failed.

If you failed to set Authorization, then following response will found:

Get API response by parameter

Add another GET request.

Set URL as: {{baseUrl}}/customer/api/v1/get/101

Set HTTP request method as: GET

Click on Send button

You will get following response:

{
    "id": 101,
    "name": "Mr. Kamal",
    "email": "mrkamal@test.com",
    "address": "Gulshan,Dhaka",
    "phone_number": "01502020110"
}

POST API to register new customer

Now create another request named as: “Add new customer”

Set HTTP request as POST

Set URL as {{baseUrl}}/customer/api/v1/create

Got to body tab and select radio button raw

Add following json in the script:

{
    "id":{{id}},
    "name":"{{name}}",
    "email":"{{email}}",
    "address":"Gulshan, Dhaka",
    "phone_number":"{{phone_number}}"
}

Now go to pre-requsite script and add following codes:

let id=_.random(1000, 9999)
pm.environment.set("id",id );
const reqObject = {
  url: 'https://api.namefake.com/english-united-states/male/',
  method: 'GET',
  header: 'Content-Type:application/json'
};

pm.sendRequest(reqObject, (err, res) => {
    let {name} = res.json()
    let {phone_w}=res.json()
    let {email_u} = res.json()
    let email=email_u+'@testmail.com'
    
    pm.environment.set("name", name);
    pm.environment.set("email", email);
    pm.environment.set("phone_number", phone_w);
});

This code actually calling an API to generate some user info like id, name, phone_number, and email and set to env variables.

Now go to Tests tab and add following lines:

pm.test("Status code is 201", function () {
    pm.response.to.have.status(201);
});

These code means that, while any customer registered newly, then the response code will be 201

Calling PUT API

Like as PUT API, create new request named as “Update customer”

Set HTTP method as PUT

Set URL as {{baseUrl}}/customer/api/v1/update/{{id}}

Write following json to body:

{
    "id":{{id}},
    "name":"Rahim Uddin",
    "email":"rahim@test.com",
    "address":"Gulshan, Dhaka",
    "phone_number":"01502212471"
}

Now click on Send button

You will get following response:

Create DELETE API

Like GET API, create another request named as “Delete customer”

set HTTP method as: DELETE

set URL as : {{baseUrl}}/customer/api/v1/delete/{{id}}

Click on send button.

You will see that the latest customer has been deleted and you will get following response:

Finally you will see that you have created following API collection:

about author

admin

salmansrabon@gmail.com

If you like my post or If you have any queries, do not hesitate to leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *