Page object model
- Page Object is a Design Pattern which has become popular in test automation for enhancing test maintenance and reducing code duplication.
- Page object is an object-oriented class that serves as an interface to a page of your Application under test. The tests then use the methods/WebElements of this page object class whenever they need to interact with that page of the UI. The benefit is that if any UI element changes of that the page we don't need to change in our Test just we need to change only the code within the page object.
- Page Objects Model is best suited for applications which have multiple pages like similar registration form/or menu bar or states. Each of which have fields which can be uniquely referenced with respect to the page.
Pom Advantages:
- Object Repository: We can create an Object Repository of the fields segmented page-wise. This as a result provides a Page Repository of the application as well.
- Functional Encapsulation: All possible functionality or operations that can be performed on a page can be defined and contained within the same class created for each page.
- Low Redundancy:Helps reduce duplication of code. If the architecture is correctly and sufficiently defined, the POM gets more done in less code.
- Efficient & Scalable: Faster than other keyword-driven/data-driven approaches where Excel sheets are to be read/written.
How to implement Page object Model
- Create a ‘New Package file and name it as ‘pageobjectpages’ by right click on the Project and select New > Package.
- 2. Create a ‘New Class’ file and refer the name to the actual page from the test object, by right click on the above created Package and select New > Class. In our case it as FacebookRegistrationpage
- Now write code for Registration page.
- Make Webdriver driver as null
- You need to define all elements according to ui whether it is radiobutton/textboxes.Just you need to define the all webelemets on which you want to perform action.
- Create a Constuctor of Your class and Pass WebDriver as Parameter
- You need to write methods/Function for all WebElements whether you want to click/write/Select from dropdown.
package pageobjectpages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.Select;
public class FacebookRegistrationpage {
//Instancing webdriver
public WebDriver driver;
//Defining webelements
By firstname = By.xpath("//*[@value='First name']");
By lastname = By.xpath("//*[@value='Last name']");
By email = By.xpath("//*[@value='Email or mobile number']");
By reemail = By.xpath("//*[@value='Re-enter email or mobile number']");
By newPassword = By.xpath("//*[@value='New password']");
By month = By.xpath("//*[@id='month']");
By day = By.xpath("//*[@id='day']");
By year = By.xpath("//*[@id='year']");
By femaleradiobutton = By.xpath("//*[@id='u_0_d']");
By signup = By.xpath("//button[@name='websubmit']");
// Instantiate class
public FacebookRegistrationpage(WebDriver driver) {
this.driver = driver;
}
// Type firstname
public void typefirstname(String uname) {
//Create a method which will enter firstname
driver.findElement(firstname).sendKeys(uname);
}
// Type lastname
public void typelastname(String lstname) {
//Create a method which will enter lastname
driver.findElement(lastname).sendKeys(lstname);
}
// Type email
public void typeemail(String emailid) {
//Create a method which will enter emailid
driver.findElement(email).sendKeys(emailid);
}//Typereemail
public void typeremail(String remailid) {
//Create a method which should type email
driver.findElement(reemail).sendKeys(remailid);
}
//typenewpassword
public void typenewPassword(String newpassrd) {
//Create method will enter password
driver.findElement(newPassword).sendKeys(newpassrd);
}
//selectmonth
public void selectmonth(String mont){
Select mon = new Select(driver.findElement(month));
mon.selectByVisibleText(mont);
}
//Select date
public void selectdate(String date){
Select datebirth = new Select(driver.findElement(day));
datebirth .selectByVisibleText(date);
}
//selecting year
public void selectyear(String yea){
Select yearsel = new Select(driver.findElement(year));
yearsel .selectByVisibleText(yea);
}
//clicking on femaleradiobutton
public void ClickingGender(){
driver.findElement(femaleradiobutton).click();
}
//clicking sign up button
public void clicksignup(){
driver.findElement(signup).click();
}
}
//In this We have created a Registration page of Facebook Now we need to call this page in our Testcase.Generally we create pages in page object model and after that we call that pages in Testcase.
package pageobjectframework;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import pageobjectpages.FacebookRegistrationpage;
public class TestCase {
WebDriver driver;
FacebookRegistrationpage page;
@BeforeTest
public void openBrowser(){
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("http://www.facebook.com");
}
//This test will fill the sign up page of facebook login
@Test
public void registerDetails(){
page = new FacebookRegistrationpage(driver);
page.typefirstname("ritika");
page.typelastname("Gulati");
page.typeemail("submityourmailid@gmail.com");
page.typeremail("submityourmailid@gmail.com");
page.typenewPassword("seleniumgyan");
page.selectdate("22");
page.selectyear("1991");
page.selectmonth("Jan");
page.ClickingGender();
page.clicksignup();
}
}
In Pageobject framework just we create pages and call these pages into our Testcases.In these pages we just write all the Webelements and functions. This is only registration page but it Depends on application How many pages your application contains.If your application have 20 ui pages Then you can call all 20 pages in One Testcase.








nice post
ReplyDeleteThanks Dwarika
ReplyDeleteSuch a nice blog.
ReplyDeleteWould you mind posting a way of customizing the execution report?
Have seen the popular reports such as TestNG and ReportNG , Is there any way to customize them ? Or any other tools available in market ?
Thanks @Badri Generally we follow xslt Reports
ReplyDeletehttps://www.youtube.com/watch?v=H9y2ujd8UyM
https://www.youtube.com/watch?v=Nfx1xCvhC7g
Really helpful post
ReplyDeleteCan you please make a post on datadriven in TestNG
ReplyDeleteThanks Prashanth.I will update very soon.
ReplyDeleteThanks:)Keep on posting...
ReplyDeleteWelcome..
ReplyDeleteHi Ritika,Nice post,but having a doubt...i am kinda new in this...what is difference b/w Page Factory and object Repository.Are these two same?
ReplyDeleteAwesome post Ritika, Can we use page object model without change in code ie handling everything in excel file.
ReplyDeleteBoth are different.In object repository we save all the locators in a single file.But in page object model all the locators are save in pages.Login page loacators are in login page.Register page locators are in different page.
ReplyDelete@Ashish we can use excel
ReplyDeleteRitika,pls correct me if m wrong...what I am doin currently is I've put all my locators in a prop file and taking those locators in a page object class(like FacebookRegistrationpage in this e.g) and calling methods to my test class...m I doin it correct? In this case m I using Page factory or its completely different thing?
ReplyDelete@Anonymous either use page factory or object repository both are doing same things.
ReplyDeleteHey nice blog.! :)
ReplyDelete- Ani
Hi Please may i know do we have to create pageobjectclass for every webpage?
ReplyDelete