Tuesday 13 March 2018

Page Object Model (POM) Framework in Selenium

Why Page Object Model (POM) needed?

Page Object Model (POM) is one type of automation framework. When you are using selenium for automation then you come across scenario that many page objects (UI Element) are used in many test cases. You have to declare all that page objects for all the test cases. It is not good approach for automation to duplicate those page objects. Lots of memory also occupy due to this page objects duplication. To over come this problem we need to use POM structure for automation framework.


What is POM?
  • POM is framework (design pattern) or you can say its framework to create Object Repository for page objects (UI Element).
  • We need to create one separate class for store page objects in POM framework.
  • This class contains all the page objects of particular page.
  • We can use that page objects through object of that class.

Advantages

  • Page object repository is independent from actual code. So, we can reuse that repository in other project also.
  • Code flow and UI element identification is separate and due to that code will become more readable.
  • Code becomes less and more optimized.
  • We can easily integrate POM with JUnit/TestNG.

Implementation

As we discussed that in POM, we have to create separate class for page object repository.

Let's take an example of login page. In General, Login Page contains below three UI Elements.
  1. Username
  2. Password
  3. Login Button
First you have to create one package called 'PageObjects' then create one class called 'Login_Page' under that package.

Now, you have to add all three page objects of Login page into that class.

Your Login_Page class look like below.

package PageObjects;

import java.util.List;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindAll;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;

public class Login_Page 
{
 
 public Login_Page(WebDriver driver)
 {
  PageFactory.initElements(driver, this);
 }

 // list of all Login page objects
 
 @FindBy(id="Username")
 public WebElement Username;
 
 @FindBy(id="Password")
 public WebElement Password;
 
 @FindBy(xpath = "//input[@id='Loginbtn']")
 public WebElement Login;

}

You can see one constructor is there in above class. I have used below method in constructor.

PageFactory.initElements(driver, this);

This is used for initialize all page objects when we create object of this class.

Now, you can use all three page objects into your Login test case through 
Object of Login_Page class.

Note : You have to import PageObjects package into your class of Login test case.

public class Login_Test 
{
 
 public void Login()
 {
  WebDriver driver = new FirefoxDriver();
  
  driver.get("URL of Login Page");
  
  Login_Page objLogin_Page = new Login_Page(driver);
  
  objLogin_Page.Password.sendKeys("Username");
  
  objLogin_Page.Password.sendKeys("Passsword");
  
  objLogin_Page.Login.click();
  
 }

}

Here, you can see i have used all three page objects of Login_Page class through object of that class.

Same way we can use objects of other classes of PageObjects package into our framework.

Please add comment of you have any questions.

3 comments:

  1. Nice article.Learn the POM So easily.it was very useful and informative..i suggest this blog to my friends..keep upate.Top selenium training institutes in chennai
    Selenium Testing Course
    software testing course in velachery chennai

    ReplyDelete
  2. Thanks for visit my blog. Sure I will keep update.

    ReplyDelete
  3. nice course. thanks for sharing this post this post harried me a lot.
    Selenium Training in Gurgaon

    ReplyDelete

Popular