Course
Selenium Tutorial for Beginners: Learn WebDriver in 7 Days
Training Summary Selenium is a popular open-source web-based automation tool. This online course is...
Page Object Model (POM) is a design pattern, popularly used in test automation that creates Object Repository for web UI elements. The advantage of the model is that it reduces code duplication and improves test maintenance.
Under this model, for each web page in the application, there should be a corresponding Page Class. This Page class will identify the WebElements of that web page and also contains Page methods which perform operations on those WebElements. Name of these methods should be given as per the task they are performing, i.e., if a loader is waiting for the payment gateway to appear, POM method name can be waitForPaymentScreenDisplay().
In this tutorial, you will learn-
Starting an UI Automation in Selenium WebDriver is NOT a tough task. You just need to find elements, perform operations on it.
Consider this simple script to login into a website
As you can observe, all we are doing is finding elements and filling values for those elements.
This is a small script. Script maintenance looks easy. But with time test suite will grow. As you add more and more lines to your code, things become tough.
The chief problem with script maintenance is that if 10 different scripts are using the same page element, with any change in that element, you need to change all 10 scripts. This is time consuming and error prone.
A better approach to script maintenance is to create a separate class file which would find web elements, fill them or verify them. This class can be reused in all the scripts using that element. In future, if there is a change in the web element, we need to make the change in just 1 class file and not 10 different scripts.
This approach is called Page Object Model in Selenium. It helps make the code more readable, maintainable, and reusable.
Simple POM:
It's the basic structure of Page object model framework where all Web Elements of the AUT and the method that operate on these Web Elements are maintained inside a class file.A task like verification should be separate as part of Test methods.
Complete Example
TestCase: Go to gtupapers Demo Site.
| Step 1) Go to gtupapers Demo Site | |
| Step 2) In home page check text "gtupapers Bank" is present | |
| Step 3) Login into application | |
| Step 4) Verify that the Home page contains text as "Manger Id: demo" | |
Here are we are dealing with 2 pages
Accordingly, we create 2 POM in Selenium classes
gtupapers Login page POM
package pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class gtupapersLogin {
WebDriver driver;
By user99GuruName = By.name("uid");
By password99Guru = By.name("password");
By titleText =By.className("barone");
By login = By.name("btnLogin");
public gtupapersLogin(WebDriver driver){
this.driver = driver;
}
//Set user name in textbox
public void setUserName(String strUserName){
driver.findElement(user99GuruName).sendKeys(strUserName);
}
//Set password in password textbox
public void setPassword(String strPassword){
driver.findElement(password99Guru).sendKeys(strPassword);
}
//Click on login button
public void clickLogin(){
driver.findElement(login).click();
}
//Get the title of Login Page
public String getLoginTitle(){
return driver.findElement(titleText).getText();
}
/**
* This POM method will be exposed in test case to login in the application
* @param strUserName
* @param strPasword
* @return
*/
public void loginTogtupapers(String strUserName,String strPasword){
//Fill user name
this.setUserName(strUserName);
//Fill password
this.setPassword(strPasword);
//Click Login button
this.clickLogin();
}
}gtupapers Home Page POM in Selenium
package pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class gtupapersHomePage {
WebDriver driver;
By homePageUserName = By.xpath("//table//tr[@class='heading3']");
public gtupapersHomePage(WebDriver driver){
this.driver = driver;
}
//Get the User name from Home Page
public String getHomePageDashboardUserName(){
return driver.findElement(homePageUserName).getText();
}
}gtupapers Simple POM in Selenium Test case
package test;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import pages.gtupapersHomePage;
import pages.gtupapersLogin;
public class Test99GuruLogin {
String driverPath = "C:\\geckodriver.exe";
WebDriver driver;
gtupapersLogin objLogin;
gtupapersHomePage objHomePage;
@BeforeTest
public void setup(){
System.setProperty("webdriver.gecko.driver", driverPath);
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://demo.gtupapers.com/V4/");
}
/**
* This test case will login in http://demo.gtupapers.com/V4/
* Verify login page title as gtupapers bank
* Login to application
* Verify the home page using Dashboard message
*/
@Test(priority=0)
public void test_Home_Page_Appear_Correct(){
//Create Login Page object
objLogin = new gtupapersLogin(driver);
//Verify login page title
String loginPageTitle = objLogin.getLoginTitle();
Assert.assertTrue(loginPageTitle.toLowerCase().contains("gtupapers bank"));
//login to application
objLogin.loginTogtupapers("mgr123", "mgr!23");
// go the next page
objHomePage = new gtupapersHomePage(driver);
//Verify home page
Assert.assertTrue(objHomePage.getHomePageDashboardUserName().toLowerCase().contains("manger id : mgr123"));
}Page Factory in Selenium is an inbuilt Page Object Model framework concept for Selenium WebDriver but it is very optimized. It is used for initialization of Page objects or to instantiate the Page object itself. It is also used to initialize Page class elements without using "FindElement/s."
Here as well, we follow the concept of separation of Page Object Repository and Test Methods. Additionally, with the help of class PageFactory in Selenium, we use annotations @FindBy to find WebElement. We use initElements method to initialize web elements
@FindBy can accept tagName, partialLinkText, name, linkText, id, css, className, xpath as attributes.
Let's look at the same example as above using Page Factory
gtupapers Login page with Page Factory
package PageFactory;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class gtupapersLogin {
/**
* All WebElements are identified by @FindBy annotation
*/
WebDriver driver;
@FindBy(name="uid")
WebElement user99GuruName;
@FindBy(name="password")
WebElement password99Guru;
@FindBy(className="barone")
WebElement titleText;
@FindBy(name="btnLogin")
WebElement login;
public gtupapersLogin(WebDriver driver){
this.driver = driver;
//This initElements method will create all WebElements
PageFactory.initElements(driver, this);
}
//Set user name in textbox
public void setUserName(String strUserName){
user99GuruName.sendKeys(strUserName);
}
//Set password in password textbox
public void setPassword(String strPassword){
password99Guru.sendKeys(strPassword);
}
//Click on login button
public void clickLogin(){
login.click();
}
//Get the title of Login Page
public String getLoginTitle(){
return titleText.getText();
}
/**
* This POM method will be exposed in test case to login in the application
* @param strUserName
* @param strPasword
* @return
*/
public void loginTogtupapers(String strUserName,String strPasword){
//Fill user name
this.setUserName(strUserName);
//Fill password
this.setPassword(strPasword);
//Click Login button
this.clickLogin();
}
}gtupapers Home Page with Page Factory
package PageFactory;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class gtupapersHomePage {
WebDriver driver;
@FindBy(xpath="//table//tr[@class='heading3']")
WebElement homePageUserName;
public gtupapersHomePage(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();
}
}package test;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import PageFactory.gtupapersHomePage;
import PageFactory.gtupapersLogin;
public class Test99GuruLoginWithPageFactory {
String driverPath = "C:\\geckodriver.exe";
WebDriver driver;
gtupapersLogin objLogin;
gtupapersHomePage objHomePage;
@BeforeTest
public void setup(){
System.setProperty("webdriver.gecko.driver", driverPath);
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://demo.gtupapers.com/V4/");
}
/**
* This test go to http://demo.gtupapers.com/V4/
* Verify login page title as gtupapers bank
* Login to application
* Verify the home page using Dashboard message
*/
@Test(priority=0)
public void test_Home_Page_Appear_Correct(){
//Create Login Page object
objLogin = new gtupapersLogin(driver);
//Verify login page title
String loginPageTitle = objLogin.getLoginTitle();
Assert.assertTrue(loginPageTitle.toLowerCase().contains("gtupapers bank"));
//login to application
objLogin.loginTogtupapers("mgr123", "mgr!23");
// go the next page
objHomePage = new gtupapersHomePage(driver);
//Verify home page
Assert.assertTrue(objHomePage.getHomePageDashboardUserName().toLowerCase().contains("manger id : mgr123"));
}
}Complete Project Structure will look like the diagram:
AjaxElementLocatorFactory is a lazy loading concept of PageFactory in Selenium. It is used to find the web elements only when the elements are used in any operation. It assigns a timeout for WebElements to the object page class. One of the key advantages of using the pattern PageFactory in Selenium is AjaxElementLocatorFactory Class.
Here, when an operation is performed on an element the wait for its visibility starts from that moment only. If the element is not found in the given time interval, Test Case execution will throw 'NoSuchElementException' exception.
Download the Selenium Project Files for the Demo in this Tutorial
Training Summary Selenium is a popular open-source web-based automation tool. This online course is...
$20.20 $9.99 for today 4.6 (128 ratings) Key Highlights of Selenium Tutorial for Beginner PDF:...
In this tutorial, we will see how to identify the following form elements Radio Button Check Box...
Why do you need Find Element/s command? Interaction with a web page requires a user to locate the web...
What is Selenium Grid? Selenium Grid is a part of the Selenium Suite that specializes in running...
Firefox profile is the collection of settings, customization, add-ons and other personalization...