Friday, December 26, 2014

PageFactory in Selenium WebDriver



Why Page Factory?

·      Page Factory is an inbuilt page object model concept for Selenium Webdriver

·      Here as well we follow the concept of separation of Page Object repository and Test methods.


·        To support Page Object Pattern  the package org.openqa.selenium’ has a PageFactory Class
·        To import Pagefactory class just import
 org.openqa.selenium.support.PageFactory;

·         PageFactory class we use annotations @FindBy to find WebElement.
·        @FindBy can accept tagName, partialLinkText, name, linkText, id, css, className, xpath as attributes.

See the below screenshot



Let take our the previous POM Example. but this time am trying to use PageFactory Class to find elements   



In this program am going to do the following scenarios.

1.       Am going to get page title
2.       Assert that page if its correct or not
3.       Clicking on about link
4.       Check the author name is correct or not

aboutpage.java

package pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class aboutpage {
WebDriver driver;
//By aboutlink = By.xpath("//table//tr[@class='heading3']");
@FindBy(xpath="//*[@id='Profile1']/div/dl/dt/a")
WebElement aboutlink;
@FindBy(xpath="//*[@id='maia-main']/div/h1")
WebElement authorname;
public aboutpage(WebDriver driver)
{
this.driver=driver;
//This initElements method will create all WebElements
PageFactory.initElements(driver, this);
}
public void clickaboutlink()
{
//driver.findElement(aboutlink).click();
aboutlink.click();
}
public String getauthorname()
{
//return driver.findElement(authorname).getText();
return authorname.getText();
}

}


homepage.java

package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class homepage {
            public WebDriver driver;
           
            @FindBy(xpath="//*[@id='header-inner']/div[1]/h1")
            //By title=By.xpath("//*[@id='header-inner']/div[1]/h1");
            WebElement title;
            //intialize driver using constructor
            public homepage(WebDriver driver)
            {
                        this.driver=driver;
                        PageFactory.initElements(driver, this);
            }
           
            public String getTitlefromHomePage()
            {
                       
                        //return driver.findElement(title).getText();
                        return title.getText();
                       
            }
           

}

Testcase

pom_testcase1.java

package tests;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import pages.aboutpage;
import pages.homepage;

public class pom_testcase1 {
           
            //initialize driver
            WebDriver driver;
            //creating object for pages
            homepage hp;
            aboutpage ap;
           
             @BeforeTest
                public void setup(){
                    driver = new FirefoxDriver();
                    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                    driver.get("http://www.automationplace.blogspot.in");
                }
             
             @Test
             public void test_homepage()
             {
                         hp=new homepage(driver);
                         //verify the title
                         String title=hp.getTitlefromHomePage();
                         //this will verify my page title
                         Assert.assertTrue(title.contains("Automationplace"));
                         System.out.println(title);
                         ap=new aboutpage(driver);
                         //click on about me link in homepage
                         ap.clickaboutlink();
                         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                         String authorname=ap.getauthorname();
                         //check author name in about page
                         Assert.assertTrue(authorname.contains("Prashanth KK"));
                         System.out.println(authorname);
                         System.out.println("Clicked about like");
                         
             }
           

}

That’s it…Run the above program.

See the changes without using PageFactory and with using PageFactory class.in the below image






Hope you like this post.

Hit LIKE button on facebook.

No comments:

Post a Comment