Thursday, December 25, 2014

Page Object Model(POM) in Selenium Webdriver




Why POM is Important?
1.       For Web UI Automation, We just finding the elements and perform some operation on it.

WebElement username = driver.findElement(By.name("Email")); -Finding elements
WebElement password=driver.findElement(By.name("Passwd")).click(); - Click on button

In the above example we just finding elements and clicking on button.

2.        So its very simple right. Some time you are using the same page in 15 test scripts. with any change in that element, you need to change all 15 scripts.as the result its takes lot of time to change all the elements in scripts and also makes error prone.
3.       The Better approach is we create a separate class file ,if any changes in elements we just go and change in that appropriate class file.
4.       This approach is called as POM(Page object Model).its make more readable,Maintainable and reusable




What is POM?
1.       Page Object Model is a design pattern to create Object Repository for web UI elements.
Under this model, for each web page in the application there should be corresponding page class.
2.        This Page class will find the WebElements of that web page and also contains Page methods which perform operations on those WebElements.

     Advantage of POM
1.       Easy to understand.
2.       Reduce code duplication
3.       object repository is independent of testcases, so we can use the same object repository for a different purpose with different tools. For example, we can integrate POM with TestNG/JUnit 

Here we using to pages for the testcase
1.       aboutpage
2.       Homepage

Am creating a new package named as pages. In that package am creating two classes
aboutpage.java
homepage.java

After all your project look like below.



In this program am test case has below scenario.
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

This we are going to achieve using POM.

1.aboutpage.java
package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class aboutpage {
      
       WebDriver driver;
       By aboutlink=By.xpath("//*[@id='Profile1']/div/dl/dt/a");
       By authorname=By.xpath("//*[@id='maia-main']/div/h1");
      
       public aboutpage(WebDriver driver)
       {
              this.driver=driver;
       }
      
       public void clickaboutlink()
       {
              //clicking about link
              driver.findElement(aboutlink).click();
       }
      
       public String getauthorname()
       {
              //get text
              return driver.findElement(authorname).getText();
       }
      
      

}

homepage.java:
package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

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

}

Creating seeprate package for testcase. here am creating tests to hold testcases and creating a new class pom_testcase1.java


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");
               
        }
      

}


Note : If any chances in the web element I don’t need to change anything in the testcases.i directly go and change the page class.

As the result the code is :
  • ·         More readable
  • ·         Maintainable
  • ·         reusable










3 comments:

  1. hello sir i am not run the test case on chrome browser or i.e only run the test case Firefox browser. when we run testcase on different browser it gives exception.
    and
    how create page object model using property file and all the value pick from excel file
    so sir please help me

    ReplyDelete
  2. Hi Great Article. How do we handle fluent waits, explicit waits on expected conditions when using page factory because when some elements appear dynamically.
    check selenium in better hands on way selenium training in chennai

    ReplyDelete