DEV Community

Cover image for Page Model Design Pattern: Writing maintainable End-to-End Test
Darasimi-Ajewole
Darasimi-Ajewole

Posted on

Page Model Design Pattern: Writing maintainable End-to-End Test

End-to-End (E2E) testing is a crucial part of the software development lifecycle, ensuring that all components of an application work seamlessly together. However, as applications grow in complexity, maintaining E2E tests becomes challenging. The Page Model Design Pattern is an excellent solution to address this challenge, providing a structured and maintainable approach to E2E test architecture.

Understanding the Page Model Design Pattern

The Page Model Design Pattern is an architectural pattern that encourages the creation of a model for each web page or component in the application. This model encapsulates the interactions and elements on that specific page, making it easier to manage and maintain E2E tests.

How to Create End-to-End Tests Using the Page Model Design Pattern

Step 1: Page Model Creation
Begin by creating a page model for each significant page or component in your application. This model should include methods to interact with the elements on that page. For example:

 // Example Page Model for a Login Page (loginPage.ts)
import { Selector, t } from 'testcafe';
import DashboardPage from './dashboardPage';

export default class LoginPage {
    private usernameInput: Selector = Selector('input').withText('username');
    private passwordInput: Selector =  Selector('input').withText('password');
    private loginButton: Selector = Selector('button').withText('login');

    async enterCredentials(username: string, password: string): Promise<void> {
        await t.typeText(this.usernameInput, username)
               .typeText(this.passwordInput, password);
    }

    async clickLoginButton(): Promise<void> {
        await t.click(this.loginButton);
    }

    async login(username: string, password: string): Promise<DashboardPage> {
        await this.enterCredentials(username, password);
        await this.clickLoginButton();
        return new DashboardPage();
    }
}

// Example Page Model for a Dashboard Page (dashboardPage.ts)
import { Selector } from 'testcafe';

export default class DashboardPage {
    private welcomeMessage: Selector = Selector('[data-id="welcome-message"]');

    async getWelcomeMessage(): Promise<string> {
        return await this.welcomeMessage.innerText;
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Test Implementation

With the page models in place, writing tests becomes more straightforward and focused. Tests can now leverage the methods provided by the page models. For example:

 // Example E2E Test Using Page Models (e2eTest.ts)
import { Selector } from 'testcafe';
import LoginPage from './page-models/loginPage';
import DashboardPage from './page-models/dashboardPage';

fixture`Login Feature`.page`https://your-app-url.com`;

test('Successful Login and Navigate to Dashboard', async (t) => {
    const loginPage = new LoginPage();

    // Log in and get the DashboardPage instance
    const dashboardPage: DashboardPage = await loginPage.login('your_username', 'your_password');

    // Assertion for a successful login
    await t.expect(dashboardPage.getWelcomeMessage()).contains('Welcome');

    // Add additional actions on the DashboardPage if needed
    // For example: await dashboardPage.someAction();
});

Enter fullscreen mode Exit fullscreen mode

Benefits of Page Model Design Pattern

  • Modularity: Each page or component has its own model, promoting modularity and making it easier to update tests when the UI changes.
  • Readability: Tests become more readable and expressive, as the interaction with elements is encapsulated within page models.
  • Reusability: Page models can be reused across multiple tests, reducing duplication and promoting a DRY (Don't Repeat Yourself) approach.
  • Maintainability: When the UI changes, updates are confined to the corresponding page model, minimizing the impact on the entire test suite.

As your applications expand, this pattern ensures your E2E tests remain strong and adaptable, consequently improving the overall quality of your software. Happy Testing!

Top comments (0)