Here’s the code sample from our tutorial on how to make API requests with Cypress including fetching data from an endpoint and using it to validate page UI

 

describe("API", () => {
beforeEach(() => {
cy.visit('https://kitchen.applitools.com/ingredients/api');
});
it('should trigger an alert with a message', () => {
cy.request(`https://kitchen.applitools.com/api/recipes`).then((response) => {
const { data } = response.body;
cy.get('#recipes-list li').each(($el, index) => {
cy.wrap($el).find('h4').contains(data[index].title);
cy.wrap($el).find('img').should('have.attr', 'src', data[index].image);
});
});
});
});
view raw api.spec.js delivered with ❤ by emgithub

View on GitHub
Colby Fayock
Developer Advocate

Ready for the next generation of testing?

Get started today Schedule a demo