Page Object Model for selenium webdriver

Like QTP, TestComplete and few other automation tools selenium webdriver does no
t provide any centralized repository where we can put object locator of page ele
ment. In POM object pattern we can accomplish this type approach.
What is POM:
In automated web testing, a Page Object is a class or object that represents a w
eb page in your application. A Page Object hides the technical details about how
you interact with a web page behind a more readable and business-focused facade
. Following are the few points in details:
Page Object Model is a design pattern to create Object Repository for web UI ele
ments. Like it provide you to option to put all locators in corresponding page (
java file ) like for home page you create a homepage java file and put all elem
ent locators of home page in same class.
Under this model, you should create java file like above ( ) home
page and create functions related to this page like clickHomeMenu, clickProduct
() verfiyHomePage() etc.
So your page object class contains elements locator and corresponding function w
hat you need to perform on that particular page.
Name of these function and object locator must be logically defined like element
s name username , password, loginButoon and corresponding function should be lik
e enterUserName(), enterPassword(), clickLoginButton()
Advantages of POM
The tests are much more readable as only function need to call from different-2
page class files into your test files.
Page elements and logic is centralized in one place, this will help much easier
in maintenance.
Easy to understand any new develpoer.
Easy to maintain in case of any changes in application or elements .
Object repository and function or independent from test scripts so we can use th
em in multiple test files.
Implementation of page object model:
In this example I will show you some example how to create POM model by using be
low url:
When you open this url you will land at home page. Also when you click on Chapter
1 , Chapter2 and Chapter3 etc you will get other pages.
In page object model you should create java file for corresponding pages like I
have created , , for home pag
e, chapter 1 page chapter 2 page respectively. And put corresponding elements lo
cator and functions.


public class HomePage {
private WebDriver driver;
WebElement chapter1;

class).test. org. import import import import import org.openqa.println(data1).WebDriver. org.driver = return PageFactory. } public ChapterSecond clickChapterSecond(){ chapter2. package com. } public ChapterFirstPage clickSecondAjaxButton() { secondajax.class). org.out. @FindBy(xpath = "//select[@id='selecttype']") WebElement dropdown.initElements(driver. . } } 2.initElements(driver.selenium.class)"Chapter2") WebElement chapter2. ChapterSecond. ChapterFirstPage.initElements(driver. public HomePage(WebDriver driver) { this.PageFactory.openqa. ChapterFirstPage. } public ChapterFirstPage clickSecondAjaxButton1(String data1) { System.openqa. } public void clickChapterThird(){ chapter3. return = @FindBy(id = "verifybutton") WebElement verifybutton. return PageFactory. public class ChapterFirstPage { private WebDriver driver.FindBy.selenium. } public ChapterFirstPage clickChapterFirst(){ chapter1.Select. public ChapterFirstPage(WebDriver driver) { this.openqa. @FindBy(id = "secondajaxbutton") WebElement secondajax.openqa.selenium.WebElement.selenium. @FindBy(linkText="Chapter3") WebElement chapter3. public ChapterSecondPage(WebDriver driver) { this. ChapterFirstPage.FindBy. return PageFactory. } public String getTest() { return index.openqa. HomePage. return PageFactory. org. import import import import org. ChapterFirstPage. @FindBy(id = "//input[@name='but2']") WebElement but2.initElements( } public ChapterFirstPage selctDropDown() { new Select(dropdown).initElements(driver. } } 3. } public HomePage clickIndex() { index.class).support.openqa. public class ChapterSecondPage { private WebDriver driver. return PageFactory.initElements(driver.driver = org. org.getText().initElements(driver.openqa. } public ChapterFirstPage verifyButton() { return PageFactory. } } .selenium.selenium. ChapterSecondPage. @FindBy(id = "Index") WebElement index.test. return PageFactory.selenium. @FindBy(xpath = "//input[@id='random']") WebElement random.selectByVisibleText("Selenium Core"). } public ChapterSecondPage clickbut2() { package ChapterSecondPage.selenium.WebDriver. ChapterFirstPage.class).return PageFactory.class).class). } public ChapterSecondPage clickRandom() { random.

PageFactory.scripts. you should create base class of all test scripts where y ou should put common functions related test case like I created TestBase.test.class). PageFactory class used to create run time object of page classes like: PageFactory.openqa.HomePage. com.annotations.FirefoxDriver. package package driver. HomePage homePage.ChapterFirstPage. You should return object of class where you application move after performing ac tions like when I click index in ChapterSecondPage.class).manage(). public class TestBase { protected protected protected protected protected WebDriver driver. TimeUnit. org. } @AfterSuite public void tearDown() throws Exception { driver.implicitlyWait(30. . import org.initElements(driver. driver = new FirefoxDriver(). com.initElements(driver.TimeUnit.BeforeSuite.concurrent. } } To create test scripts and import TestBase class and call corresponding function s from page java file as needed in your test Similar way another example when you click on clickbut2() then same page open PageFactory.quit(). PageFactory.theautomatedtester. org.openqa. org.test. import import import import import import import import java.openqa.selenium.test. Now creating test". ChapterSecondPage chapterSecond. HomePage.timeouts(). ChapterSecondPage.ChapterSecondPage.testng. @BeforeSuite public void setUp() { baseUrl = "http://book. HomePage.firefox.selenium.So in above examples you can see that page elements locator are defined using @F indBy annotation and corresponding functions are defined which will separate fro m each ChapterFirstPage String baseUrl. file it returm HomePage object as after clicking on index page Home page will open.

clickbut2().verifyButton().test.import org.getTest(). . Leave comment in case you have any query.selctDropDown().clickRandom().util.clickIndex().TestBase.clickSecondAjaxButton1(data).annotations.testng. String data = chapterSecond. chapterFirstPage. chapterFirstPage.class). import com. } } Hope this will help you to design page object model in selenium webdriver. driver. homePage = chapterSecond. public class MyTest extends TestBase { @Test public void testPageObject() throws Exception { homePage = PageFactory.clickSecondAjaxButton(). HomePage.get(baseUrl).Test.initElements(driver. chapterFirstPage. chapterSecond.HomePage. import com.clickChapterFirst().clickChapterSecond(). chapterFirstPage = homePage. chapterSecond = homePage. chapterFirstPage.test.