0
0
Cypresstesting~20 mins

API-first setup pattern in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
API-first Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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');
  });
});
ATest fails: 'this.userId' is undefined causing visit URL to be incorrect.
BTest fails: cy.request is asynchronous and test runs before user creation.
CTest fails: API request returns 404 causing assertion failure.
DTest passes: user is created via API, profile page loads with name visible.
Attempts:
2 left
💡 Hint
Remember how Cypress aliases and 'this' context work inside tests.
assertion
intermediate
1: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?
});
Aexpect(response.body.id).to.be.undefined;
Bexpect(response.body.id).to.exist.and.to.be.a('string').and.to.have.length.greaterThan(0);
Cexpect(response.body).to.have.property('id', null);
Dexpect(response.status).to.equal(404);
Attempts:
2 left
💡 Hint
Check that the user ID exists and is a non-empty string.
🔧 Debug
advanced
2: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');
});
AThe 'this.userId' is accessed before the alias is set because 'before' hook is asynchronous.
BThe API endpoint '/api/users' is incorrect causing undefined response body.
CThe test is missing a 'cy.wait()' causing timing issues.
DThe 'cy.wrap' does not create an alias accessible via 'this'.
Attempts:
2 left
💡 Hint
Consider how Cypress handles asynchronous commands and test context.
framework
advanced
1: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?
Acy.intercept() to mock API responses without real data creation.
Bcy.visit() to load the UI and create data manually.
Ccy.request() to call backend API endpoints directly.
Dcy.wait() to delay tests until data is ready.
Attempts:
2 left
💡 Hint
Think about how to create real data via backend before UI tests.
🧠 Conceptual
expert
2: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.
AIt creates test data quickly and reliably via backend APIs, reducing flaky UI setup steps.
BIt avoids the need for any UI tests by testing APIs only.
CIt allows tests to run without any backend server by mocking all API calls.
DIt requires less coding because UI interactions create all test data automatically.
Attempts:
2 left
💡 Hint
Think about speed, reliability, and test flakiness.