PageObjectModel
Page Object
Model
Page Object is a Design Pattern for enhancing
test maintenance and reducing code duplication. A page object is an
object-oriented class that serves as an interface to a page of your AUT. The
tests then use the methods of this page object class whenever they need to
interact with the UI of that page. The benefit is that if the UI changes for
the page,it don’t require us to change any tests, just need to change only the
code within the page objects (Only at one place) needs to change. Subsequently
all changes to support that new UI are located in one place.
Page
Object Design Pattern advantages
1. There is a clean separation between test
code and page specific code such as locators and layout.
2. There is a single repository for the services
or operations offered by the page rather than having these services scattered
throughout the tests.
There are problems with this approach.
There is no separation between the test
method and the UI locators, both are intertwined in a single method. If the UI
changes, it must be changed in multiple places. It will very difficult to
identify locators where these are used in multiple tests.
Applying the page object techniques,
Now we handling two pages
1) Login
Page
2) Home
Page
1) Create
a Project ,Project structure will be like this
Create
a Package and keep all classes Page wise in this package. In our case it is
FBLoginPage and FBHomePage
Now create a methods and
Locators (Objects) for each class (Page)
a) For
each page we will create a separate class with constructor. We identify the
locators and keep all together
b) We
need to identify list methods or functionalities for these pages
Create FBLoginPage.java, (in this class we are creating constructor ,methods
and locators)
package com.pom;
import java.text.DateFormat;
import
java.text.ParseException;
import
java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.testng.annotations.Test;
public class FBLoginPage {
WebDriver driver;
By eUsrName =
By.name("email");
By Pwd =By.id("pass");
By SingIn = By.id("u_0_l");
public FBLoginPage(WebDriver
driver){
this.driver=driver;
}
public void setLoginUserName(String
userName){
driver.findElement(eUsrName).sendKeys("satishd77@gmail.com");
}
public void clickSignIn(){
driver.findElement(SingIn).click();
}
public void setLoginPassword(String
Password){
driver.findElement(Pwd).sendKeys("Tester1");
}
public String verifyTitle() throws
InterruptedException{
String sTitle=driver.getTitle();
System.out.println("Title"+sTitle);
return sTitle;
}
public void SingInToFB(String UserName,
String Password){
this.setLoginUserName(UserName);
this.setLoginPassword(Password);
this.clickSignIn();
}
}// GoogleLogin end
Create
FBHomePage.class (in this class we are creating constructor ,methods and
locators)
package com.pom;
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
publicclass
FBHomePage {
WebDriver driver;
By
HomeLink=By.xpath("//*[@id='u_0_1']");
By
updatestatus=By.xpath("//*[contains(text(),'Update
Status')]");
public
FBHomePage(WebDriver driver){
this.driver=driver;
}
publicvoid
ClickHomeLink(){
driver.findElement(HomeLink).click();
}
public String
VerifyNewMessage(){
String x=driver.getTitle();
System.out.println(x);
returndriver.getTitle();
}
}// GoogleHomePage
class end
Now we need to create a Test case for all the page objects based on
our functionalities(for this case we are
login and verifying the page tittle)
Create
FBLoginTestCase.java
package com.test;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.firefox.FirefoxDriver;
import
org.testng.Assert;
import
org.testng.annotations.BeforeClass;
import
org.testng.annotations.Test;
import
com.pom.FBLoginPage;
importcom.pom.FBHomePage;
publicclass FBLoginTestCase
{
WebDriver driver;
FBLoginPage fbLogin;
FBHomePagefbhomePage;
@BeforeClass()
publicvoid
Configuration(){
driver = new
FirefoxDriver();
driver.get("https://www.facebook.com/");
}
@Test()
publicvoid
HomePageTestCase() throws
InterruptedException{
fbLogin=new
FBLoginPage(driver);
String sVerifyTitle=fbLogin.verifyTitle();
Assert.assertTrue(sVerifyTitle.contains("Facebook
- Log In or Sign Up"));
fbLogin.SingInToFB("xyz@gmail.com", "89898ddd");
fbhomePage=newFBHomePage(driver);
fbhomePage.ClickHomeLink();
String homePageTitle=fbhomePage.VerifyNewMessage();
Assert.assertTrue(homePageTitle.contains("Facebook"));
}
}
Now we will execute
them using 'testng.xml' file. We will add the classes which we want to test.
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd">
<suitename="Suite"parallel="none">
<testname="Test">
<classes>
<classname="com.test.FBLoginTestCase"/>
</classes>
</test><!--
Test -->
</suite><!-- Suite
-->
Comments
Post a Comment