0
0
Cypresstesting~20 mins

Asserting request properties in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Request Property Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
assertion
intermediate
2:00remaining
Check HTTP method in intercepted request
You intercept a network request in Cypress and want to assert that the HTTP method used is POST. Which assertion correctly checks this property?
Cypress
cy.intercept('/api/data').as('getData');
cy.visit('/page');
cy.wait('@getData').its('request.method').should(/* your assertion here */);
A"contain", "POST"
B"include", "POST"
C"equal", "POST"
D"have.property", "POST"
Attempts:
2 left
💡 Hint
Use the assertion that checks exact equality of the HTTP method string.
Predict Output
intermediate
2:00remaining
Output of request header assertion
Given the following Cypress test code, what will be the test result if the intercepted request does NOT include the header 'Authorization'?
Cypress
cy.intercept('/api/user').as('getUser');
cy.visit('/profile');
cy.wait('@getUser').its('request.headers').should('have.property', 'authorization');
ATest passes successfully
BTest fails with an assertion error about missing 'authorization' property
CTest fails with a timeout error waiting for the request
DTest throws a syntax error
Attempts:
2 left
💡 Hint
Think about what happens when you assert a property that does not exist on an object.
🔧 Debug
advanced
2:00remaining
Identify the error in request body assertion
This Cypress code tries to assert that the intercepted request body contains a field 'userId' equal to 123. What is wrong with the assertion?
Cypress
cy.intercept('POST', '/api/orders').as('postOrder');
cy.visit('/order');
cy.wait('@postOrder').its('request.body').should('contain', { userId: 123 });
AThe 'request.body' is a string, so object comparison fails
BThe 'should' assertion is missing a callback function
CThe 'contain' assertion does not work with objects; use 'deep.include' instead
DThe alias '@postOrder' is not defined before use
Attempts:
2 left
💡 Hint
Check the data type of the request body in intercepted requests.
🧠 Conceptual
advanced
2:00remaining
Best practice for asserting JSON response properties
When asserting properties in a JSON response intercepted by Cypress, which approach is best to ensure the test is reliable and clear?
AUse 'expect(response.body.key).to.exist' inside a '.then()' callback
BUse 'should("contain", { key: value })' directly on the response body
CUse 'should("have.property", "key")' on the response body object
DUse 'should("include", { key: value })' on the entire response object
Attempts:
2 left
💡 Hint
Consider how Cypress handles asynchronous commands and object references.
framework
expert
3:00remaining
Correct Cypress code to assert multiple request properties
You want to assert that an intercepted request has method POST, includes a header 'Content-Type' with value 'application/json', and the JSON body contains a field 'active' set to true. Which Cypress code snippet correctly performs all these assertions?
A
cy.wait('@req').then(({ request }) => {
  expect(request.method).to.equal('POST');
  expect(request.headers['Content-Type']).to.equal('application/json');
  expect(request.body).to.include('active:true');
});
B
cy.wait('@req').its('request').should('have.property', 'method', 'POST')
  .and('have.property', 'headers').and('contain', { 'content-type': 'application/json' })
  .and('have.property', 'body').and('include', { active: true });
C
cy.wait('@req').should(({ request }) => {
  expect(request.method).to.equal('POST');
  expect(request.headers.contentType).to.equal('application/json');
  expect(request.body.active).to.be.true;
});
D
cy.wait('@req').then(({ request }) => {
  expect(request.method).to.equal('POST');
  expect(request.headers['content-type']).to.equal('application/json');
  const body = JSON.parse(request.body);
  expect(body.active).to.be.true;
});
Attempts:
2 left
💡 Hint
Remember header keys are case-insensitive but usually lowercase in Cypress. Also, parse JSON body before checking fields.