Cypress has grown in popularity as a powerful end-to-end testing framework for web applications due to its fast execution, real-time feedback, and robust API. Many teams use design patterns like the Page Object Model (POM) to further improve test automation. In this blog post, we’ll look at how to use Cypress to implement the Page Object Model for a scalable and maintainable testing infrastructure.
What is the Page Object Model (POM)?
The Page Object Model design pattern encourages code reuse, maintainability, and readability in automated tests. Each web page or component in POM is represented by a JavaScript class that encapsulates the page’s elements and related actions.
The Advantages of Using POM in Cypress:
POM allows you to create reusable components representing different pages or UI elements. This reduces redundancy and increases the modularity of the test code.
When the UI changes, you only need to update the corresponding page object rather than changing every test that interacts with that page.
Readability: Because it closely matches the application’s structure under test, POM makes your test code more readable and understandable.
Setting Up a Cypress Project:
Before diving into POM implementation, ensure Cypress is installed in your project. You can install Cypress using npm:
- Open Local Terminal/bash
- npm install cypress –save-dev.
- After installation, initialize Cypress with:
- npx cypress open
Creating Page Objects:
BasePage Object:
Create a base page object with common methods and properties shared across multiple pages. This can include navigation functions or common assertions:
class BasePage { launchApplication(){ cy.visit('https://opensource-demo.orangehrmlive.com/web/index.php/auth/login') }
LoginPage Object:
For each page in your application, create a corresponding page object class that extends the base page. Include page-specific elements and actions.
import BasePage from "../BasePage" const basePage = new BasePage() export LoginPage Login{ // Define page elements username=':nth-child(2) > .oxd-input-group > :nth-child(2) > .oxd-input' password=':nth-child(3) > .oxd-input-group > :nth-child(2) > .oxd-input' btn='.oxd-button' // Define page actions navigateURL(){ basePage.launchApplication() } enterUsernamePassword(){ cy.get(this.username).type('Admin') cy.get(this.password).type('admin123') } clickButton(){ cy.get(this.btn).click() } }
Using Page Objects in Tests:
In your Cypress test files, import, and use the page objects to interact with the application. Here’s an example:
import{Login}from "../Pages/PomPage.cy" //Import Class const lp= new Login() //Create Object of Class it('Page Object Model Valid Credential', function() { lp.navigateURL() lp.enterUsernamePassword() lp.clickButton() } )
Now, it’s time to run our tests
- Execute this command to open Cypress Test Runner –
- npx cypress open
Conclusion:
Implementing the Page Object Model in Cypress can significantly improve the maintainability and scalability of your test suite. By organizing your test code into reusable and modular components, you’ll be better equipped to handle changes in your application while keeping your tests readable and efficient.
Remember to continuously update and refine your page objects as your application evolves, ensuring that your test suite remains a reliable asset in your development pipeline. With the Page Object Model, Cypress becomes even more powerful for building robust end-to-end tests for your web applications.
Happy Testing!