Test Overview
This test demonstrates how using external test data in Cypress improves maintainability by separating test logic from data. It verifies that the login form accepts valid credentials loaded from an external JSON file.
This test demonstrates how using external test data in Cypress improves maintainability by separating test logic from data. It verifies that the login form accepts valid credentials loaded from an external JSON file.
import { describe, it, beforeEach } from 'cypress'; describe('Login form with external test data', () => { let credentials; beforeEach(() => { cy.fixture('loginData.json').then((data) => { credentials = data; }); cy.visit('/login'); }); it('logs in successfully with valid credentials', () => { cy.get('input[name="username"]').type(credentials.username); cy.get('input[name="password"]').type(credentials.password); cy.get('button[type="submit"]').click(); cy.url().should('include', '/dashboard'); cy.get('h1').should('contain.text', 'Welcome'); }); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Load external test data from fixture file 'loginData.json' | Test data object with username and password is loaded into variable | — | PASS |
| 2 | Open browser and navigate to '/login' page | Login page is displayed with username and password fields and submit button | — | PASS |
| 3 | Find username input field and type username from external data | Username field contains the typed username | — | PASS |
| 4 | Find password input field and type password from external data | Password field contains the typed password | — | PASS |
| 5 | Find and click the submit button | Form is submitted, page starts navigation | — | PASS |
| 6 | Verify URL includes '/dashboard' after login | Browser URL is '/dashboard' | URL contains '/dashboard' | PASS |
| 7 | Verify page header contains text 'Welcome' | Page shows header with text 'Welcome' | Header text includes 'Welcome' | PASS |
cy.fixture() to load external test data files.cy.fixture('data.json').then((data) => { ... }) syntax loads and uses the data asynchronously.cy.fixture('user.json').then((user) => {
cy.get('#username').type(user.name);
cy.get('#password').type(user.password);
cy.get('#login').click();
cy.contains(user.welcomeMessage).should('be.visible');
});user.json file changes the welcomeMessage text?welcomeMessage changes in user.json, the test expects the new message in the app.cy.fixture('data.json').then((data) => {
cy.get('#email').type(data.email);
cy.get('#submit').click();
cy.contains('Thank you, ' + data.name).should('be.visible');
});data.name is undefined. What is the most likely cause?data.name is undefined, the JSON file likely lacks the name key.cy.fixture() loads JSON correctly in Cypress.data.json file does not have a name property. -> Option Bcy.fixture() in tests. -> Option DPass the test and this pattern moves unknown