App Actions pattern in Cypress - Build an Automation Script
class LoginActions { enterUsername(username) { cy.get('#username').clear().type(username); } enterPassword(password) { cy.get('#password').clear().type(password); } clickLogin() { cy.get('#loginBtn').click(); } } class DashboardActions { verifyUrl() { cy.url().should('include', '/dashboard'); } verifyWelcomeMessage(username) { cy.get('#welcomeMsg').should('contain.text', `Welcome, ${username}!`); } } describe('Login and Dashboard Test using App Actions pattern', () => { const login = new LoginActions(); const dashboard = new DashboardActions(); it('should log in and display welcome message', () => { cy.visit('/login'); login.enterUsername('testuser'); login.enterPassword('Password123!'); login.clickLogin(); dashboard.verifyUrl(); dashboard.verifyWelcomeMessage('testuser'); }); });
The code defines two classes: LoginActions and DashboardActions. Each class groups related UI actions and assertions, following the App Actions pattern to keep tests clean and maintainable.
LoginActions has methods to enter username, enter password, and click the login button. These methods use Cypress commands to interact with the page elements by their IDs.
DashboardActions has methods to verify the URL contains '/dashboard' and to check the welcome message text includes the username.
The test case visits the login page, uses the login object to perform login steps, then uses the dashboard object to verify the expected results.
This structure makes the test easy to read and maintain, separating UI interactions from assertions.
Now add data-driven testing with 3 different user credentials to verify login and welcome message for each user