Photo by Sigmund on Unsplash

Member-only story

Troubleshooting an API Assertion in Cypress: Lesson Learned and Solution Provided

Anthony
3 min readFeb 23, 2024

This post is meant to be a quick one for anyone who may face the same challenge. A few days back I began meddling with APIs using Cypress. I could not for the life of me assert against anything inside of the response body.

The Goal

Long story short, using the API at automationexercise.com, my goal was to GET a list of products and assert that the responseCode was equal to 200.

The Cypress docs and plenty of online tutorials parse through JSON properties and objects using the below syntax.

https://docs.cypress.io/api/commands/request

In this example, we see that all we need to parse through the JSON is a response, body, the property name, and the value we expect.

Simple right? Not really.

My test was straightforward:

describe ('Products List API', () => {
context("GET /productsList", () => {

it("gets a list of products", () => {
cy.request("GET", "https://automationexercise.com/api/productsList").then((response) => {
expect(response.status).to.eq(200)…

--

--

Anthony
Anthony

Written by Anthony

Join me as I share general, technical, and practical knowledge in the QA field.

No responses yet