What is the purpose of asserting request properties in Cypress tests?
Asserting request properties helps verify that the network requests sent by the application have the correct method, URL, headers, and body. This ensures the app communicates properly with the backend.
Click to reveal answer
beginner
How do you intercept a network request in Cypress to assert its properties?
Use cy.intercept() to spy or stub a request. Then use cy.wait() with the alias to get the intercepted request and assert its properties like method, url, headers, or body.
Click to reveal answer
intermediate
Example: How to assert that a POST request has a JSON body with a specific field?
After intercepting and waiting for the request, use expect(request.body).to.have.property('fieldName', 'expectedValue') to check the JSON body contains the field with the right value.
Click to reveal answer
beginner
Why is it important to alias intercepted requests in Cypress?
Aliasing allows you to wait for specific requests and access their details easily. This helps in writing clear assertions about the request properties after the app triggers them.
Click to reveal answer
beginner
What Cypress command lets you check the HTTP method of an intercepted request?
You can check the method by accessing request.method inside cy.wait('@alias').then(({ request }) => { expect(request.method).to.equal('POST') })
Click to reveal answer
Which Cypress command is used to intercept network requests for assertion?
Acy.intercept()
Bcy.request()
Ccy.visit()
Dcy.get()
✗ Incorrect
cy.intercept() is used to spy on or stub network requests, enabling assertions on request properties.
How do you wait for an intercepted request to complete before asserting its properties?
Acy.request('@alias')
Bcy.get('@alias')
Ccy.wait('@alias')
Dcy.intercept('@alias')
✗ Incorrect
cy.wait('@alias') waits for the intercepted request with the given alias to complete.
What property would you check to verify the HTTP method of a request in Cypress?
Arequest.headers
Brequest.url
Crequest.body
Drequest.method
✗ Incorrect
The HTTP method (GET, POST, etc.) is stored in request.method.
Which assertion checks that a request body contains a field named 'username' with value 'alice'?
C. Headers keys are case-insensitive; 'Content-Type' should be 'content-type'
D. The intercept callback must use res instead of req
Solution
Step 1: Check header key case sensitivity
HTTP headers are case-insensitive, but Cypress normalizes header keys to lowercase in the req.headers object. 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, and req is proper for request interception.
Final Answer:
Headers keys are case-insensitive; 'Content-Type' should be 'content-type' -> Option C
5. You want to assert that a POST request to /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?
hard
A. 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 /)
})
B. cy.intercept('POST', '/api/submit', (req) => {
expect(req.body.email).to.exist
expect(req.headers.authorization).to.include('Bearer')
})
C. cy.intercept('POST', '/api/submit', (req) => {
assert.isString(req.body.email)
assert.include(req.headers.authorization, 'Bearer')
})
D. cy.intercept('POST', '/api/submit', (req) => {
expect(req.body.email).to.be.empty
expect(req.headers.authorization).to.equal('Bearer')
})
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 /)
}) asserts email is a non-empty string using to.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 regex to.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-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.
Final Answer:
expect(req.body.email).to.be.a('string').and.not.be.empty expect(req.headers.authorization).to.match(/^Bearer /) -> Option A
Quick Check:
Use precise type and regex assertions for properties [OK]
Hint: Use type checks and regex for precise property assertions [OK]
Common Mistakes:
Ignoring case sensitivity in headers
Checking only existence, not content
Using wrong assertion style inconsistently
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.