How to handle API testing with Cypress?

Best Cypress Training Course Institute in Hyderabad

In the fast-evolving landscape of web development and quality assurance, Cypress testing has become a game-changer in the world of front-end automation testing. For those aspiring to master Cypress and launch a successful career in software testing, Quality Thought stands out as the best Cypress training institute in Hyderabad.

Whether you're a graduate, postgraduate, someone with an education gap, or a job domain changer, Quality Thought has designed a career-transforming Cypress course that meets your unique need

in Cypress, fixtures are external pieces of static data that you can use during your tests. They are usually stored in the cypress/fixtures folder as JSON, CSV, or text files, and allow you to separate test data from your test scripts. This makes your tests more organized, reusable, and easier to maintain.

For example, instead of hardcoding login credentials or user information inside your test, you can store them in a fixture file like user.json and reference it whenever needed. This is especially useful for testing with multiple sets of data, API responses, or mock data.

1. Using cy.request() for Direct API Calls

  • cy.request() is used to send GET, POST, PUT, DELETE requests directly to an API endpoint.

  • It bypasses the UI and directly interacts with the backend.

Example: GET request

describe('API Testing with Cypress', () => { it('Fetches users list', () => { cy.request('GET', 'https://reqres.in/api/users?page=2') .then((response) => { expect(response.status).to.eq(200) // Validate status code expect(response.body.data).to.have.length(6) // Validate response body }) }) })

Example: POST request

it('Create a new user', () => { cy.request('POST', 'https://reqres.in/api/users', { name: 'Siva', job: 'QA Engineer' }).then((response) => { expect(response.status).to.eq(201) expect(response.body).to.have.property('name', 'Siva') }) })

🔹 2. Using cy.intercept() to Spy/Stub API Calls

  • cy.intercept() helps you monitor or mock API responses while interacting with the UI.

  • Useful for validating network requests or stubbing data to test different scenarios.

Example: Spying on API call

it('Spies on GET users API', () => { cy.intercept('GET', '/api/users*').as('getUsers') cy.visit('https://reqres.in') // Your app that calls the API cy.wait('@getUsers').then((interception) => { expect(interception.response.statusCode).to.eq(200) }) })

Example: Stubbing response

it('Stubs user data', () => { cy.intercept('GET', '/api/users*', { statusCode: 200, body: { data: [{ id: 1, name: 'Stubbed User' }] } }).as('stubUsers') cy.visit('/users') cy.wait('@stubUsers') cy.contains('Stubbed User').should('be.visible') })

🔹 3. Best Practices

  • Separate test data: Keep API request payloads in fixtures/ for reusability.

  • Use assertions wisely: Check status codes, headers, and body properties.

  • Chain API + UI tests: Example — create a user with API, then verify in UI.

  • Environment config: Store base URLs and tokens in cypress.config.js or environment variables.

Comments

Popular posts from this blog

What is a test script?

Where can I find the best Cypress course near me?

Is there any Cypress internship program in Hyderabad?