乐闻世界logo
搜索文章和话题

How to search through JSON response with Cypress assertions

2个答案

1
2

In end-to-end testing with Cypress, asserting JSON responses from API endpoints is a common and critical operation. I'll demonstrate this with a practical example.

Suppose we want to test an API that returns user details. The returned JSON structure is approximately as follows:

json
{ "id": 1, "name": "John Doe", "email": "john.doe@example.com", "roles": ["Admin", "User"] }

We need to verify that the returned data is accurate, including the user's id, name, email, and roles. Here are the steps to perform assertions using Cypress:

  1. Initiate API Request: First, use the cy.request() method to make a GET request.
javascript
cy.request('GET', '/api/users/1').as('userRequest');
  1. Assert Response Status Code: Confirm the request was successful; the status code should be 200.
javascript
cy.get('@userRequest').its('status').should('equal', 200);
  1. Assert Response Body: Next, validate the content of the response body.
javascript
cy.get('@userRequest').its('body').should('include', { id: 1, name: 'John Doe', email: 'john.doe@example.com' });
  1. Deeply Assert Arrays and Objects: Since the user's roles is an array, confirm it contains specific roles.
javascript
cy.get('@userRequest').its('body.roles').should('be.an', 'array').and('deep.eq', ['Admin', 'User']);

By doing this, we not only verify basic data types and structure but also deeply validate the exact content of complex types like arrays. Such assertions ensure API responses fully meet expectations, helping to improve application stability and user trust.

2024年6月29日 12:07 回复

When using Cypress for end-to-end testing, asserting the JSON response from an API is a common and critical operation. I'll demonstrate this with an example.

Let's assume we're testing an API that returns detailed user information. The JSON structure returned is approximately as follows:

json
{ "id": 1, "name": "John Doe", "email": "john.doe@example.com", "roles": ["Admin", "User"] }

We must confirm that the returned data is accurate, including the user's id, name, email, and roles. Here are the steps to assert using Cypress:

  1. Initiate API Request: First, we initiate a GET request using cy.request().

    javascript
    cy.request('GET', '/api/users/1').as('userRequest');
  2. Verify Response Status Code: Check if the request was successful, expecting a status code of 200.

    javascript
    cy.get('@userRequest').its('status').should('equal', 200);
  3. Verify Response Body: Next, we verify the content of the response body.

    javascript
    cy.get('@userRequest').its('body').should('include', { "id": 1, "name": "John Doe", "email": "john.doe@example.com" });
  4. Deeply Verify Arrays and Objects: As the user's roles are an array, we must verify that it contains the specified roles.

    javascript
    cy.get('@userRequest').its('body.roles').should('be.an', 'array').and('deep.eq', ['Admin', 'User']);

By doing this, we not only verify basic data types and structures but also deeply validate the exact content of complex types like arrays. These assertions ensure that the API response meets expectations, enhancing application stability and user trust.

2024年6月29日 12:07 回复

你的答案