Selenium PageObject Model

Page Object Model in Selenium: A Comprehensive Guide

The Selenium Page Object model is a popular design pattern that is used to organize and maintain test code for web applications. It provides a way to encapsulate elements of a page and their actions in a single place, making it easier to create and maintain tests. In this article, we’ll take a closer look at the Page Object model, how it works, and how to use it effectively with Selenium.

Don’t miss the complete selenium with python training!

What is selenium page object model?

The Page Object model is a design pattern that was introduced as a way to improve the maintenance and readability of our Selenium tests. It does this by separating the test logic from the page elements and their actions in the project structure. This separation of concerns makes it easier to update and modify tests as the application changes, without having to rewrite the test code from scratch.

In the Page Object model, each page in the application is represented by a class, and the elements and actions on that page are represented by methods. For example, if you have a login page with a username field and a password field, you might create a LoginPage class with methods for entering the username and password, as well as a method for submitting the login form.

Here’s an example of what a LoginPage class might look like in Selenium using the Page Object model:

class LoginPage {
  private WebDriver driver;

  @FindBy(id = "username")
  private WebElement usernameField;

  @FindBy(id = "password")
  private WebElement passwordField;

  @FindBy(css = "button[type='submit']")
  private WebElement submitButton;

  public LoginPage(WebDriver driver) {
    this.driver = driver;
    PageFactory.initElements(driver, this);
  }

  public void enterUsername(String username) {
    usernameField.sendKeys(username);
  }

  public void enterPassword(String password) {
    passwordField.sendKeys(password);
  }

  public void submitForm() {
    submitButton.click();
  }
}

Sure! Here is a blog post article about the Selenium Page Object model:


The Selenium Page Object model is a popular design pattern that is used to organize and maintain test code for web applications, reducing code duplication. It provides a way to encapsulate elements of a page and their actions in a single place, making it easier to create and maintain tests. In this article, we’ll take a closer look at the Page Object model, how it works, and how to use it effectively with Selenium.

What is the selenium Page Object model?

The Page Object model is a design pattern that was introduced as a way to improve the maintenance and readability of test code. It does this by separating the test logic from the page elements and their actions. This separation of concerns makes it easier to update and modify tests as the application changes, without having to rewrite the test code from scratch.

In the Page Object model, each page in the test suite is represented by a class, and the elements and actions on that page are represented by methods. For example, if you have a login page with a username field and a password field, you might create a LoginPage class with methods for entering the username and password, as well as a method for submitting the login form.

Here’s an example of what a LoginPage class might look like in Selenium using the Page Object model:

class LoginPage {
  private WebDriver driver;

  @FindBy(id = "username")
  private WebElement usernameField;

  @FindBy(id = "password")
  private WebElement passwordField;

  @FindBy(css = "button[type='submit']")
  private WebElement submitButton;

  public LoginPage(WebDriver driver) {
    this.driver = driver;
    PageFactory.initElements(driver, this);
  }

  public void enterUsername(String username) {
    usernameField.sendKeys(username);
  }

  public void enterPassword(String password) {
    passwordField.sendKeys(password);
  }

  public void submitForm() {
    submitButton.click();
  }
}

In this example, the LoginPage class has three private fields that represent the username field, password field, and submit button on the login page. These fields are annotated with the @FindBy annotation, which tells Selenium how to locate the elements on the page.

The class also has three methods for entering the username and password, and submitting the form. These methods operate on the private fields and allow you to interact with the page elements in a simple and intuitive way.

Using the Page Object model with Selenium

Now that you have an understanding of the Page Object model, let’s take a look at how to use it with Selenium. To do this, you’ll need to create a Page Object class for each page in your application, as well as a separate test class for each test case.

Here’s an example of a test case that uses the LoginPage class we defined earlier:

class LoginTest {
  private WebDriver driver;
  private LoginPage loginPage;

  @BeforeMethod
  public void setUp() {
    driver = new ChromeDriver();
    loginPage = new LoginPage(driver);
  }

  @Test
  public void testSuccessfulLogin() {
    loginPage.enterUsername("user");
    loginPage.enterPassword("password");
    loginPage.submitForm();
    // Assert that the user is redirected to the home page after logging in
    assertEquals(driver.getCurrentUrl(), "http://myapp.com/home");
  }

  @AfterMethod
  public void tearDown() {
    driver.quit();
  }
}

In this example, the LoginTest class has a setUp method that is run before each test method, and an tearDown method that is run after each test method. The setUp method creates a new ChromeDriver instance and a new LoginPage instance, and the tearDown method quits the driver.

The testSuccessfulLogin method is a test case that attempts to log in to the application using the LoginPage class. It enters a username and password, and then submits the form. Finally, it uses an assertEquals statement to verify that the user is redirected to the home page after logging in.

Advantages of Page Object model

The Page Object model has several advantages that make it a popular choice for organizing and maintaining test code:

  • Improved maintainability: By separating the test logic from the page elements and their actions, the Page Object model makes it easier to update and modify tests as the application changes.
  • Improved readability: The Page Object model makes it easier to understand the intent of a test case, as the test logic is separated from the page elements and their actions. This makes the test code more readable and easier to debug.
  • Re usability: The Page Object model promotes reuse of code, as the same Page Object class can be used by multiple test cases. This can save a lot of time and effort, especially in large test suites.
  • Reduced duplication: The Page Object model helps reduce duplication of test code, as the same page elements and their actions are encapsulated in a single class. This can improve the reliability of your tests, as there is less room for human error.

Why Page Object Model is important?

In conclusion, the Selenium Page Object model is a powerful design pattern that can help you create and maintain automated tests for web applications. By separating the test logic from the page elements and their actions, it promotes better code reuse, readability, and maintainability. With the Page Object model in place, you can create a more scalable and reliable test suite that is easier to update and modify as your application evolves.

Leave a Comment

Scroll to Top