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, this 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,dropdown etc.Now we do some operational or structural work on this components like click,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 same 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 GooglePage.
In BlogPage we have one operation click on google link which will navigate to the 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;
import org.openqa.selenium.WebDriver;
public class BlogPage {
private final By inputBox=By.name("txtbox1");
private final By submitBtn=By.name("btnsub");
private final String googleLink="Google";
// private final WebElement inputBox;
// private final WebElement submitBtn;
// private final WebElement googleLink;
private final WebDriver driver;
public BlogPage(WebDriver driver) {
this.driver = driver;
// this.inputBox = driver.findElement(By.name("txtbox1"));
// this.googleLink = driver.findElement(By.linkText("Google"));
// this.submitBtn = driver.findElement(By.name("btnsub"));
}
/**********************************
* This Method Return BlogPage Object
* Because After Submit It's Stay On The
* Same Page
**********************************/
public BlogPage submitForm(String inputTxt){
driver.findElement(inputBox).sendKeys(inputTxt);
driver.findElement(submitBtn).click();
// inputBox.sendKeys(inputTxt);
// submitBtn.click();
return new BlogPage(driver);
}
/**********************************
* This Method Return GoolePage Object
* Because After Click On This Link
* It's Redirect To The Google Page
**********************************/
public GooglePage clickonGoogleLink(){
driver.findElement(By.linkText(googleLink)).click();
// googleLink.click();
return new GooglePage(driver);
}
}
Google Test Page
-------------------------------
-------------------------------
package com.core.pageobject;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class GooglePage {
private final By searchBox=By.name("q");
// private final WebElement searchBox;
private final WebDriver driver;
public GooglePage(WebDriver driver) {
this.driver = driver;
// this.searchBox=driver.findElement(By.name("q"));
}
/**********************************
* This Method Return Goole Page Object
* Because After Click On Search Button
* Redirect To The Google Page
**********************************/
public GooglePage searchTxt(String searchTxt){
driver.findElement(searchBox).sendKeys(searchTxt);
// searchBox.sendKeys(searchTxt);
return new GooglePage(driver);
}
}
Page Object Model Test
--------------------------------------
--------------------------------------
package com.core.pageobject;
import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class PageObjectModelTest {
WebDriver driver;
/**********************************
*Initialize Driver For All Test
***********************************/
@BeforeTest
public void testSetUp(){
FirefoxBinary bin=new FirefoxBinary(new File("G:\\Program Files\\Mozilla Firefox\\firefox.exe"));
FirefoxProfile prof=new FirefoxProfile();
driver=new FirefoxDriver(bin, prof);
}
/**********************************
* This Is Created To Go the Base Page
* Before Every Test
**********************************/
@BeforeMethod
public void testMethodSetUp(){
driver.get("http://startingwithseleniumwebdriver.blogspot.in/2013/12/frmset1.html");
}
@Test()
public void testSubmit() throws Exception{
BlogPage blog=new BlogPage(driver);
blog.submitForm("Test");
}
@Test
public void testGoogleLink()throws Exception{
BlogPage blog=new BlogPage(driver);
blog.clickonGoogleLink().searchTxt("Test");
}
/**********************************
* This Is Created To Close The Driver
**********************************/
@AfterTest
public void tearDown(){
driver.quit();
}
}
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, this 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,dropdown etc.Now we do some operational or structural work on this components like click,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 same 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 GooglePage.
In BlogPage we have one operation click on google link which will navigate to the 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;
import org.openqa.selenium.WebDriver;
public class BlogPage {
private final By inputBox=By.name("txtbox1");
private final By submitBtn=By.name("btnsub");
private final String googleLink="Google";
// private final WebElement inputBox;
// private final WebElement submitBtn;
// private final WebElement googleLink;
private final WebDriver driver;
public BlogPage(WebDriver driver) {
this.driver = driver;
// this.inputBox = driver.findElement(By.name("txtbox1"));
// this.googleLink = driver.findElement(By.linkText("Google"));
// this.submitBtn = driver.findElement(By.name("btnsub"));
}
/**********************************
* This Method Return BlogPage Object
* Because After Submit It's Stay On The
* Same Page
**********************************/
public BlogPage submitForm(String inputTxt){
driver.findElement(inputBox).sendKeys(inputTxt);
driver.findElement(submitBtn).click();
// inputBox.sendKeys(inputTxt);
// submitBtn.click();
return new BlogPage(driver);
}
/**********************************
* This Method Return GoolePage Object
* Because After Click On This Link
* It's Redirect To The Google Page
**********************************/
public GooglePage clickonGoogleLink(){
driver.findElement(By.linkText(googleLink)).click();
// googleLink.click();
return new GooglePage(driver);
}
}
Google Test Page
-------------------------------
-------------------------------
package com.core.pageobject;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class GooglePage {
private final By searchBox=By.name("q");
// private final WebElement searchBox;
private final WebDriver driver;
public GooglePage(WebDriver driver) {
this.driver = driver;
// this.searchBox=driver.findElement(By.name("q"));
}
/**********************************
* This Method Return Goole Page Object
* Because After Click On Search Button
* Redirect To The Google Page
**********************************/
public GooglePage searchTxt(String searchTxt){
driver.findElement(searchBox).sendKeys(searchTxt);
// searchBox.sendKeys(searchTxt);
return new GooglePage(driver);
}
}
Page Object Model Test
--------------------------------------
--------------------------------------
package com.core.pageobject;
import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class PageObjectModelTest {
WebDriver driver;
/**********************************
*Initialize Driver For All Test
***********************************/
@BeforeTest
public void testSetUp(){
FirefoxBinary bin=new FirefoxBinary(new File("G:\\Program Files\\Mozilla Firefox\\firefox.exe"));
FirefoxProfile prof=new FirefoxProfile();
driver=new FirefoxDriver(bin, prof);
}
/**********************************
* This Is Created To Go the Base Page
* Before Every Test
**********************************/
@BeforeMethod
public void testMethodSetUp(){
driver.get("http://startingwithseleniumwebdriver.blogspot.in/2013/12/frmset1.html");
}
@Test()
public void testSubmit() throws Exception{
BlogPage blog=new BlogPage(driver);
blog.submitForm("Test");
}
@Test
public void testGoogleLink()throws Exception{
BlogPage blog=new BlogPage(driver);
blog.clickonGoogleLink().searchTxt("Test");
}
/**********************************
* This Is Created To Close The Driver
**********************************/
@AfterTest
public void tearDown(){
driver.quit();
}
}