Data cleanup approaches in Cypress - Build an Automation Script
/// <reference types="cypress" /> // Custom command to login as admin Cypress.Commands.add('loginAsAdmin', () => { cy.visit('/login'); cy.get('[data-cy=email-input]').type('admin'); cy.get('[data-cy=password-input]').type('AdminPass!'); cy.get('[data-cy=login-button]').click(); cy.url().should('include', '/dashboard'); }); describe('Data cleanup: Delete test user', () => { before(() => { cy.loginAsAdmin(); }); it('Deletes user testuser123 from user management', () => { cy.visit('/user-management'); // Wait for search input and type username cy.get('[data-cy=user-search-input]').should('be.visible').type('testuser123'); // Click search button cy.get('[data-cy=user-search-button]').click(); // Wait for user row to appear cy.get('[data-cy=user-row-testuser123]').should('be.visible'); // Click delete button for the user cy.get('[data-cy=delete-user-testuser123]').click(); // Confirm deletion dialog appears cy.get('[data-cy=confirm-delete-dialog]').should('be.visible'); // Click confirm delete button cy.get('[data-cy=confirm-delete-button]').click(); // Verify user no longer appears in the list cy.get('[data-cy=user-row-testuser123]').should('not.exist'); }); });
The code starts by adding a custom Cypress command loginAsAdmin to reuse the login steps easily. This helps keep the test clean and avoids repeating login code.
In the test suite, the before hook logs in as admin once before running the test. This ensures the test starts with the right user context.
The test visits the user management page, waits for the search input to be visible, and types the username testuser123. It clicks the search button and waits for the user row to appear.
Then it clicks the delete button for that user, waits for the confirmation dialog, and confirms the deletion. Finally, it asserts that the user row no longer exists, confirming the user was deleted.
Selectors use data-cy attributes for stability and clarity. Explicit waits with should('be.visible') ensure elements are ready before interacting, avoiding flaky tests.
This approach follows best practices by using custom commands, explicit waits, and stable selectors to automate data cleanup reliably.
Now add data-driven testing to delete three different test users: 'testuser123', 'testuser456', and 'testuser789'.
Practice
Solution
Step 1: Understand test independence
Tests should not depend on data left by previous tests to avoid false results.Step 2: Recognize reliability benefits
Cleaning data ensures each test starts fresh, making results trustworthy.Final Answer:
It keeps tests independent and reliable -> Option CQuick Check:
Data cleanup = test independence and reliability [OK]
- Thinking cleanup slows tests
- Believing cleanup causes more failures
- Assuming cleanup removes assertions
Solution
Step 1: Identify hooks for cleanup timing
afterEachruns after every test, ideal for cleanup after each test.Step 2: Compare other hooks
beforeandbeforeEachrun before tests;afterruns once after all tests.Final Answer:
afterEach-> Option AQuick Check:
Cleanup after each test =afterEach[OK]
afterEach to clean after every test [OK]- Using
beforefor cleanup - Confusing
beforeEachwith cleanup hook - Using
afterwhich runs only once
after(() => {
cy.request('DELETE', '/api/test-data')
})Solution
Step 1: Understand
afterhook behaviorafterruns once after all tests in the suite complete.Step 2: Analyze the API request timing
The DELETE request runs only once after all tests, cleaning data once at the end.Final Answer:
Test data is cleaned once after all tests finish -> Option BQuick Check:
after= cleanup once after all tests [OK]
after runs once after all tests finish [OK]- Thinking
afterruns after each test - Confusing
afterwithafterEach - Assuming cleanup runs before tests
afterEach(() => {
cy.request('DELETE', '/api/test-data')
})But test data is not cleaned properly. What is the likely problem?
Solution
Step 1: Verify
afterEachhook validityafterEachis valid and commonly used for cleanup in Cypress.Step 2: Check API request issues
If data is not cleaned, the DELETE request may fail due to wrong URL or server issues.Final Answer:
The API endpoint URL might be incorrect or unavailable -> Option AQuick Check:
Failed cleanup often means API endpoint problem [OK]
- Thinking
afterEachis unsupported - Using wrong HTTP method assumptions
- Switching hooks unnecessarily
Solution
Step 1: Understand test data lifecycle
Creating data before each test and cleaning after each test keeps tests isolated and reliable.Step 2: Use API requests in hooks
API calls inbeforeEachandafterEachefficiently manage test data per test.Final Answer:
UsebeforeEachto create data andafterEachto delete data via API requests -> Option DQuick Check:
Setup and cleanup per test =beforeEach+afterEach[OK]
beforeEach and afterEach with API calls [OK]- Cleaning data only once before or after all tests
- Not using hooks for cleanup
- Manually deleting data inside tests
Ready to prove it?
Pass the test and this pattern moves unknown
