REST API Automation for WSO2 Identity Server with Cypress Test Framework.

Shanika Wickramasinghe
3 min readMay 19, 2020

Hi All,

When it comes to using Cypress, many users think about front-end testing. But we can use Cypress when writing e2e API tests. If there are REST APIs created in our application, automated API tests should be an integral part of the application’s e2e tests. Cypress allows us to write these two types of tests. It is undoubtedly convenient because everything is done using one tool and one programming language which is JavaScript.

Cypress is an exciting alternative to Selenium WebDriver, and the support of the community centered around this framework is invaluable.

Requirements

As prerequisites, you need to have below in your machine.

  • Install Node JS
  • Install npm
  • Install Cypress latest version
  • Install Visual Studio Code

Setting Up

  1. Install Cypress latest version with below command

npm install cypress — save-dev

For specific cypress version,

npm install — save-dev cypress@4.1.0

2. Create a Cypress Project with below Folder Structure

Inside Cypress/Integration create a folder as rest-api and create a javascript file there inside to maintain the test file with extension .specs.js

Inside Cypress/Integration/rest-api/rest-api-objects create a javascript file there inside to maintain the API request and the response validation as an Object in Cypress. The advantage of maintaining the request and response as an object here would be you get the chance to export the whole class as an object and use any of the methods defined there in any of your test spec files inside the whole project structure.

Lets Start Coding

Let's get Application Management Rest APIS API Get operation as a sample API to proceed with the automation

https://localhost:9443/api/server/v1/applications?limit=30&offset=0

Next let's check the CURL request related to this API

Super Tenant Flow

curl -X GET — user admin:admin “https://localhost:9443/t/carbon.super/api/server/v1/applications?limit=30&offset=0" -H “accept: application/json” -i -k

Tenant Flow

curl -X GET — user admintenant:admin “https://localhost:9443/t/test.com/api/server/v1/applications?limit=30&offset=0" -H “accept: application/json” -i -k

According to the above CURL request, there are major parts such as credentials, headers, request URL, parameters. With those, you can organize your request and response as an object in application-management-group-object.js file as below.

application-management-group-object.js

Next, You can have your application-management.specs.js file as below to pass the necessary data for the request object.

import applicationManagementRestapiObject from ‘./rest-api-objects/application-management-group-object.js’

describe(‘GET Applications with Application Management REST API’, function () {

const appmgtObject = new applicationManagementRestapiObject()

it (‘getApplication with attrbute limit and offset-dataAttr’+tenant.domain,function(){

appmgtObject.getApplicationsForTenantWithLimitAndOffset(tenant.uname, tenant.pword, tenant.domain, “limit=30&offset=0”)

})

})

Other than the above 2 files you can maintain a cypress.env.json file to define your environment variables. According to above sample you can define below environment variables there.

  • BASE_URL
  • TENANT_DOMAIN
  • SUPER_TENANT_DOMAIN
  • TENANT_USERNAME
  • TENANT_PASSWORD
  • SUPER_TENANT_USERNAME
  • SUPER_TENANT_PASSWORD

EG :- BASE_URL = “https://localhost:9443/ ”

This is how you can automate a REST API with WSO2 Identity Server using Cypress Automation Framework.

Hope you enjoyed the Article !! :)

--

--

Shanika Wickramasinghe

Senior Software Engineer and Freelance Technical Writer. I write about any Computer Science related topic. https://www.linkedin.com/in/shanikawickramasinghe