We check request properties to make sure our app sends the right data to the server. This helps catch mistakes early.
Asserting request properties in Cypress
cy.intercept('METHOD', 'URL', (req) => { expect(req.body.property).to.equal('expectedValue') })
Use cy.intercept to watch network requests.
Inside the callback, use expect to check request details.
cy.intercept('POST', '/api/login', (req) => { expect(req.body.username).to.equal('user123') })
cy.intercept('GET', '/api/data', (req) => { expect(req.headers).to.have.property('authorization') })
cy.intercept('PUT', '/api/user/*', (req) => { expect(req.method).to.equal('PUT') })
This test watches the POST request to '/api/submit'. It checks that the request body has the name 'Alice' and that age is a number. The test fills a form and submits it to trigger the request.
describe('Test request properties', () => { it('checks POST request body', () => { cy.intercept('POST', '/api/submit', (req) => { expect(req.body.name).to.equal('Alice') expect(req.body.age).to.be.a('number') }).as('submitRequest') cy.visit('/form') cy.get('input[name="name"]').type('Alice') cy.get('input[name="age"]').type('30') cy.get('form').submit() cy.wait('@submitRequest') }) })
Always give your intercepts an alias with .as() to wait for them easily.
Assertions inside cy.intercept callbacks run when the request happens.
Use expect from Chai for clear, readable checks.
Use cy.intercept to watch network requests in Cypress.
Assert request properties inside the intercept callback with expect.
Check body, headers, method, or any request detail to ensure correctness.
Practice
cy.intercept in Cypress tests?Solution
Step 1: Understand
cy.interceptrolecy.interceptis used to monitor or stub network requests in Cypress tests.Step 2: Identify correct purpose
It helps watch and control HTTP requests, not UI or styling tasks.Final Answer:
To watch and control network requests during test execution -> Option AQuick Check:
cy.intercept = watch/control requests [OK]
- Confusing intercept with UI manipulation
- Thinking intercept generates test data
- Mixing intercept with CSS styling
cy.intercept callback?Solution
Step 1: Identify correct assertion syntax
Cypress uses Chai assertions likeexpect(value).to.equal(expected).Step 2: Match method and case sensitivity
HTTP methods are uppercase strings, so 'POST' must be exact.Final Answer:
expect(req.method).to.equal('POST') -> Option DQuick Check:
expect().to.equal() with exact case [OK]
- Using lowercase 'post' instead of 'POST'
- Using assert syntax instead of expect
- Checking wrong method like 'GET'
cy.intercept('POST', '/api/login', (req) => {
expect(req.body).to.have.property('username', 'admin')
})Solution
Step 1: Analyze the assertion target
The assertion checksreq.bodyfor a property named 'username' with value 'admin'.Step 2: Understand request vs response and headers
It does not check URL, response, or headers, only the request body.Final Answer:
The request body must include a username property with value 'admin' -> Option BQuick Check:
expect(req.body).to.have.property('username', 'admin') [OK]
- Confusing request body with URL query
- Checking response instead of request
- Looking at headers instead of body
cy.intercept('GET', '/api/data', (req) => {
expect(req.headers['Content-Type']).to.equal('application/json')
})Solution
Step 1: Check header key case sensitivity
HTTP headers are case-insensitive, but Cypress normalizes header keys to lowercase in thereq.headersobject. The code uses'Content-Type'which is incorrect; it should be'content-type'.Step 2: Validate other parts
Method GET is valid, assertion syntax is correct, andreqis proper for request interception.Final Answer:
Headers keys are case-insensitive; 'Content-Type' should be 'content-type' -> Option CQuick Check:
Cypress req.headers use lowercase keys [OK]
- Using wrong HTTP method
- Misusing response object instead of request
- Incorrect assertion syntax
/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?Solution
Step 1: Check email property assertion
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 /) })assertsemailis a non-empty string usingto.be.a('string').and.not.be.empty, which is precise.Step 2: Check Authorization header assertion
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 regexto.match(/^Bearer /)to ensure header starts with 'Bearer ', which is exact and case-sensitive.Step 3: Compare other options
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-standardassertstyle 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.Final Answer:
expect(req.body.email).to.be.a('string').and.not.be.empty
expect(req.headers.authorization).to.match(/^Bearer /) -> Option AQuick Check:
Use precise type and regex assertions for properties [OK]
- Ignoring case sensitivity in headers
- Checking only existence, not content
- Using wrong assertion style inconsistently
Ready to prove it?
Pass the test and this pattern moves unknown
