Page Object Model (POM) and Page Factory in Selenium


The Page Object Model (POM) is a popular design pattern used in Selenium WebDriver to create maintainable and reusable automated tests. The POM involves creating a separate class for each web page in the application under test, with each class encapsulating the details of the page structure and behavior.

PageFactory is a class in Selenium WebDriver that allows you to initialize Page Objects and their associated web elements in a more streamlined and efficient way. It is a useful tool when implementing the Page Object Model (POM) design pattern in your test automation framework.

With PageFactory, you can define your web elements using the @FindBy annotation within the Page Object class. This annotation specifies the method of locating the web element, such as ID, name, class, CSS selector, or XPath. You can also use the @CacheLookup annotation to cache the web element in memory for faster access.

Here are the main components of the POM:

  1. Page Object Class: Each web page in the application is represented by a separate Page Object class, which contains the web elements and methods associated with that page. For example, you might create a LoginPage class for the login page of the application.

  2. Web Elements: In each Page Object class, you define the web elements on that page using the @FindBy annotation. This annotation allows you to locate the web element using various locators such as ID, name, class, CSS selector, or XPath.

  3. Page Methods: The Page Object class also contains methods that represent the actions that can be taken on that page. These methods encapsulate the functionality of the page, such as logging in or submitting a form.

  4. Page Navigation: The Page Object classes may also contain methods for navigating to other pages within the application, such as clicking a link or navigating to a new URL.

  5. Page object constructors: These are used to initialize the web elements of the page or section, typically using the PageFactory.initElements() method.

  6. Test Code: In your test code, you create an instance of the Page Object for the page you want to interact with. This can be done using the PageFactory class in Selenium. You then call the methods on the Page Object to perform the desired actions on the page.

Here is an example of a Page Object class for a login page:

import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.PageFactory; public class LoginPage { private WebDriver driver; @FindBy(id = "username") private WebElement usernameField; @FindBy(id = "password") private WebElement passwordField; @FindBy(id = "login-button") private WebElement loginButton; 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 clickLoginButton() { loginButton.click(); } }

In this example, we have defined a LoginPage class that represents the login page of a website. It contains three page object elements for the username field, password field, and login button. We have also defined three page object methods that allow us to enter the username and password and click the login button. Finally, we have defined a constructor that initializes the web elements of the page using PageFactory.initElements().

Using this POM-based page class, we can write more readable and maintainable test code. Here is an example of a test case that uses the LoginPage class:

import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class LoginTest { public static void main(String[] args) { // Initialize Chrome driver System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); // Navigate to login page driver.get("https://www.example.com/login"); LoginPage loginPage = new LoginPage(driver); // Enter username and password loginPage.enterUsername("myusername"); loginPage.enterPassword("mypassword"); // Click login button loginPage.clickLoginButton(); // Verify that the user is logged in // Close the browser driver.quit(); } }

With PageFactory, you no longer need to use the findElement() method in your test code to locate the web elements. Instead, you can simply call the methods defined in the Page Object class, which will use the web element annotations to locate and interact with the elements.

Using PageFactory can simplify your test code and make it easier to maintain, especially when working with complex web pages with many elements. It also promotes better code organization and reusability when implementing the POM design patter 

By using the POM, you can create more modular and maintainable test code, because changes to the page structure can be easily made in the Page Object class without affecting the test code. Additionally, the POM can make your test code more readable and easier to understand, because the actions being taken on the page are encapsulated in descriptive method names.

No comments:

Post a Comment

Tips to improve Selenium Code

Intro to Selenium Selenium is an open-source testing framework used for automating web browsers. It allows testers to write scripts in vario...