Automation Using Selenium Webdriver
Showing posts with label POM Framework Using With Page Factory and With out Page Factory. Show all posts
Showing posts with label POM Framework Using With Page Factory and With out Page Factory. Show all posts

Sunday 2 October 2016

POM Framework Using With Page Factory and With out Page Factory


Page Object Model using Selenium Webdriver and Implementation


1- It is design pattern in which will help you to maintain the codeand code duplication,
  which is a crucial thing in Test automation.

2- You can store all locators and respective methods inthe separateclass and Call them from the test in which you have to use.So the benefit from this will be if any changes in Page the you do not have to modify the test simply modify the respective page and that all.

3- You can create a layer between your test script and application page, which you have to automate.

4- In other words, it will behave as Object repository where all locators are saved.

“Please don’t get confused if you are using Object repository concept then do not use Page Object model because both will serve the same purpose.
Implementation of Page Object model using Selenium WebdriverIf you want to implement
 Page Object model then you have two choices and you can use any of it.

1 – Page Object model without PageFactory

2- Page Object Model with Pagefactory.

Personally, I am a big fan of Page Factory, which comes with Cache Lookup feature, which allows me to store frequently used locators in catch so that performance will be faster. We will discuss both you can implement based on your preferences.

Page Object Model With out page Factory


  Let’s take very basic scenario which you can relate to any application. Consider you have login page where username, password, and login button is present.

I will create a separate Login Page, will store three locators, and will create methods to access them. Kindly refer below screenshot for reference.
Now I want to design test case so I can use the Login class, which I created and can call the methods accordingly.
This Login class will be used by all the scripts, which you will create in future so if any changes happened then you have to update only Login Class not all the test cases. This sounds good right.
Program for Page Object Model using Selenium Webdriver without Page factory
package com.Facebook.Pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;

/**
* @author jagan


* This class will store all the locator and methods of login page
*
*/
public class LoginPage 
{
WebDriver driver;
By username=By.id("user_login");
By password=By.xpath(".//*[@id='user_pass']");
By loginButton=By.name("wp-submit");
public LoginPage(WebDriver driver)
{
this.driver=driver;
}
public void loginToFacebook(String userid,String pass)
{

driver.findElement(username).sendKeys(userid);
driver.findElement(password).sendKeys(pass);
driver.findElement(loginButton).click();
}
public void typeUserName(String uid)
{
driver.findElement(username).sendKeys(uid);
}
public void typePassword(String pass)
{
driver.findElement(password).sendKeys(pass);
}
 public void clickOnLoginButton()
{
driver.findElement(loginButton).click();
}
}
TestCase using Page Object Model using Selenium Webdriver
package com.Facebook.Testcases;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

import com.Facebook.Pages.LoginPage;

/**
* @author Jagan
*
*/
public class VerifyFacebookLogin 
{
@Test
public void verifyFacebookLogin()
{
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.facebook.com");
LoginPage login=new LoginPage(driver):
login.clickOnLoginButton();
driver.quit();
}
}
Page Object Model using Selenium Webdriver with Page Factory
Selenium has built in class called PageFactory, which they mainly created for Page Object purpose, which allows you to store elements in cache lookup.

The only difference, which you will get without PageFactory and with PageFactory, is just initElement statement.Let us check the code and will see what changes required for with PageFactory Approach


Code for Page Object Model Using Selenium Webdriver using Page Factory

package com.Facebook.Pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
public class LoginPageNew 
{
WebDriver driver;
public LoginPageNew(WebDriver ldriver)
{
this.driver=ldriver;
}
@FindBy(id="user_login") 
@CacheLookup
WebElement username; 
@FindBy(how=How.ID,using="user_pass")
@CacheLookup
WebElement password;
@FindBy(how=How.XPATH,using=".//*[@id='wp-submit']")
@CacheLookup
WebElement submit_button;
@FindBy(how=How.LINK_TEXT,using="Lost your password?")
@CacheLookup
WebElement forget_password_link;
public void login_Facebook(String uid,String pass)
{
username.sendKeys(uid);
password.sendKeys(pass);
submit_button.click();
} 
}
                Test Case using Page Factory

package com.facebook.Testcases;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;
import com.wordpress.Pages.LoginPage;
import com.wordpress.Pages.LoginPageNew;
import Helper.BrowserFactory;
public class VerifyValidLogin 
{
 @Test
public void checkValidUser()
{
// This will launch browser and specific url 
WebDriver driver=BrowserFactory.startBrowser("firefox", "http://www.facebook.com");
// Created Page Object using Page Factory
LoginPageNew login_page=PageFactory.initElements(driver, LoginPageNew.class);
// Call the method
login_page.login_wordpress("jagan", "jagan768");
}
}