Tuesday, 8 September 2015

Page Object Model (POM)



This is widely used concept in the world on selenium webdriver also known as POM.
With the use of POM, we can increase readability of code also it is easy to maintain and user can reuse it.

        Basically POM is design pattern to create object repository (OR).
        In POM, we are maintaining different pages for different web pages. This page class
        contains      
        Elements along with page methods which perform operation on web element.

The main advantage of Page Object Model is that if the UI changes for any page, it don’t require us to change any tests, we just need to change only the code within the page objects.

Below I have given example of POM

      //Class for page objects (elements) 

 public PageName(WebDriver driver)  
 {        
     this.driver = driver;  
    
    //This initElements method will create all WebElements  
    //PageFactory.initElements(driver, this);  
     PageFactory.initElements(new AjaxElementLocatorFactory(driver,     
     Constants.WebDriverWaitDuration), this);  
 }  
        
        
 /*  
  * All WebElements are identified by @FindBy annotation  
  * @FindBy can accept tagName, partialLinkText, name, linkText, id, css, className, xpath as      
  * attributes.  
  */        
   
   
   
   
 Say we have text box  
        
 @FindBy(Locator="LocatorValue")  
 public WebElement NameForElement;  
        
 //Method to enter input  
 public void EnterInput(String text)  
 {  
      NameForElement.clear();  
      NameForElement.sendKeys(text);  
 }  
   
 //If Element is button  
 public void ClickOnButton()  
 {  
      NameForElement.click();  
 }  
   
 //If Element is drop-down  
 public void SelectValueFromDropDown(String visibletext)  
 {  
      new Select(NameForElement).selectByVisibleText(visibletext);  
 }  
   

Lets consider two different cases of writing code as below, in case 1  there no use of POM concept and in case 2 I have used POM

       Case 1 
 Without POM 

 driver.findElement(By.id("username")).sendKeys("testuser");  
 driver.findElement(By.id("password")).sendKeys("testuser");  
 driver.findElement(By.id("signIn")).click();      
   
   

Case 2 
With POM 
Say we are working with login and elements resides in loginpage 
Code will look like below –  

 loginpage. EnterUserName(“testuser”);  
 loginpage. EnterPassword(“testuser”);  
 loginpage. ClickOnLoginButton();  
   

So suppose I have used login code in 10 classes as per case 1, and if there is change in locator value. In that case I have to make changes in all 10 classes but if code is written as per case 2, I have to make changes at one place only where all elements are resides.


Below I have tried to list out some benefits of POM

  1. Code is more readable and robust.
  2. Avoid duplication of code.
  3. Improves the maintainability of tests (Useful in Agile methodology based projects).
  4. Simple and clear tests.
  5. Good support of tests, because everything is stored in one place.
  6. The UI changes, the fix need only be applied in one place.
  7. Code re-use: Able to use the same page object in a variety of tests cases.

 

2 comments:

  1. Hello bro. I discovered your blog by searching online programming tutorials in Bing Search Engine. This is an extremely well written article. I’ll make sure to bookmark it and return to learn more of your helpful information. Thank you for the post. I’ll definitely comeback.

    ReplyDelete
  2. Far and away the best prize that life offers is the chance to work hard at work worth doing.
    _________________________
    This is football game,you can try to play for free:Cheap FIFA 16 Coins and FIFA 16 Coins

    ReplyDelete