Test Overview
This test sends a GET request to a sample API endpoint and verifies that the response body contains the expected data.
This test sends a GET request to a sample API endpoint and verifies that the response body contains the expected data.
describe('API Response Body Assertion', () => { it('should verify the response body contains expected user data', () => { cy.request('https://jsonplaceholder.typicode.com/users/1') .then((response) => { expect(response.status).to.equal(200); expect(response.body).to.have.property('id', 1); expect(response.body).to.have.property('username', 'Bret'); }); }); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Cypress test runner initialized | — | PASS |
| 2 | Sends GET request to https://jsonplaceholder.typicode.com/users/1 | Request sent, waiting for response | — | PASS |
| 3 | Receives response with status 200 and JSON body | Response received with user data | Check response.status equals 200 | PASS |
| 4 | Asserts response body has property 'id' with value 1 | Response body contains user object | response.body.id === 1 | PASS |
| 5 | Asserts response body has property 'username' with value 'Bret' | Response body contains user object | response.body.username === 'Bret' | PASS |
| 6 | Test ends successfully | All assertions passed | — | PASS |
name equal to "John" in Cypress?cy.request() returns a response object. To assert a property in the body, use its('body') to access it.should('have.property', key, value) for assertionshould('have.property', 'name', 'John').cy.request('/api/data').then((response) => {
expect(response.status).to.eq(200)
expect(response.body.items).to.have.length(3)
})response.status to equal 200, so the server must respond with HTTP 200.response.body.items to have exactly 3 elements. If it has more or less, the test fails.success is true:
cy.request('/api/submit').then((response) => {
expect(response.body.success).to.be.true()
})to.be.true is a property, not a function, so it should not have parentheses.response.body.success is valid if the property exists.id equal to 5 and a roles array containing "admin". Which Cypress assertion correctly checks both conditions?should with a callback allows multiple assertions on the body object.expect(body.id).to.eq(5) checks the id correctly. expect(body.roles).to.include('admin') checks the roles array contains 'admin'.Pass the test and this pattern moves unknown