MainMenu

Home Core Java Overview Maven Tutorials

Saturday, 14 January 2017

Pageobject Model in Selenium



Page Object Model


For Video Tutorial : Move on Youtube Channel


Note : Select the playlist as per your need & move with number sequence


For Video : CLICK HERE For Part1


For Video : CLICK HERE For Part2




1.)Page Object Model is just a design pattern not a framework.
2.)As the name says, every Webelement will be inside Pages, all locator & methods.
A Page Object is a Design Pattern became popular for :
1.)Enhancing test maintainance
2.)Reducing code duplication
3.)Generally It Maps UI Page to a class.

How it Works?


1.)Each Page is defined as its own class.
2.)Actions(Including navigation) as represented as functions.
3.)Each Function returns a new page object.
4.)Tests Only talk to the page objects.
5.)Page objects only talk to driver and that particular class.

Advantages


1.)Simple and clear test
2.)Script will be more readable format
3.)Using POM you will be able to achieve below three features :
Easy to maintain, readable format & reusable scripts.

Ways to Implement :

There are mainly two ways to implement it.
1.)Using normal approach
2.)Using PageFactory and @findBy
1.)
Using normal approach:

Code:
This is a wordpress login page.
Here we have created a class, inside it we located each element in seprate method.

package com.login;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class Loginpage
{
WebDriver driver;
public Loginpage(WebDriver driver)
{
this.driver = driver;
}
By username = By.xpath("/html/body/div[1]/form/p[1]/label/input");
By password = By.xpath("/html/body/div[1]/form/p[2]/label/input");
By loginbutton = By.xpath("/html/body/div[1]/form/p[4]/input[1]");
public void un(String uid)
{
driver.findElement(username).sendKeys(uid);
}
public void pd(String pwd)
{
driver.findElement(password).sendKeys(pwd);
}
public void lb()
{
driver.findElement(loginbutton).click();
}
}

Here we have created another class to run the test


package com.logintest;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.login.Loginpage;
public class Logintest
{
WebDriver driver;
public static void main(String args[])
{
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.iamchandan.com/wp-admin");
Loginpage obj = new Loginpage(driver);
obj.un("cchauhan707");
obj.pd("ttest@123");
obj.lb();
}
}

2.)
Using PageFactory and @findBy

Now we will create page object model using page factory

First of all i will create a class & locate my all the elements

package com.loginpage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
public class Log
{
WebDriver driver;
@FindBy(xpath = "/html/body/div[1]/form/p[1]/label/input")
WebElement username ;
@FindBy(how = How.XPATH, using= "/html/body/div[1]/form/p[2]/label/input")
WebElement password;
@FindBy(xpath = "/html/body/div[1]/form/p[4]/input[1]")
WebElement loginbutton;
public Log(WebDriver driver)
{
this.driver = driver;
}
public void ennterusername(String uname)
{
username.sendKeys(uname);
}
public void enterpassword(String pwd)
{
password.sendKeys(pwd);
}
public void clickloginbutton()
{
loginbutton.click();
}
}

After that i will implement pageFactory & using pageFactory object i will access the methods :

package com.logtest;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
import com.loginpage.Log;
public class Logtest
{
static WebDriver driver;
public static void main(String args[])
{
driver = new FirefoxDriver();
driver.get("http://iamchandan/wp-admin");
driver.manage().window().maximize();
Log object = PageFactory.initElements(driver, Log.class);
object.ennterusername("cchauhan707");
object.enterpassword("ttest@123");
object.clickloginbutton();
}
}
Note : In different framework it can be used in different ways but core concept will be same.
Thanks
:) Tags :

Page factory in selenium

how to create page object model in selenium

What is page object model in selenium

No comments:

Post a Comment