POM with Page Factory

What is Page Factory?

It is also known as Page Factory Design Pattern (Enhanced POM). Page Factory is an inbuilt Page Object Model concept for Selenium WebDriver.

Here we also follow the concept of separation of page classes and test methods but with the help of PageFactory class, here we use @FindBy annotations to find web elements. We use initElements method to initialize web elements. Once we call initElements() method, all elements will get initialized. PageFactory.initElements() static method takes the driver instance of the given class and the class type, and returns a Page Object with its fields fully initialized.

@FindBy can accept tagName, partialLinkText, name, linkText, id, css, className, xpath as attributes.

Let’s look at the same Orange HRM login example using Page Factory.

Step 1:

Create a class for Orange HRM Login Page with PageFactory

package pageObjectModel;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class PageFactory_OrangeHRMLoginPage {

	WebDriver driver;

	@FindBy(xpath = "//input[@id='txtUsername']")
	WebElement userName;

	@FindBy(id = "txtPassword")
	WebElement password;

	@FindBy(id = "btnLogin")
	WebElement loginButton;

	// Creating an constructor
	public PageFactory_OrangeHRMLoginPage(WebDriver driver) {
		this.driver = driver;
		// This initElements method will create all WebElements
		PageFactory.initElements(driver, this);
	}

	// Create user actions

	// Set or type user name in textbox
	public void typeUserName(String UN) {
		userName.sendKeys(UN);
	}

	// Return a value from user name
	public void getUserNameValue() {
		userName.getAttribute("value");
	}

	// Set or type password in textbox
	public void typePassWord(String PW) {
		password.sendKeys(PW);
	}

	// Click on login button
	public void clickLogin() {
		loginButton.click();
	}
}

Step 2:

Create a class for Orange HRM Home Page with PageFactory

package pageObjectModel;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class PageFactory_OrangeHRMHomePage {
	
	WebDriver driver;
	
	@FindBy(xpath = "//a[@id='welcome']")
	WebElement homePageUserName;
	
	// Creating an constructor
		public PageFactory_OrangeHRMHomePage(WebDriver driver) {
			this.driver = driver;
			// This initElements method will create all WebElements
			PageFactory.initElements(driver, this);
		}
		
		// Get the User name from Home Page
		public String getHomePageDashboardUserName() {
			return homePageUserName.getText();
		}
}

Step 3:

Creating Simple Orange HRM POM Test case

package pageObjectModel;

import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;

public class PageFactory_OrangeHRMLoginTestCase {
	WebDriver driver;
	PageFactory_OrangeHRMLoginPage login;
	PageFactory_OrangeHRMHomePage homePage;

	@Test

	public void loginTestCase() {

		System.setProperty("webdriver.chrome.driver","F:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		driver.get("https://opensource-demo.orangehrmlive.com/index.php/auth/login");

		login = new PageFactory_OrangeHRMLoginPage(driver);

		// 1.Enter the user name
		login.typeUserName("admin");

		// 2.Enter the pass word
		login.typePassWord("admin123");

		// 3.Click on login button and go to home page
		login.clickLogin();

		// 4.Verify home page
		homePage = new PageFactory_OrangeHRMHomePage(driver);
		Assert.assertEquals(homePage.getHomePageDashboardUserName(), "Welcome Admin");
		
		driver.close();
	}
}

Leave a comment

Design a site like this with WordPress.com
Get started