Page Object Model is a design pattern which has become popular in test automation for enhancing test maintenance and reducing code duplication. A page object is an object-oriented class that serves as an interface to a page of your AUT.
Page Object model is an object design pattern in Selenium, where web pages are represented as classes, and the various elements on the page are defined as variables on the class.
Page Object Model is a Design Pattern which has become popular in Selenium Test Automation. Page object model (POM) can be used in any kind of framework such as modular, data-driven, keyword driven, hybrid framework etc.
The Page Factory Class is an extension to the Page Object design pattern. It is used to initialize the elements of the Page Object or instantiate the Page Objects itself. Annotations for elements can also be created (and recommended) as the describing properties may not always be descriptive enough to tell one object from the other.
Page Factory is an inbuilt page object model concept for Selenium Web Driver, but it is much optimized. Page Factory can be used in any kind of framework such as Data Driven, Modular or Keyword Driven. Page Factory gives more focus on how the code is being structured to get the best benefit out of it.
Page Object is a class that represents a web page and hold the functionality and members.
Page Factory is a way to initialize the web elements you want to interact with within the page object when you create an instance of it.
Test Class – In Test Class, we will write an actual selenium test script. Here, we call Page Action and mentioned actions to be
performed on Web Pages. For each page, we can write our own test class for better code readability. We can write test cases in @Test annotation.
In Page Action Class, we can write all web pages action as per the pages and functionality. Under Page Action component, for each page in the application, we have corresponding Page class.
Page Factory class is nothing but Object Repository in other term. For each web page, it has its own Page Object definitions. Each web element should uniquely get identified and should be defined at class level. We will use Find By annotation and will define web element so that we will be able to perform actions on them.
package pageObjects;
import java.util.List;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindAll;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
public class CheckoutPage {
WebDriver driver;
public CheckoutPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
@FindBy(how = How.CSS, using = "#billing_first_name")
private WebElement txtbx_FirstName;
public void enter_Name(String name) {
txtbx_FirstName.sendKeys(name);
}
}
@FindBy – It is used to locate web elements using different locators strategies.
@FindBy(how = How.ID, using = “userName”)
WebElement username;
@FindBys – To locate a web element with more than one search criteria, you can use @FindBys annotation. This annotation locates the web element by using the AND condition on the search criteria.
@FindBys({
@FindBy(class=”classname”),
@FindBy(id=”idname”)
})
WebElement name;
@FindAll – The @FindAll annotation locates the web element using more than one criteria, given that at least one criteria match. Contrary to @FindBys, it uses an OR conditional relationship between the multiple @FindBy.
@FindAll({
@FindBy(id=”btn”, //doesn’t match
@FindBy(name=”submit”), //Matches
@FindBy(class=”submitclass”) //doesn’t match
})
WebElement submitButton;
@CacheLookUp – The @CacheLookUp annotation is very useful when you are referring to the same web element multiple times. Using @CacheLookUp, we can store the web elements in cache memory right after reading for the first time. It fastens our execution and the code, need not look up for the element on the web page and directly references it from memory.
The @CacheLookUp can be prefixed with any of the annotations discussed above, i.e., @FindBy, @FindBys & @FindAll.
@CacheLookUp
@FindBys({
@FindBy(class=”checkbox-class”),
@FindBy(id=”checkbox-id”)
})
WebElement chkBox;
When we use “initElements” method of PageFactory, all page objects of Page Object class are initialized. It does not mean webdriver locates all web elements of page and store it.
Webdriver will locate element only when page object(Web element) is being used or you can say when any action like click etc are performed on web element and it will through noSuchElementException if web element is not found.