Cypress API Testing Basics and Setup

Apr 9, 2025

Lecture Notes: Cypress API Testing

Introduction

  • The lecture introduces a new series on Cypress API testing.
  • Cypress supports API testing along with web automation testing.

Environment Setup

  • Environment setup for API testing with Cypress is the same as web automation.
  • Review Part 2 of the web automation series for environment setup details.
  • Key components required:
    • Install Node.js
    • Install npm package
    • Install Visual Studio Code
    • Install Cypress package

Project Setup

  • Create a project folder named Cypress Automation.
  • Default folder structure created includes an e2e folder for specification files.
  • Create a new folder under e2e named API Testing for API test cases.

Writing and Executing HTTP Requests

  • Describe Block: Used for grouping test cases.
  • It Block: Used for writing individual test cases.

Handling GET Requests

  • Use Postman to identify API URL and expected responses.
  • In Cypress, use cy.request() for sending HTTP requests.
  • Validate the response status code using .its('status').should('equal', 200);.

Handling POST Requests

  • POST requests require sending a request body along with the URL.
  • Send POST request using cy.request() with method, URL, and body parameters.
  • Validate with .its('status').should('equal', 201); to check for successful creation.

Handling PUT Requests

  • Use PUT for updating existing records.
  • Define method, URL, and JSON body for the PUT request.
  • Validate using .its('status').should('equal', 200);.

Handling DELETE Requests

  • DELETE requests remove resources specified by the URL.
  • Use cy.request() with method and URL parameters.
  • Validate deletion with .its('status').should('equal', 200);.

Running Tests

  • Use terminal commands to run tests:
    • npx cypress run --spec "file-path" for specific file execution.
    • npx cypress open to run tests through the Cypress application interface.

Conclusion

  • Cypress can perform different types of HTTP requests without additional libraries.
  • Future videos will cover more in-depth topics such as response validation.

This lecture covers the basics of setting up Cypress for API testing and sending various HTTP requests. It includes practical steps for writing test cases and validating them using Cypress.