
API Testing With Nightwatch JS
Nightwatch JS is a popular automated testing framework that can be used to test APIs, web applications, and other software systems. In this article, we will cover how to use Nightwatch JS to test a JSON REST API.
Let's assume that we have a JSON REST API that returns a list of users. The API has the following endpoints:
/users: returns a list of all users
/users/:id: returns a specific user by ID
Our goal is to test these endpoints using Nightwatch JS.
To start, we need to install Nightwatch JS and its dependencies. We can do this by running the following command in the terminal:
npm install nightwatch axios --save-dev
Next, we need to create a Nightwatch JS configuration file. We can do this by creating a nightwatch.conf.js file in the project root directory. This file should contain the following code:
module.exports = {
"src_folders": ["tests"],
"test_settings": {
"default": {
"globals": {
"axios": require("axios")
}
}
}
};
This configuration file sets the source folder for our tests to tests, and makes the axios library available to our tests.
Next, we can create a test file in the tests folder. We will call this file api.js. This file should contain the following code:
module.exports = {
"List all users": function (browser) {
browser.globals.axios.get("http://localhost:8080/users")
.then(response => {
browser.assert.equal(response.status, 200);
browser.assert.equal(response.data.length, 2);
browser.assert.equal(response.data[0].name, "John");
browser.assert.equal(response.data[1].name, "Jane");
browser.end();
})
.catch(error => {
console.error(error);
browser.end();
});
},
"Get user by ID": function (browser) {
browser.globals.axios.get("http://localhost:8080/users/1")
.then(response => {
browser.assert.equal(response.status, 200);
browser.assert.equal(response.data.name, "John");
browser.assert.equal(response.data.age, 30);
browser.end();
})
.catch(error => {
console.error(error);
browser.end();
});
}
};
This test file defines two test cases, one for each of our API endpoints. The first test case, List all users, sends a GET request to the /users endpoint and checks that the response status is 200, the response contains two users with the correct names, and ends the test. The second test case, Get user by ID, sends a GET request to the /users/1 endpoint (assuming that user with ID 1 is "John") and checks that the response status is 200, the response contains the correct name and age for the user, and ends the test.
We can now run this test using the following command:
bash Copy code nightwatch tests/api.js This will run the tests using the axios library to send HTTP requests to the specified URLs. If the tests pass, we will see a message indicating that the tests have passed. If the tests fail, we will see an error message indicating the reason for the failure.
In this example, we used Nightwatch JS to test a JSON REST API. By testing each endpoint separately, we can ensure that each endpoint is functioning as expected and that our API is providing accurate and reliable data to our users.
Testing APIs is an important part of the software development lifecycle. By using an automated testing framework like Nightwatch JS, we can ensure that our APIs are functioning correctly and providing accurate data to our users. In this article, we showed how to use Nightwatch JS to test specific JSON REST API endpoints. By following these examples and adapting them to your specific use case, you can ensure that your API is reliable and secure for your users.
More Posts
Blocking Ad Traffic In Nightwatch JS

Example showing how you can block unwanted ad traffic in your Nightwatch JS tests....
Blocking Ad Traffic In Cypress

Example showing how you can block unwanted ad traffic in your Cypress tests....
Three Ways To Resize The Browser In Nightwatch

Outlining the three different ways to resize the browser in Nightwatch JS with examples....
Happy Path VS Sad Path Testing

As a test engineer it is crucial that both happy path and sad path use cases have been considered and fully tested...