API-first setup pattern in Cypress - Build an Automation Script
describe('API-first setup pattern test', () => { it('creates a user via API and verifies UI shows it', () => { // Step 1: Create user via API cy.request({ method: 'POST', url: '/api/users', body: { name: 'Alice', email: 'alice@example.com' }, headers: { 'Content-Type': 'application/json' } }).then((response) => { expect(response.status).to.equal(201); expect(response.body).to.have.property('id'); }); // Step 2: Visit the users page cy.visit('/users'); // Step 3: Verify the user appears in the list cy.get('[data-test=user-list]').should('be.visible'); cy.get('[data-test=user-list]').contains('Alice').should('exist'); cy.get('[data-test=user-list]').contains('alice@example.com').should('exist'); }); });
This test uses Cypress to automate the manual test case.
First, it sends a POST request to the API to create a user named Alice. We check the response status is 201 to confirm success and that the response body contains an id, meaning the user was created.
Next, the test visits the /users page in the browser to check the UI.
Finally, it looks for the user list element using a data-test attribute, which is a best practice for stable selectors. It asserts that the list contains the name 'Alice' and the email 'alice@example.com'.
This approach uses the API to set up test data quickly and reliably, then verifies the UI shows the expected result, following the API-first setup pattern.
Now add data-driven testing with 3 different users created via API and verify each appears in the UI