My goal is to land on a page
Ex: www.example.com
When I land on this page a particular API is hit, I want to intercept that API and store the json response in a variable and extract specific values from the response.
I have tried this code but it fails after waiting for 5 seconds:
describe('Intercept API response', () => {
it('Stores the response in a variable', () => {
let responseData;
cy.intercept('GET', 'https://jsonplaceholder.typicode.com/posts/1').as('getData');
cy.visit('https://www.example.com')
.then(() => {
cy.wait('@getData')
.then((xhr) => {
responseData = xhr.response.body;
console.log(responseData);
});
});
});
});