- Selenium Tutorial
- Selenium - Home
- Selenium - Overview
- Selenium - Components
- Selenium - Automation Testing
- Selenium - Environment Setup
- Selenium - Remote Control
- Selenium IDE Tutorial
- Selenium - IDE Introduction
- Selenium - Features
- Selenium - Limitations
- Selenium - Installation
- Selenium - Creating Tests
- Selenium - Creating Script
- Selenium - Store Variables
- Selenium - Alerts & Popups
- Selenium - Selenese Commands
- Selenium - Actions Commands
- Selenium - Accessors Commands
- Selenium - Assertions Commands
- Selenium - Assert/Verify Methods
- Selenium - Locating Strategies
- Selenium - Script Debugging
- Selenium - Verification Points
- Selenium - Pattern Matching
- Selenium - JSON Data File
- Selenium - Browser Execution
- Selenium - User Extensions
- Selenium - Code Export
- Selenium - JavaScript Functions
- Selenium - Plugins
- Selenium WebDriver Tutorial
- Selenium - Introduction
- Selenium WebDriver vs RC
- Selenium - Installation
- Selenium - First Test Script
- Selenium - Driver Sessions
- Selenium - Browser Options
- Selenium - Chrome Options
- Selenium - Edge Options
- Selenium - Firefox Options
- Selenium - Safari Options
- Selenium Web Elements
- Selenium - File Upload
- Selenium - Locator Strategies
- Selenium - Relative Locators
- Selenium - Finders
- Selenium - Find All Links
- Selenium - User Interactions
- Selenium - WebElement Commands
- Selenium - Browser Interactions
- Selenium - Browser Commands
- Selenium - Browser Navigation
- Selenium - Alerts & Popups
- Selenium - Handling Forms
- Selenium - Windows and Tabs
- Selenium - Handling Links
- Selenium - Input Boxes
- Selenium - Radio Button
- Selenium - Checkboxes
- Selenium - Dropdown Box
- Selenium - Handling IFrames
- Selenium - Handling Cookies
- Selenium - Date Time Picker
- Selenium - Dynamic Web Tables
- Selenium - Actions Class
- Selenium - Action Class
- Selenium - Keyboard Events
- Selenium - Key Up/Down
- Selenium - Copy and Paste
- Selenium - Handle Special Keys
- Selenium - Mouse Events
- Selenium - Drag and Drop
- Selenium - Scroll Operations
- Selenium - Waiting Strategies
- Selenium - Explicit/Implicit Wait
- Selenium - Support Features
- Selenium - Multi Select
- Selenium - Wait Support
- Selenium - Select Support
- Selenium - Color Support
- Selenium - Errors & Logging
- Selenium - Logging
- Selenium - Exception Handling
- Selenium - Miscellaneous
- Selenium - Handling Ajax Calls
- Selenium - JSON Data File
- Selenium - CSV Data File
- Selenium - Excel Data File
- Selenium - Cross Browser Testing
- Selenium - Multi Browser Testing
- Selenium - Multi Windows Testing
- Selenium - JavaScript Executor
- Selenium - Headless Execution
- Selenium - Capture Screenshots
- Selenium - Capture Videos
- Selenium - Page Object Model
- Selenium - Page Factory
- Selenium - Record & Playback
- Selenium - Frameworks
- Selenium - Browsing Context
- Selenium Grid Tutorial
- Selenium - Overview
- Selenium - Architecture
- Selenium - Components
- Selenium - Configuration
- Selenium - Create Test Script
- Selenium - Test Execution
- Selenium - Endpoints
- Selenium - Customizing a Node
- Selenium Reporting Tools
- Selenium - Reporting Tools
- Selenium - TestNG
- Selenium - JUnit
- Selenium - Allure
- Selenium & other Technologies
- Selenium - Java Tutorial
- Selenium - Python Tutorial
- Selenium - C# Tutorial
- Selenium - Javascript Tutorial
- Selenium - Kotlin Tutorial
- Selenium - Ruby Tutorial
- Selenium - Maven & Jenkins
- Selenium - Database Testing
- Selenium - LogExpert Logging
- Selenium - Log4j Logging
- Selenium - Robot Framework
- Selenium - AutoIT
- Selenium - Flash Testing
- Selenium - Apache Ant
- Selenium - Github
- Selenium - SoapUI
- Selenium - Cucumber
- Selenium - IntelliJ
- Selenium - XPath
- Selenium Miscellaneous Concepts
- Selenium - Automation Frameworks
- Selenium - Keyword Driven Framework
- Selenium - Data Driven Framework
- Selenium - Hybrid Driven Framework
- Selenium - SSL Certificate Error
- Selenium - Alternatives
- Selenium Useful Resources
- Selenium - Quick Guide
- Selenium - Useful Resources
- Selenium - Automation Practice
- Selenium - Discussion
Selenium WebDriver - Page Factory
The test cases developed using Selenium Webdriver can be streamlined by incorporating different design patterns within the tests. One of the widely used design patterns is Page Object Model, popularly known as the POM. In order to implement a Page Object Model, we can take help of the factory class which is known as the Page Factory. The Page Factory class is available from the Selenium Webdriver.
What is a Page Factory?
As mentioned previously, a Page Factory is a class which aids in implementing a Page Object Model. It is an improved version of Page Object Model and it is provided by the Webdriver class. It has the feature to use annotations to initialize the web elements which constitute the page objects.
Features of Page Factory
@FindBy
This annotation is used in Page Factory to identify, declare, and perform actions on the web elements with the help of various locators like id, name, class name, link text, partial link text, css, xpath, and tagname.
Syntax
FindBy(name = "elementname") WebElement e;
@FindBys
This annotation is used in Page Factory to identify, declare, and perform actions on the web elements with the help of various locators like id, name, class name, link text, partial link text, css, xpath, and, tagname by specifying multiple search conditions. An ADD condition among search criterias has to be met to identify an element uniquely.
Syntax
@FindBys({ @FindBy(tagname = "a"), @FindBy(linkText = "Search") }) WebElement lnk;
In the above example, if all the two conditions are met, we will be able to identify a web element.
@FindAll
This annotation in Page Factory is similar to @FindBys since it allows multiple search conditions. However, in order to locate an element uniquely, any one condition needs to be satisfied(OR condition among the criterias should be true).
Syntax
@FindAll({ @FindBy(tagname = "a"), @FindBy(linkText = "Search"), @FindBy(class = "name"), }) WebElement btn;
In the above example, if anyone of the three conditions are met, we will be able to identify a web element.
@CacheLookUp
This annotation is used in Page Factory to optimize the execution speed. This is usually used to refer to an element used frequently in a test, for example the user credentials which will be required every time for every test case. It allows to stock up the frequently used variables in the cache memory post the first time usage. This annotation can be used simultaneously along with other Page Factory annotations.
Syntax
@CacheLookUp @FindAll({ @FindBy(tagname = "a"), @FindBy(linkText = "Search"), @FindBy(class = "name"), }) WebElement loginBtn;
initElement() − It is a static method from the Page Factory class used to initialize all the web elements identified by the @FindBy annotation.
Lazy initialization − The AjaxElementLocatorFactory is based on the lazy load idea in Page Factory to locate the web elements in action and pass a time out. This is normally used for applications having Ajax elements.
What are differences between Page Factory and Page Object Model?
The differences between the Page Factory and Page Object are listed below −
In Page Factory, the elements are identified by @FindBy, where in Page Object Model, the elements are identified using the By.
Page Factory has the concept of lazy initialization which is absent in Page Object Model.
Page Object Model is a design pattern while the Page Factory is a class available in the Webdriver class which helps to implement the Page Object Model.
In Page Object Model, every object has to be initialized separately, while in the Page Factory, the initElement() method initializes all objects at once.
Example
Let us take an example of the below page, where we have the New User button on the Welcome Page.
On clicking the New User button, we will be navigating to the Registration page, having the Welcome, Register text as shown in the below image.
For the above example, we would create two page classes - WelcomePage.java and RegisterPage.java where we would have the web elements declared on these respective pages(using the @FindBy annotation) and actions to be performed on those elements. We would also create the test class - WelcomePageTest.java which would contain the actual test case, initialize the driver objects of the WelcomePage and RegisterPage, and assertions related to that test case. All the test cases would be under the same POM package.
Code Implementation on page class WelcomePage.java.
package POM; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.PageFactory; public class WelcomePage { WebDriver driver; @FindBy(xpath = "//*[@id='signInForm']/h1") WebElement text; @FindBy(xpath = "//*[@id='signInForm']/div[3]/a") WebElement btn;; public WelcomePage(WebDriver driver) { this.driver=driver; // initializing all page objects PageFactory.initElements(driver, this); } public String verifyPageHeading() { String getHeadtext = text.getText(); return getHeadtext; } public void clickOnNewUser() { btn.click(); } }
Code Implementation on page class RegisterPage.java.
package POM; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.PageFactory; public class RegisterPage { WebDriver driver; @FindBy(xpath = "//*[@id='signupForm']/h1") WebElement text; public RegisterPage(WebDriver driver) { this.driver=driver; // initializing all page objects PageFactory.initElements(driver, this); } public String verifyPageHeading() { String getHeadtext = text.getText(); return getHeadtext; } }
Code Implementation on test class WelcomePageTest.java.
package POM; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.*; import java.util.concurrent.TimeUnit; import static org.testng.Assert.assertEquals; public class WelcomePageTest { WebDriver driver; WelcomePage objWelcomePage; RegisterPage objRegisterPage; @BeforeTest public void setup() { // Initiate the Webdriver driver = new ChromeDriver(); // adding implicit wait of 12 secs driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Opening the webpage driver.get("https://www.tutorialspoint.com/selenium/practice/login.php"); } @Test(priority = 1) public void verifyWelcomePageHeading() { // object of WelcomePage page class objWelcomePage = new WelcomePage(driver); String text = objWelcomePage.verifyPageHeading(); System.out.println("Page heading in Welcome Page: " + text); // assertions to test case assertEquals("Welcome, Login In", text); } @Test(priority = 2) public void moveToRegisterPage() { objWelcomePage = new WelcomePage(driver); objWelcomePage.clickOnNewUser(); } @Test(priority = 3) public void verifyRegisterPageHeading() { // object of RegisterPage page class objRegisterPage = new RegisterPage(driver); String text = objRegisterPage.verifyPageHeading(); System.out.println("Page heading in Register Page: " + text); // assertions to test case assertEquals("Welcome,Register", text); } @AfterTest public void teardown() { // quitting browser driver.quit(); } }
Dependencies in pom.xml.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>SeleniumJava</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>16</maven.compiler.source> <maven.compiler.target>16</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.11.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.testng/testng --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.9.0</version> <scope>test</scope> </dependency> </dependencies> </project>
Output
Page heading in Welcome Page: Welcome, Login In Page heading in Register Page: Welcome,Register =============================================== Default Suite Total tests run: 3, Passes: 3, Failures: 0, Skips: 0 =============================================== Process finished with exit code 0
In the above example, we had first identified the web elements on the Welcome and Register pages and performed actions on them within the page classes - WelcomePage.java and RegisterPage.java.
Within the test class WelcomePageTest.java, we had created objects of the above mentioned page classes to create the whole flow of the test case and also added assertions.
We had taken help of the TestNG test framework along with Page Factory, to implement the Page Object Model within the Selenium Webdriver test and retrieved the page headers with the message in the console - Page heading in Welcome Page: Welcome, Login In and Page heading in Register Page: Welcome,Register.
The result in the console shows Total tests run: 3, as there are three methods with @Test annotations - verifyWelcomePageHeading(), moveToRegisterPage(), and verifyRegisterPageHeading().
Finally, the message Passes: 3, and Process finished with exit code 0 was received, signifying successful execution of the code.
This concludes our comprehensive take on the tutorial on Selenium Webdriver - Page Factory. We’ve started with describing a Page Factory, and walked through the features of a Page Factory, differences between Page Object Model and Page Factory, and an example illustrating how to use Page Factory along with Selenium. This equips you with in-depth knowledge of the Page Factory. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.