Test Overview
This test verifies that after creating a new user via the UI, the test cleans up by deleting the user to keep the test environment clean.
This test verifies that after creating a new user via the UI, the test cleans up by deleting the user to keep the test environment clean.
describe('User creation and cleanup', () => { it('creates a user and deletes it after test', () => { // Visit user creation page cy.visit('/users/new') // Fill user form cy.get('[data-cy=username]').type('testuser123') cy.get('[data-cy=email]').type('testuser123@example.com') cy.get('[data-cy=submit]').click() // Verify user is created cy.contains('User created successfully').should('be.visible') // Cleanup: delete the created user cy.visit('/users') cy.contains('testuser123').parent().find('[data-cy=delete]').click() cy.on('window:confirm', () => true) // Confirm deletion // Verify user is deleted cy.contains('testuser123').should('not.exist') }) })
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Test runner initialized, browser ready | — | PASS |
| 2 | Browser opens and navigates to '/users/new' | User creation page loaded with form fields | URL contains '/users/new' | PASS |
| 3 | Find username input and type 'testuser123' | Username field filled with 'testuser123' | Input value is 'testuser123' | PASS |
| 4 | Find email input and type 'testuser123@example.com' | Email field filled with 'testuser123@example.com' | Input value is 'testuser123@example.com' | PASS |
| 5 | Find submit button and click | Form submitted, page processing | — | PASS |
| 6 | Verify success message 'User created successfully' is visible | Success message displayed on page | Message 'User created successfully' is visible | PASS |
| 7 | Navigate to '/users' page to manage users | Users list page loaded | URL contains '/users' | PASS |
| 8 | Find user row containing 'testuser123' and click delete button | Delete confirmation dialog appears | Delete button found for user 'testuser123' | PASS |
| 9 | Confirm deletion in browser dialog | User deletion processed | — | PASS |
| 10 | Verify user 'testuser123' no longer exists in users list | User 'testuser123' removed from list | User 'testuser123' is not found on page | PASS |
afterEach runs after every test, ideal for cleanup after each test.before and beforeEach run before tests; after runs once after all tests.afterEach -> Option AafterEach [OK]afterEach to clean after every test [OK]before for cleanupbeforeEach with cleanup hookafter which runs only onceafter(() => {
cy.request('DELETE', '/api/test-data')
})after hook behaviorafter runs once after all tests in the suite complete.after = cleanup once after all tests [OK]after runs once after all tests finish [OK]after runs after each testafter with afterEachafterEach(() => {
cy.request('DELETE', '/api/test-data')
})afterEach hook validityafterEach is valid and commonly used for cleanup in Cypress.afterEach is unsupportedbeforeEach and afterEach efficiently manage test data per test.beforeEach to create data and afterEach to delete data via API requests -> Option DbeforeEach + afterEach [OK]beforeEach and afterEach with API calls [OK]Pass the test and this pattern moves unknown