Categories
Other

API Documentation CodeceptJs

Introduction

This document contains basic information about how to import Codeceptjs API Feature in codeceptjs framework. All the steps are Described in order,

Steps:

  1. REST

In order to Test API a new helper is needed

Example how to declare that, Inside helpers user need to initialize that, Here endpoint is the URL where users API will hit. So users can Declare the primary URL here.

{
  helpers: {
    REST: {
      endpoint: 'http://site.com/api',
      onRequest: (request) => {
      // request.headers.auth = '123';
    }
  }
}
  1. Insert Assertion

In order to use assertion for proper validation, users must use codeceptjs-chai package for better using the Asserts function. The command is, 

npm i codeceptjs-chai

After Using the command User need to define chai wrapper inside the codecept.conf file it should look like this,

{ 
  "helpers": {
    "ChaiWrapper" : {
      "require": "codeceptjs-chai"
    }
  }
}
  1. Getting started with Methods

In order to Test API users need to send requests and all we know is there are mainly Get, Post, Delete, Put, etc requests. Codeceptjs has built-in functions for handling these requests. Such as, with these following methods, users can get the response and can check the response they need.

I.sendGetRequest()
I.sendPostRequest()
I.sendDeleteRequest()
I.sendPutRequest()
  1. Use of assertions and Data accessing:

API’s are mainly returning Data in JSON format So as a tester, Asserting core elements by accessing it; is quite important. Here is a demo response after a successful get call,

{
  status: 200,
  statusText: 'OK',
  headers: {
    server: 'nginx/1.17.3',
    'content-type': 'application/json',
    'transfer-encoding': 'chunked',
    date: 'Tue, 31 Aug 2021 09:26:30 GMT',
    'x-ratelimit-limit': '150',
    'x-ratelimit-remaining': '149',
    'x-frame-options': 'SAMEORIGIN',
    'x-xss-protection': '1; mode=block',
    'x-content-type-options': 'nosniff'
  },
}
{
  id: 420,
  first_name: 'Alvi',
  last_name: 'Tazwar',
  email: '[email protected]',
  timezone: 'Asia/Dhaka',
  uuid: '232433',
  allow_premium: true,
  subscribed: true
}

This is a sample Response. In order to access data, the user needs to store the whole response in a variable. Let’s see an example,

const res = await I.sendPostRequest('/login', data);
await I.assertEqual(res.status, 200);
console.log(res.data);

Here if the user wants to check and assert the status. Then use dot(.) and add the attribute name for accessing it.


Leave a Reply

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