Test Overview
This test checks that a network request is sent with the correct URL and HTTP method when a button is clicked. It verifies the request properties to ensure the app behaves as expected.
This test checks that a network request is sent with the correct URL and HTTP method when a button is clicked. It verifies the request properties to ensure the app behaves as expected.
describe('Request properties test', () => { it('should send a GET request to /api/data when button clicked', () => { cy.intercept('GET', '/api/data').as('getData'); cy.visit('https://example.com'); cy.get('#load-data-btn').click(); cy.wait('@getData').its('request').then((req) => { expect(req.method).to.equal('GET'); expect(req.url).to.include('/api/data'); }); }); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | cy.intercept sets up a spy for GET requests to /api/data and aliases it as 'getData' | No page loaded yet, intercept ready to capture matching requests | — | PASS |
| 2 | cy.visit opens the page at https://example.com | Browser shows the example.com homepage with a button #load-data-btn visible | — | PASS |
| 3 | cy.get finds the button with id 'load-data-btn' and clicks it | Button clicked, triggering a network request to /api/data | — | PASS |
| 4 | cy.wait waits for the intercepted request '@getData' to complete | Request to /api/data captured by intercept | Verify request method is 'GET' and URL includes '/api/data' | PASS |
cy.intercept in Cypress tests?cy.intercept rolecy.intercept is used to monitor or stub network requests in Cypress tests.cy.intercept callback?expect(value).to.equal(expected).cy.intercept('POST', '/api/login', (req) => {
expect(req.body).to.have.property('username', 'admin')
})req.body for a property named 'username' with value 'admin'.cy.intercept('GET', '/api/data', (req) => {
expect(req.headers['Content-Type']).to.equal('application/json')
})req.headers object. The code uses 'Content-Type' which is incorrect; it should be 'content-type'.req is proper for request interception./api/submit contains a JSON body with a non-empty email property and a header Authorization starting with 'Bearer '. Which is the best way to write this assertion inside cy.intercept?cy.intercept('POST', '/api/submit', (req) => {
expect(req.body.email).to.be.a('string').and.not.be.empty
expect(req.headers.authorization).to.match(/^Bearer /)
}) asserts email is a non-empty string using to.be.a('string').and.not.be.empty, which is precise.cy.intercept('POST', '/api/submit', (req) => {
expect(req.body.email).to.be.a('string').and.not.be.empty
expect(req.headers.authorization).to.match(/^Bearer /)
}) uses regex to.match(/^Bearer /) to ensure header starts with 'Bearer ', which is exact and case-sensitive.cy.intercept('POST', '/api/submit', (req) => {
expect(req.body.email).to.exist
expect(req.headers.authorization).to.include('Bearer')
}) uses correct lowercase header key, but .to.exist only checks existence (could be empty or falsy), and .to.include checks substring anywhere, not prefix.cy.intercept('POST', '/api/submit', (req) => {
assert.isString(req.body.email)
assert.include(req.headers.authorization, 'Bearer')
}) uses non-standard assert style and misses non-empty check; include not prefix.cy.intercept('POST', '/api/submit', (req) => {
expect(req.body.email).to.be.empty
expect(req.headers.authorization).to.equal('Bearer')
}) incorrectly expects empty email and exact 'Bearer' match only.Pass the test and this pattern moves unknown