Page Object Model in Selenium WebDriver Step by Step Guide

Page Object Model in Selenium WebDriver Projects has become crucial Automation Framework in different organization and many software Testers are implementing Selenium Page Object Model in their project why because it is easy to maintain Test Scripts, easy to understand the Test Execution, easy to maintain Page Object Repository and last but not least reduces the duplication of code.

Recommended Articles

  1. Write Test Cases PASS OR FAIL in Excel with SELENIUM
  2. Create Object Repository in Selenium Project.
  3. Page Factory Framework Implementation Examples.
  4. Start APPIUM server programmatically.


Page Object Model in Selenium WebDriver

The main advantage of Page Object Model in Selenium WebDriver is easy to maintain Page Objects, easy to avoid duplicate of Code. Duplicate code in Selenium cause duplicate of different Functionalities with this duplicate code which results in multiple time repetition of duplicate element locators. You will learn How to implement Page Object Model in Selenium using Simple Examples as explained below. In this examples I’m using simple Login.java class, so that you can implement for other classes in your Selenium Projects.

In this Page Object Model, you need to create two packages

One for Page Objects Second one for Page Test Cases Implementation as below example.

How to IMPLEMENT?

STEP 1: First Create JAVA Project
STEP 2: Configure required JAR files in Build Path
STEP 3: Create two packages under src folder
STEP 4: One Package for Page Object (Object Repository)
STEP 5: Second Package for Page Test Classes.


Login.java

Above class created under page Objects package and code will be as follows
package com.selenium.pageObjects; 
import org.openqa.selenium.*; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement;
 
public class LoginPage {
 
        private static WebElement element;
 
    public WebElement setUserName(WebDriver driver){
 
         element = driver.findElement(By.id("uName"));
 
         return element;
 
         }
 
     public WebElement setPassword(WebDriver driver){
 
         element = driver.findElement(By.id("password"));
 
         return element;
 
         }
 
     public WebElement clickOnSignin(WebDriver driver){
 
         element = driver.findElement(By.id("submit"));
 
         return element;
 
         }
 
}

WebDriver is being passed as a Method Arguments for each methods, so that Selenium is able to locate the Web Page elements on the specified browser using driver object. In above code element is returned, because method is not a void that is the reason we should return the values inside the method to use in another class or somewhere.so that an Action can be performed on elements. Method is declared as Public WebElement, so that method can be used in any other methods without instantiate the class.Now let’s create Page Test Case class under page Actions package as below for Login Page.

LoginTest.java
package com.selenium.Actions;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.selenium.pageObjects.LoginPage ;

public class LoginTest {

     private static WebDriver driver = null;
 LoginPage login=new LoginPage();

//Main Method
   public static void main(String[] args) {

 System.setProperty("webdriver.gecko.driver","E:\\geckodriver\\geckodriver.exe");
     driver = new FirefoxDriver();
     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
     driver.get("http://www.yourloginurl.com");

     // Use page Object library now

     login.setUserName(driver).sendKeys("rajesh_3434");
     login.setPassword(driver).sendKeys("makeindia");
     login.clickOnSignin(driver).click();

     System.out.println(" User Login Successfully,My Account screen is open");

      driver.quit();

     }

}





Now create testng.xml file to run your Test Actions classes as specified in your testng.xml file,Just right click on it and Run As – TestNG Tests. 

 You will see Test script Execution on Firefox Browser as provided login credentials. 

 Thank you for reading my blog ,Please share this post in case you feel informative and provide your valuable comments on this post.

Post a Comment

0 Comments