15 Cypress API - biswajitsundara/Cypress GitHub Wiki
Cypress supports API testing as well and it helps greatly in developing end to end tests.
- We can perform the Http CRUD operation
- We can prepare the test data using back end API calls
- Then test the UI using UI automation script
Let's fetch data from the pokemon api using GET call and then validate the response body
describe('sample tests',()=>{
const apicall = (endPoint,abilitiesCount,statsCount) =>{
cy.request({
method: 'GET',
url: `https://pokeapi.co/api/v2/pokemon/${endPoint}`
}).then((response)=>{
console.log(JSON.stringify(response));
expect(response.body.abilities.length).equals(abilitiesCount);
expect(response.body.forms[0].name).equals(endPoint);
expect(response.body.stats.length).equals(statsCount);
})
}
it('pokeman test',()=>{
apicall("ditto", 2, 6);
apicall("bulbasaur", 2, 6);
apicall("ivysaur", 2, 6);
})
})
- Please note, the
cy.request()takes anObjectin the above example
//correct
cy.request({..});
//incorrect
cy.request(()=>{
})
- TODO: How to retry the API calls?