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
Practice
App Actions pattern in Cypress tests?Solution
Step 1: Understand the purpose of App Actions pattern
The pattern groups common test steps into reusable methods to avoid repetition.Step 2: Identify the benefit in test code
This grouping makes tests cleaner and easier to maintain by reusing code.Final Answer:
It groups test steps into reusable methods to keep tests clean and maintainable. -> Option DQuick Check:
Reusable methods = cleaner tests [OK]
- Thinking App Actions generate test data automatically
- Confusing App Actions with test assertions
- Assuming App Actions speed up test execution
Solution
Step 1: Identify valid JavaScript function syntax
const login = () => { cy.get('#user').type('admin'); cy.get('#pass').type('1234'); cy.get('#submit').click(); } uses a valid arrow function assigned to a constant, which is common in Cypress helper files.Step 2: Check other options for syntax errors
function login() { cy.get('#user').type('admin'); cy.get('#pass').type('1234'); cy.get('#submit').click(); } is a function declaration but not exported or assigned; cy.login = () => { cy.get('#user').type('admin'); cy.get('#pass').type('1234'); cy.get('#submit').click(); } incorrectly assigns to cy object; login() { cy.get('#user').type('admin'); cy.get('#pass').type('1234'); cy.get('#submit').click(); } is invalid syntax.Final Answer:
const login = () => { cy.get('#user').type('admin'); cy.get('#pass').type('1234'); cy.get('#submit').click(); } -> Option AQuick Check:
Arrow function assigned to const = correct syntax [OK]
- Using invalid function syntax without const or arrow
- Attaching methods directly to cy object incorrectly
- Omitting function keyword or arrow in definitions
const addItem = (name) => { cy.get('#item-input').type(name); cy.get('#add-button').click(); }What will be the result of this test code?
addItem('Book'); cy.get('#item-list').should('contain', 'Book');Solution
Step 1: Understand what addItem does
It types the given name into the input and clicks the add button, triggering UI update.Step 2: Analyze the assertion
The test checks if '#item-list' contains the text 'Book', which should be true after adding.Final Answer:
The test will pass if 'Book' appears in the item list after adding. -> Option BQuick Check:
UI updates with added item = test passes [OK]
- Assuming missing return causes failure
- Confusing input placeholder with typed value
- Expecting addItem to return a value
const login = (user, pass) => { cy.get('#username').type(user); cy.get('#password').type(pass); cy.get('#login-btn').click }What is the error and how to fix it?
Solution
Step 1: Check method chaining syntax
Theclickcommand is missing parentheses, so it is not called.Step 2: Fix the click command
Add parentheses to call the function:click()to perform the click action.Final Answer:
Missing parentheses after click; fix by changing tocy.get('#login-btn').click()-> Option AQuick Check:
Method calls need parentheses [OK]
- Forgetting parentheses on Cypress commands
- Changing selectors without verifying correctness
- Thinking return is needed for Cypress commands
Solution
Step 1: Understand the need for reusability and verification
Looping over the array and callingaddItemreuses existing code and ensures each item is added properly.Step 2: Verify all items appear after adding
After adding, asserting all items appear confirms the UI updated correctly for each item.Final Answer:
Create an addItems method that loops over the array, calls addItem for each, then asserts all items appear. -> Option CQuick Check:
Loop + reuse + verify = best practice [OK]
- Typing all items at once without separate adds
- Writing many separate tests instead of one reusable method
- Assuming one click adds multiple items automatically
Ready to prove it?
Pass the test and this pattern moves unknown
