Bird
Raised Fist0
Cypresstesting~10 mins

Asserting request properties in Cypress - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to assert the request method is GET.

Cypress
cy.intercept('/api/data').as('getData');
cy.wait('@getData').its('request.method').should('[1]', 'GET');
Drag options to blanks, or click blank then click option'
Aeq
Bequals
Cis
Dequal
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'equals' or 'equal' instead of 'eq' causes assertion failure.
Using 'is' is not a valid Cypress assertion.
2fill in blank
medium

Complete the code to assert the response status code is 200.

Cypress
cy.intercept('/api/data').as('getData');
cy.wait('@getData').its('response.statusCode').should('[1]', 200);
Drag options to blanks, or click blank then click option'
Aequal
Bbe
Cequals
Deq
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'equals' or 'equal' instead of 'eq' causes assertion failure.
Using 'be' alone is not sufficient for equality check.
3fill in blank
hard

Fix the error in the code to assert the request URL includes '/api/data'.

Cypress
cy.intercept('/api/data').as('getData');
cy.wait('@getData').its('request.url').should('[1]', '/api/data');
Drag options to blanks, or click blank then click option'
Acontain
Bincludes
Cinclude
Dcontains
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'contain' or 'contains' causes assertion failure.
Using 'includes' is not a valid Cypress assertion.
4fill in blank
hard

Fill both blanks to assert the request headers contain 'content-type' and its value is 'application/json'.

Cypress
cy.intercept('/api/data').as('getData');
cy.wait('@getData').its('request.headers.[1]').should('[2]', 'application/json');
Drag options to blanks, or click blank then click option'
Acontent-type
Beq
Cequal
DcontentType
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'contentType' instead of 'content-type' causes failure.
Using 'equal' instead of 'eq' causes assertion failure.
5fill in blank
hard

Fill all three blanks to assert the response body has a property 'success' that is true.

Cypress
cy.intercept('/api/data').as('getData');
cy.wait('@getData').its('response.body.[1]').should('[2]', [3]);
Drag options to blanks, or click blank then click option'
Asuccess
Beq
Ctrue
Dbe
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'eq' with 'true' as string causes failure.
Using 'true' without quotes is correct here because it's a boolean.

Practice

(1/5)
1. What is the primary purpose of using cy.intercept in Cypress tests?
easy
A. To watch and control network requests during test execution
B. To create UI elements dynamically
C. To write CSS styles for the application
D. To generate random test data automatically

Solution

  1. Step 1: Understand cy.intercept role

    cy.intercept is used to monitor or stub network requests in Cypress tests.
  2. Step 2: Identify correct purpose

    It helps watch and control HTTP requests, not UI or styling tasks.
  3. Final Answer:

    To watch and control network requests during test execution -> Option A
  4. Quick Check:

    cy.intercept = watch/control requests [OK]
Hint: Remember: intercept = watch network calls [OK]
Common Mistakes:
  • Confusing intercept with UI manipulation
  • Thinking intercept generates test data
  • Mixing intercept with CSS styling
2. Which of the following is the correct syntax to assert that a request method is POST inside a cy.intercept callback?
easy
A. assert(req.method == 'POST')
B. assert.equal(req.method, 'GET')
C. expect(req.method).equals('post')
D. expect(req.method).to.equal('POST')

Solution

  1. Step 1: Identify correct assertion syntax

    Cypress uses Chai assertions like expect(value).to.equal(expected).
  2. Step 2: Match method and case sensitivity

    HTTP methods are uppercase strings, so 'POST' must be exact.
  3. Final Answer:

    expect(req.method).to.equal('POST') -> Option D
  4. Quick Check:

    expect().to.equal() with exact case [OK]
Hint: Use expect().to.equal() with exact method case [OK]
Common Mistakes:
  • Using lowercase 'post' instead of 'POST'
  • Using assert syntax instead of expect
  • Checking wrong method like 'GET'
3. Given the following code snippet, what will the assertion check inside the intercept callback?
cy.intercept('POST', '/api/login', (req) => {
  expect(req.body).to.have.property('username', 'admin')
})
medium
A. The request headers must include username 'admin'
B. The request body must include a username property with value 'admin'
C. The response body must have username 'admin'
D. The request URL must contain 'username=admin'

Solution

  1. Step 1: Analyze the assertion target

    The assertion checks req.body for a property named 'username' with value 'admin'.
  2. Step 2: Understand request vs response and headers

    It does not check URL, response, or headers, only the request body.
  3. Final Answer:

    The request body must include a username property with value 'admin' -> Option B
  4. Quick Check:

    expect(req.body).to.have.property('username', 'admin') [OK]
Hint: Check if property is in req.body, not URL or headers [OK]
Common Mistakes:
  • Confusing request body with URL query
  • Checking response instead of request
  • Looking at headers instead of body
4. Identify the error in this Cypress intercept assertion:
cy.intercept('GET', '/api/data', (req) => {
  expect(req.headers['Content-Type']).to.equal('application/json')
})
medium
A. The assertion syntax is incorrect
B. The method should be POST, not GET
C. Headers keys are case-insensitive; 'Content-Type' should be 'content-type'
D. The intercept callback must use res instead of req

Solution

  1. 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'.
  2. Step 2: Validate other parts

    Method GET is valid, assertion syntax is correct, and req is proper for request interception.
  3. Final Answer:

    Headers keys are case-insensitive; 'Content-Type' should be 'content-type' -> Option C
  4. Quick Check:

    Cypress req.headers use lowercase keys [OK]
Hint: Cypress req.headers uses lowercase keys [OK]
Common Mistakes:
  • Using wrong HTTP method
  • Misusing response object instead of request
  • Incorrect assertion syntax
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

  1. 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.
  2. 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.
  3. 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.
  4. Final Answer:

    expect(req.body.email).to.be.a('string').and.not.be.empty
    expect(req.headers.authorization).to.match(/^Bearer /)
    -> Option A
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard