Cypress is one of the most popular tools for test automation. Initially, it was created to implement unit and integration tests, however, it has quickly gained strength as one of the most used tools for functional tests.
What makes Cypress such a versatile tool is its architecture, which allows direct access to applications, browsers, DOM, system, and network traffic. This last point has allowed Cypress to be used for API testing as well.
Cypress works at the network layer level; it has access to read and alter web traffic. It also comes with a custom command that allows you to send requests to specific endpoints.
Cy.request
This custom command makes HTTP requests quick and easy to implement. It takes the following arguments:
- URL (string): the first argument that this function receives is the URL to which the request will be made.
- Body (string, Object): optional, depending on the type of request, it will carry a body that is the information that will be sent in the request.
- Method: The type of request to make. If none is specified, Cypress uses GET by default.
The request returns the response as an Object Literal that contains several properties such as: Status, Headers, and Body, among others.
A request can be as simple as this:
- If no method is specified, Cypress uses GET as the default method.
- If you want to specify the method, this must be the first argument and the second argument must be the URL.
- The third argument will be the body of the request.
An example of how we can implement a POST request with an object as body:
Multiple Attributes
Cypress allows us to send an object as an argument in the cy.request method, in case we need to send more options, such as specific headers.
Validating the Response of a Request
If we want to validate the response of a request, it is necessary to perform this validation within the .then() function. Then, using Chai (which is already integrated into Cypress) the necessary validations are carried out.
Before making the decision to use Cypress to automate API tests, it is important to understand that Cypress, regardless of the type of tests to be executed, will always be executed by opening a browser. This can mean unnecessary use of resources each time the tests are run.
Advantages of Performing API Tests with Cypress:
Cypress comes with “batteries included”, that is, it comes with all the necessary libraries and integrations to carry out the tests, send the requests, and do the validations, so to start creating the tests we only have to install Cypress and start coding.
Cypress has been created to have excellent usability, this includes a fairly complete debugger that shows us all the information about the request made and the responses in a fairly simple way.
If you are already using Cypress for functional or end-to-end testing, it may be useful to use the same tool for API testing as well.
About Encora
At Encora, we’re interested in learning more about your software testing projects.
Encora offers fast-growing tech companies tangible business value by setting you up for growth with experienced software testing processes, tools, metrics, talent, and communication.
Contact us to schedule a consultation.