Page Object Model In Selenium WebDriver

What Is Page Object Model :
Page Object Model is nothing but a code organization structure by which we can
separated the web pages and Test classes.
Rule To Making This Structure :
1. Separate the Test classes to WebPage classes.
2. Create page related functionality on the same Page Class, from where we can
perform functionality of this pages.
3. If we perform any action on the page and redirect to the the another page, t
his method return type should be the another page object rather than void.If It
stay on the same page then return the same page object or void.
e.g : We can consider two page TestPage1 and TestPage2 for this example.
TestPage1 and TestPage2 hold different component like button,image,checkbox,drop
down etc.Now we do some operational or structural work on this components like c
lick,select etc to perform a logical or functional operation like login, sighout
etc. After any individual logical or functional operation if we stay on the sam
e page then this operation(method) return the same page object or void otherwise
return the page object where it is redirected.
4. Isolate the Assertion(Pass,Fail) from the page classes and put them into test
classes.
5. Page classes only hold the services that we can expect from this page.
6.Try to Exception handling into the test class.
Code Sample:
We have two page
1. Blog Test Page
2. Google Search Page
We navigate Google search page from google link on Blog Test Page and give some
text search on google page.So we must have 2 different class.BlogPage and Google
Page.
In BlogPage we have one operation click on google link which will navigate to th
e google page so this method return type should be google page object.But submit
button on this page does not navigate to any other page it stay on this page so
it's return type should be the blogpage object.
We follow the same structure in googlepage class also.

Blog Test Page
----------------------------------------------------------------package com.core.pageobject;
import org.openqa.selenium.By;

public GooglePage(WebDriver driver) { .import org.openqa.driver = driver. public class BlogPage { private final By inputBox=By. // this.findElement(By.selenium. private final WebDriver driver. // submitBtn.findElement(submitBtn). } /********************************** * This Method Return BlogPage Object * Because After Submit It's Stay On The * Same Page **********************************/ public BlogPage submitForm(String inputTxt){ driver. } /********************************** * This Method Return GoolePage Object * Because After Click On This Link * It's Redirect To The Google Page **********************************/ public GooglePage clickonGoogleLink(){ driver.click().linkText(googleLink)).click(). private final WebDriver driver.name("txtbox1")). // private final WebElement inputBox.selenium. // private final WebElement submitBtn.click().core.WebDriver. private final String googleLink="Google".sendKeys(inputTxt). import org.openqa. private final By submitBtn=By.pageobject.linkText("Google")). // this.selenium. return new GooglePage(driver). driver.By. // private final WebElement googleLink. public class GooglePage { private final By searchBox=By.findElement(inputBox).name("btnsub")).findElement(By.WebDriver. return new BlogPage(driver).name("btnsub").inputBox = driver. // } } Google Test Page ------------------------------------------------------------package com.googleLink = driver.click().findElement(By.sendKeys(inputTxt). import org.submitBtn = driver. googleLink. // private final WebElement searchBox.findElement(By.openqa. public BlogPage(WebDriver driver) { this. // this. // inputBox.name("txtbox1").name("q").

testng.BeforeMethod.html").selenium.selenium. } @Test() public void testSubmit() throws Exception{ BlogPage blog=new BlogPage(driver).searchBox=driver. // } /********************************** * This Method Return Goole Page Object * Because After Click On Search Button * Redirect To The Google Page **********************************/ public GooglePage searchTxt(String searchTxt){ driver. /********************************** *Initialize Driver For All Test ***********************************/ @BeforeTest public void testSetUp(){ FirefoxBinary bin=new FirefoxBinary(new File("G:\Program Files\Mozilla Firefox\firefox.sendKeys(searchTxt).exe")).blogspot.firefox. org. driver=new FirefoxDriver(bin.name("q")). prof).FirefoxProfile.findElement(By. org. org.findElement(searchBox). import import import import import import import import import java.pageobject. org.core.driver = driver.FirefoxBinary. org.testng.openqa. FirefoxProfile prof=new FirefoxProfile(). . public class PageObjectModelTest { WebDriver driver.WebDriver. return new GooglePage(driver).testng.sendKeys(searchTxt).selenium. org.firefox. org.annotations.annotations.testng. } /********************************** * This Is Created To Go the Base Page * Before Every Test **********************************/ @BeforeMethod public void testMethodSetUp(){ driver.firefox.selenium.in/2013/12/frmset1 .openqa.io.annotations. // searchBox. this.FirefoxDriver. } } Page Object Model Test --------------------------------------------------------------------------package com.Test.openqa. org.this.get("http://startingwithseleniumwebdriver.openqa.annotations.File.BeforeTest.AfterTest.

} @Test public void testGoogleLink()throws Exception{ BlogPage blog=new BlogPage(driver).searchTxt("Test"). blog. } /********************************** * This Is Created To Close The Driver **********************************/ @AfterTest public void tearDown(){ driver.blog. } } .clickonGoogleLink().quit().submitForm("Test").