API Automation with NodeJs using mocha and chai
In this tutorial we are going to automate REST API with NodeJs using mocha and chai.
Prerequisite :
Before dive into this tutorial, you must have knowledge of
- NodeJS : Node.js is an open-source, cross-platform, JavaScript run-time environment that executes JavaScript code outside of a browser.
- Mocha : Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases.
- Chai : Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework
Let's start...........
Problem statement:
GET service
- https://jsonplaceholder.typicode.com/posts
- verify that status code is 200 , verify the schema and verify that API returns at least 100 records
- https://jsonplaceholder.typicode.com/posts/1
- verify that status code is 200 , verify the schema ,verify that API returns only one record and verify that id in response matches the input (1)
- https://jsonplaceholder.typicode.com/invalidposts
- verify that status code is 404 and log the complete request and response details for troubleshooting
POST service
- https://jsonplaceholder.typicode.com/posts
body : { "title": "foo", "body": "bar", "userId": 1 } and send body as string
- verify that status code is 201 and verify the schema - verify the record created
PUT service
- https://jsonplaceholder.typicode.com/posts/1
body : { "id": 1, "title": "abc", "body": "xyz", "userId": 1 } and send body as JSON
- verify that status code is 200 , verify the schema and verify the record updated
DELETE service
- https://jsonplaceholder.typicode.com/posts/1
- verify that status code is 200 and verify the response
Note: for all requests use following headers content/type = application/json charset = UTF-8
Let's Setup Our Project First........
Code Editor : any code editor, I personally use VS code. You can use Atom, sublime text , Brackets etc as well. Download VS code here.
NodeJs : Assuming you have already NodeJs installed in your system. If not install it from here. Verify the installation using below command in terminal.
node -v
This will tell you NodeJs version installed in your system.
Create a project folder in your system , say api_automation_example
Navigate to your folder and create a file called package.json using
npm init
content of which looks like something like below.
{ "name": "api_automation_automation", "version": "1.0.0", "description": "Rest API Automation", "main": "test.js", "author": "Dhruv", "license": "ISC", "dependencies": { }, "devDependencies": { "npm": "^6.4.1", }, "scripts": { "test": ""echo \"Error: no test specified\" && exit 1"" } }
Install mocha and chai using
npm install mocha && chai --save
Request Promise
Request is designed to be the simplest way possible to make http calls. It supports HTTPS and follows redirects by default. But we will be using the simplified HTTP request client 'request' with Promise support. Install it using
npm install request-promise --save
Mochawesome
Mochawesome is a custom reporter for use with the Javascript testing framework, mocha. It runs on Node.js (>=8) and works in conjunction with mochawesome-report-generator to generate a standalone HTML/CSS report to help visualize your test runs.
npm i mochawesome --save
Logger
Its library used to logging any info, error warning etc.
npm i log4js --save
Let's start with automating Get Service.
Below is the list of what all things we need to keep our minds before writing code.
Method : whether it is GET, POST , PUT or DELETE etc.
Body : Type of body , JSON or XML etc
Header e.g content/type = application/json
Http Server request-promise in our case
We are done with our project setup. Solution for above problem statement has been uploaded on GitHub. You can find it here
Do reach out to me if you want to know more about it or have any concern.
A Programming Enthusiast | startup and stuff
5 年Its something new I have learnt today, thanks dhruv. Keep writing such an amazing post.
Hands on Technologist, Leader, Mentor, Strategist, Team Player
5 年doing good?
Senior Software Engineer @Lowe's | Senior Full Stack Developer | Java | Angular
5 年Good work Dhruv
Associate Director - Analytics & BI
5 年Good one...