How to Use the By Class in Selenium Page Object Model (POM)

The By class in Selenium plays a critical role in identifying and interacting with web elements during automated testing. It provides a flexible way to store and manage locators in Selenium, making your test scripts cleaner and more maintainable. If you’re working with the Page Object Model (POM) design pattern, using the By class is highly recommended for organizing locators effectively.

In this blog post, we’ll discuss how to use the By class in Selenium within the POM structure and how it enhances test efficiency with the help of an example. If you don’t know or want to read the basics, please scroll down and read through. Basics of By class in selenium are written at the bottom.

Example Code for Using By Class in Selenium POM

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class LoginPage {

    WebDriver driver;

    // Locators using By class
    private By usernameField = By.id("username");
    private By passwordField = By.id("password");
    private By loginButton = By.xpath("//button[@type='submit']");
    
    // Constructor
    public LoginPage(WebDriver driver) {
        this.driver = driver;
    }

    // Method to enter username
    public void enterUsername(String username) {
        driver.findElement(usernameField).sendKeys(username);
    }

    // Method to enter password
    public void enterPassword(String password) {
        driver.findElement(passwordField).sendKeys(password);
    }

    // Method to click the login button
    public void clickLogin() {
        driver.findElement(loginButton).click();
    }
}

Explanation:

• The By class is used to define locators (usernameField, passwordField, loginButton) for the elements on the login page.

• The constructor of the class takes a WebDriver instance as a parameter, which is used to interact with the web elements.

• Each action on the web elements (e.g., entering text, clicking buttons) is encapsulated in methods (enterUsername, enterPassword, clickLogin) to improve readability and reusability.

What is the By Class in Selenium?

The By class is a part of Selenium’s API that allows you to locate web elements on a web page. It provides various static methods to identify elements by their attributes, such as:

  • By.id(): Locates elements by the id attribute.
  • By.name(): Locates elements by the name attribute.
  • By.xpath(): Locates elements using XPath expressions.
  • By.cssSelector(): Locates elements using CSS selectors.
  • By.linkText(): Locates elements by their link text.
  • By.partialLinkText(): Locates elements by partial match of the link text.
  • By.tagName(): Locates elements by their tag name.
  • By.className(): Locates elements by their class attribute.

Why Use the By Class in Selenium?

Using the By class offers several benefits for writing clean and maintainable test code:

  • Reusability: You can reuse the By locators across multiple test cases without repeating yourself, reducing redundancy.
  • Readability: Storing locators using the By class keeps your code clean and easy to understand.
  • Test Scalability: As your test suite grows, organizing locators with the By class makes it scalable and easier to manage.
  • Maintainability: When you use the Page Object Model, locators are stored in one place, making it easy to update them if the UI changes.

Using the By Class in Selenium Page Object Model (POM)

The Page Object Model (POM) is a design pattern that encourages separation of test logic and page elements. When using POM, all locators for a specific page are stored in a corresponding page class. Here’s how to use the By class to store locators within a POM structure. If you need to understand how to use this, please see the example above.


Discover more from Automation Script

Subscribe to get the latest posts sent to your email.

Related Posts