Wednesday, October 29, 2014

Chapter 25 -Selenium Page Object Model

Selenium Page Object Model

As we all know that Selenium acts on webelements with the help of their properties such ID, name, Xpath etc. Unlike QTP which has an inbuilt object repository(OR), Selenium has NOT got an inbuilt OR.
Hence we need to build OR which should also be maintainable and accessible on demand. Page Object Model(POM) is a popular design pattern to create an Object Repository in which each one of those webelements properties are created using a class file.

Advantages

  • Page Object model is an implementation where test objects and functions are seperated from each other and thereby keeping the code clean.
  • The Objects are kept independent of test script. An object can be accessed by one or more test scripts, hence POM helps us to create object once and use it multiple times.
  • Since objects are created once, it is easy to access as well as easy to update a particular property of an object.

POM Flow Diagram

Objects are created for Each one of the pages and methods are developed exclusively to access to those objects. Let us make use of http://calculator.net for understanding the same.
There are various calculators associated with it and each one of those objects in a particular page is created in a seperate class file as static methods and they all are accessed in 'tests' class file in which a static method would be accessing the objects.
Selenium IDE 145

Example

Let us understand it by performing a implementing POM for percent calculator test.
Step 1 : Create a simple class(page_objects_perc_calc.java) file within a package and create methods for each one of those object identifiers as shown below.
package PageObject;

import org.openqa.selenium.*;
 
public class page_objects_perc_calc 
{
    private static WebElement element = null;
 
    // Math Calc Link
 public static WebElement lnk_math_calc(WebDriver driver)
 {
      element = driver.findElement(By.xpath(".//*[@id='menu']/div[3]/a"));
      return element;
 }
 
    // Percentage Calc Link
 public static WebElement lnk_percent_calc(WebDriver driver)
 {
      element = driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a"));
      return element;
 }
 
    // Number 1 Text Box
 public static WebElement txt_num_1(WebDriver driver)
 {
      element = driver.findElement(By.id("cpar1"));
      return element;
 }
 
    // Number 2 Text Box 
 public static WebElement txt_num_2(WebDriver driver)
 {
      element = driver.findElement(By.id("cpar2"));
      return element;
 }
 
    // Calculate Button 
 public static WebElement btn_calc(WebDriver driver)
 {
      element = driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr/td[2]/input"));
      return element;
 } 
 
    // Result 
 public static WebElement web_result(WebDriver driver)
 {
      element = driver.findElement(By.xpath(".//*[@id='content']/p[2]/span/font/b"));
      return element;
 } 
}
Step 2 : Create a class with main and import the package and create methods for each one of those object identifiers as shown below.
package PageObject;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class percent_calculator 
{
  private static WebDriver driver = null;
  
  public static void main(String[] args) 
  {
   driver = new FirefoxDriver();
   driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
   driver.get("http://www.calculator.net");
  
   // Use page Object library now
   page_objects_perc_calc.lnk_math_calc(driver).click();  
   page_objects_perc_calc.lnk_percent_calc(driver).click();
   
   
   page_objects_perc_calc.txt_num_1(driver).clear();
   page_objects_perc_calc.txt_num_1(driver).sendKeys("10");
  
   page_objects_perc_calc.txt_num_2(driver).clear();
   page_objects_perc_calc.txt_num_2(driver).sendKeys("50");
  
   page_objects_perc_calc.btn_calc(driver).click();
   
   String result =  page_objects_perc_calc.web_result(driver).getText();  
   
   if(result.equals("5"))
   {
    System.out.println(" The Result is Pass");
   }
   else
   {
    System.out.println(" The Result is Fail");
   }
   
   driver.close();
  }
}

Output

The test is executed and the result is printed on console. Below is the snapshot of the same.
Selenium IDE 146

No comments: