API Testing with Axios Framework :: Part-2

Signup a New Customer

To signup a new customer, you have to call a POST HTTP Request

URL: https://customer-test-api.herokuapp.com/customer/api/v1/create

Body:
{
    "id": int,
    "name": "String",
    "email": "String",
    "address": "String",
    "phone_number":"String"
}

Remove “.only” from the previous HTTP Request

Go to env.json file

Add these key:

    "id":"", 
    "name": "",
    "email": "",
    "address": "",
    "phone_number": ""

We will generate following information before calling the Create API

Write this code:

before("Generate Fake Info", async () => {
        const response = await axios.get(`https://api.namefake.com/english-united-states`,
            {
                headers: {
                    'Content-Type': 'application/json',
                }
            }
        ).then(res => res.data)
        envVariables.id = Math.floor((Math.random() * (9999-1001)) + 1);
        envVariables.name = response.name;
        envVariables.email = `${response.email_u}@test.com`;
        envVariables.address = response.address;
        envVariables.phone_number = response.phone_w;
        fs.writeFileSync('./env.json', JSON.stringify(envVariables));

    })

Here, “before” is a mocha hook. It will execute the fake info generating API before calling the Customer SignUp API so that we can use the generated information to sign up new customers

Now write this code:

it.only("Signup User", async () => {
        const response = await axios.post(`${envVariables.baseUrl}/customer/api/v1/create`,
            {
                "id": envVariables.id,
                "name": envVariables.name,
                "email": envVariables.email,
                "address": envVariables.address,
                "phone_number": envVariables.phone_number
            },
            {
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': envVariables.token
                }
            }
        ).then(res => res.data)
        console.log(response);
        expect(response.message).contains("Success");

    })

Now execute by the following command:
npx mocha .\customers.test.js --timeout=30000

Here, You see that some fake info has been generated in the env.json file and a new customer has been registered by the generated info

Calling customer update API

Now skip the fake info generating API by using “before.skip” and remove “.only” from the previous SignUp API

Now write following code:

it.only("Update Customer", async () => {
        const response = await axios.put(`${envVariables.baseUrl}/customer/api/v1/update/${envVariables.id}`,
            {
                "id": envVariables.id,
                "name": envVariables.name,
                "email": envVariables.email,
                "address": "Dhaka,Bangladesh",
                "phone_number": "01602212410"
            },
            {
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': envVariables.token
                }
            }
        ).then(res => res.data)
        console.log(response);
        expect(response.message).contains("Success");

    })

Now execute by the following command:
npx mocha .\customers.test.js --timeout=30000

Calling the customer delete API

Remove “.only” from the previous API. Then write following code:

it.only("Delete Customer", async () => {
        const response = await axios.delete(`${envVariables.baseUrl}/customer/api/v1/delete/${envVariables.id}`,
            {
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': envVariables.token
                }
            }
        ).then(res => res.data)
        console.log(response);
        expect(response.message).contains("Customer Deleted!");
    })

Now execute by the following command:
npx mocha .\customers.test.js --timeout=30000

You will see that the customer will be deleted

Run Full Script

Now remove “.only” from the previous test cases and remove “skip” from the “before” hook

Execute by the following command:
npx mocha .\customers.test.js --timeout=30000

From the above picture, we see that all our 7 test cases has been passed.

Generate Report

install following package

npm i shelljs mochawesome

Create a file named mochawesome.js and write following code:

const shell = require('shelljs');

const addParams = process.argv;
let file = '';
if (addParams[2] === 'file' && addParams[3]) {
    file += addParams[3];
}
shell.exec(`npx mocha --timeout=90000 ${file} --retries=3 --reporter mochawesome --reporter-options reportDir=Reports,reportFilename=report.html`)

Then go to package.json and update the following line under scripts:
"test": "node .\\mochawesome.js"

Now run the script by following command:

npm test file .\customers.test.js

A report folder has been generated.

And the output is :

Open the Report folder by “Reveal in File Explorer”

Then open the report.html file

Here is the full project:
https://github.com/salmansrabon/axios-api-test-demo

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 *