Intercepting API calls
Cypress lets users work with underlying API requests and stub responses where necessary. Let us analyze the API calls when loading the Packt Publishing home page and try to stub one of the responses. cy.intercept() is the command used to work with API calls, and it offers a wide variety of parameters. For this example, we will be using the routeMatcher and staticResponse arguments. We add a second it block to intercept the underlying API call and specify the type of HTTP call, URL, and a predefined response as parameters, as shown in Figure 5.20:
Figure 5.20 – cy.intercept call
The value of the static response parameter can be obtained using the Network tab of the developer tools to get the actual response for the API call. This is illustrated in Figure 5.21. By passing this in as the staticResponse parameter, the GET call on this URL will always return the stubbed response instead of the original: