Challenge - 5 Problems
API-first Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the test result of this Cypress API setup code?
Consider this Cypress test code that uses API-first setup to create a user before UI tests. What will be the test execution result?
Cypress
describe('User Profile Test', () => { before(function() { cy.request('POST', '/api/users', { name: 'Alice' }).then((response) => { expect(response.status).to.eq(201); cy.wrap(response.body.id).as('userId'); }); }); it('loads user profile page', function() { cy.visit(`/users/${this.userId}`); cy.contains('Alice').should('be.visible'); }); });
Attempts:
2 left
💡 Hint
Remember how Cypress aliases and 'this' context work inside tests.
✗ Incorrect
The 'before' hook creates the user via API and saves the user ID as an alias. The test accesses it via 'this.userId' and visits the profile page. The API returns 201 status, so the test passes.
❓ assertion
intermediate1:30remaining
Which assertion correctly verifies API response in API-first setup?
You want to verify that the API response contains a user object with a non-empty 'id' field after creating a user. Which assertion is correct in Cypress?
Cypress
cy.request('POST', '/api/users', { name: 'Bob' }).then((response) => { // Which assertion below is correct? });
Attempts:
2 left
💡 Hint
Check that the user ID exists and is a non-empty string.
✗ Incorrect
Option B correctly asserts that the 'id' field exists, is a string, and is not empty. Other options check for undefined, null, or wrong status code.
🔧 Debug
advanced2:00remaining
Why does this API-first setup test fail intermittently?
This Cypress test sometimes fails with 'Cannot read property "id" of undefined'. What is the likely cause?
Cypress
before(function() { cy.request('POST', '/api/users', { name: 'Carol' }).then((response) => { cy.wrap(response.body.id).as('userId'); }); }); it('loads user profile', function() { cy.visit(`/users/${this.userId}`); cy.contains('Carol').should('be.visible'); });
Attempts:
2 left
💡 Hint
Consider how Cypress handles asynchronous commands and test context.
✗ Incorrect
The 'before' hook runs asynchronously but the test may start before the alias is set, causing 'this.userId' to be undefined intermittently.
❓ framework
advanced1:30remaining
Which Cypress command best supports API-first test setup?
In API-first testing, which Cypress command is best to create test data before UI tests?
Attempts:
2 left
💡 Hint
Think about how to create real data via backend before UI tests.
✗ Incorrect
cy.request() sends real HTTP requests to backend APIs to create data, which is the core of API-first setup. Other commands do not create real data.
🧠 Conceptual
expert2:00remaining
What is the main advantage of API-first setup pattern in end-to-end testing?
Choose the best explanation for why API-first setup is preferred in modern UI testing frameworks like Cypress.
Attempts:
2 left
💡 Hint
Think about speed, reliability, and test flakiness.
✗ Incorrect
API-first setup creates data directly via backend APIs, making tests faster and less flaky than relying on UI steps to create data. It complements UI tests rather than replacing them.