CodeceptJS API documentation

CodeceptJS API documentation

Introduction

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


Steps:

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 user can Declare the primary URL here.{

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

Insert Assertion

In order to use assertion for proper validation, users must use codeceptjs-chai package for better using 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 looks like this,

{ "helpers": {
    "ChaiWrapper" : {
      "require": "codeceptjs-chai"
    }
  }
}

Getting started with Methods

In order to Test API users need to send requests and all we know 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()


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: 'xya@gmail.com',
  timezone: 'Asia/Dhaka',
  uuid: '232433',
  allow_premium: true,
  subscribed: true
}

This is a sample Response. In order to access data, user need to store the whole response in a variable. Lets 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.


To view or add a comment, sign in

Others also viewed

Explore content categories