The App Actions pattern helps organize test steps into clear, reusable actions. It makes tests easier to read and maintain.
App Actions pattern in Cypress
Introduction
Syntax
Cypress
class AppActions { login(username, password) { cy.get('#username').type(username) cy.get('#password').type(password) cy.get('button[type="submit"]').click() } addItemToCart(itemName) { cy.contains(itemName).click() cy.get('#add-to-cart').click() } } // Usage in test const app = new AppActions() app.login('user1', 'pass123') app.addItemToCart('Book')
Define actions as methods inside a class or object.
Use Cypress commands inside these methods to perform UI steps.
Examples
Cypress
class AppActions { logout() { cy.get('#logout').click() } }
Cypress
class AppActions { search(term) { cy.get('#search-box').type(term) cy.get('#search-button').click() } }
Sample Program
This test uses the App Actions pattern to login and add an item to the cart. It checks the URL after login and verifies the cart contains the item.
Cypress
class AppActions { login(username, password) { cy.get('#username').type(username) cy.get('#password').type(password) cy.get('button[type="submit"]').click() } addItemToCart(itemName) { cy.contains(itemName).click() cy.get('#add-to-cart').click() } } describe('Shopping Cart Tests', () => { const app = new AppActions() it('should login and add a book to the cart', () => { cy.visit('/login') app.login('user1', 'pass123') cy.url().should('include', '/dashboard') app.addItemToCart('Book') cy.get('#cart-items').should('contain', 'Book') }) })
Important Notes
Keep actions focused on one task each for clarity.
Use meaningful method names to describe the action.
Update actions if UI selectors or flows change to keep tests working.
Summary
App Actions pattern groups test steps into reusable methods.
This makes tests cleaner and easier to maintain.
Use it to simplify common tasks like login or adding items.
Practice
1. What is the main benefit of using the
App Actions pattern in Cypress tests?easy
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]
Hint: Remember: App Actions = reusable test steps [OK]
Common Mistakes:
- Thinking App Actions generate test data automatically
- Confusing App Actions with test assertions
- Assuming App Actions speed up test execution
2. Which of the following is the correct way to define an App Action method for logging in a user in Cypress?
easy
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]
Hint: Use arrow functions assigned to const for App Actions [OK]
Common Mistakes:
- Using invalid function syntax without const or arrow
- Attaching methods directly to cy object incorrectly
- Omitting function keyword or arrow in definitions
3. Given this App Action method:
What will be the result of this test code?
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');medium
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]
Hint: Add item then check list contains it [OK]
Common Mistakes:
- Assuming missing return causes failure
- Confusing input placeholder with typed value
- Expecting addItem to return a value
4. This App Action method is intended to log in a user:
What is the error and how to fix it?
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?
medium
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]
Hint: Always add () to call Cypress commands like click [OK]
Common Mistakes:
- Forgetting parentheses on Cypress commands
- Changing selectors without verifying correctness
- Thinking return is needed for Cypress commands
5. You want to create an App Action to add multiple items from an array and verify all appear in the list. Which is the best approach?
hard
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]
Hint: Loop array, reuse addItem, then assert all present [OK]
Common Mistakes:
- Typing all items at once without separate adds
- Writing many separate tests instead of one reusable method
- Assuming one click adds multiple items automatically
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
