Page Object Model in Cypress - Build an Automation Script
/// <reference types="cypress" /> // cypress/support/pageObjects/LoginPage.js class LoginPage { visit() { cy.visit('https://example.com/login'); } fillUsername(username) { cy.get('#username').clear().type(username); } fillPassword(password) { cy.get('#password').clear().type(password); } clickLogin() { cy.get('button[type="submit"]').click(); } } export default LoginPage; // cypress/integration/login_spec.js import LoginPage from '../support/pageObjects/LoginPage'; describe('Login Test using Page Object Model', () => { const loginPage = new LoginPage(); it('should login successfully with valid credentials', () => { loginPage.visit(); loginPage.fillUsername('testuser'); loginPage.fillPassword('Test@1234'); loginPage.clickLogin(); cy.url().should('eq', 'https://example.com/dashboard'); cy.contains('h1', 'Welcome, testuser!').should('be.visible'); }); });
The LoginPage class encapsulates all selectors and actions related to the login page. This keeps test code clean and easy to maintain.
The visit() method opens the login page URL. The fillUsername() and fillPassword() methods enter the credentials into the input fields. The clickLogin() method clicks the login button.
In the test file, we create an instance of LoginPage and call these methods in order. After clicking login, we assert that the URL is the dashboard URL and that the welcome heading is visible.
This structure follows the Page Object Model pattern, making tests easier to read and maintain.
Now add data-driven testing with 3 different sets of username and password inputs using the Page Object Model.
Practice
Page Object Model in Cypress testing?Solution
Step 1: Understand the role of Page Object Model
The Page Object Model (POM) organizes web page elements and actions into classes to reuse code easily.Step 2: Identify the main benefit in Cypress tests
Using POM makes tests easier to write, read, and maintain by grouping page logic separately from test logic.Final Answer:
To organize page interactions into reusable classes -> Option DQuick Check:
Page Object Model = reusable page classes [OK]
- Thinking POM speeds up browser loading
- Believing POM removes need for assertions
- Assuming POM avoids selectors entirely
button.submit?Solution
Step 1: Recognize class method syntax in JavaScript
Inside a class, methods are defined asmethodName() { ... }without thefunctionkeyword or arrow syntax.Step 2: Match the correct syntax for Cypress command
The method should callcy.get('button.submit').click();inside the method body.Final Answer:
clickSubmit() { cy.get('button.submit').click(); } -> Option AQuick Check:
Class method syntax = clickSubmit() { cy.get('button.submit').click(); } [OK]
- Using arrow functions inside class methods incorrectly
- Adding 'function' keyword inside class method
- Using object literal syntax inside class
class LoginPage {
visit() {
cy.visit('/login')
}
enterUsername(name) {
cy.get('#username').type(name)
}
enterPassword(pass) {
cy.get('#password').type(pass)
}
submit() {
cy.get('button[type=submit]').click()
}
}What will happen when the test calls:
const page = new LoginPage(); page.visit(); page.enterUsername('user1'); page.enterPassword('pass1'); page.submit();Solution
Step 1: Analyze method calls in sequence
The test creates a LoginPage object and calls methods to visit the page, type username and password, then click submit.Step 2: Confirm Cypress commands are correct
Selectors like#username,#password, andbutton[type=submit]are valid and commands are chained properly.Final Answer:
The login page loads, username and password are typed, and submit button is clicked -> Option AQuick Check:
Method calls execute Cypress commands in order [OK]
- Thinking Cypress commands need async/await
- Assuming selectors are invalid without checking
- Believing only visit() runs without chaining
login(username, password) {
cy.get('#user').type(username)
cy.get('#pass').type(password)
cy.get('button.login').click()
}But your test calls
cy.visit('/login') before login(), yet fails because the username input is not found. What is the likely problem?Solution
Step 1: Identify cause of element not found error
If the username input is not found, the selector#usermight be wrong or changed on the page.Step 2: Evaluate other options
While missingcy.visit()can cause failure, the question states the failure is specifically about the username input not found, pointing to selector issue.Final Answer:
The selector#useris incorrect or outdated -> Option BQuick Check:
Element not found = wrong selector [OK]
- Assuming cy.find() fixes selector issues
- Thinking click() must come before typing
- Ignoring the need to visit page first
Solution
Step 1: Understand POM best practices
Best practice is to create a class that groups page elements and actions into clear, reusable methods with good selectors.Step 2: Compare options
Writing all steps in tests or using global variables reduces reusability and clarity. One big method reduces flexibility and readability.Final Answer:
Create a class with methods for each field interaction and a submit method, using clear selectors -> Option CQuick Check:
Clear methods + selectors = POM best practice [OK]
- Putting all steps directly in tests
- Using global selectors without encapsulation
- Combining all actions into one big method
Ready to prove it?
Pass the test and this pattern moves unknown
