In this Framework, we will have a Page Class and for every Page class we will have respective Test Class associated with it. Page c...

Simple Selenium Framework Using The Concept of Page Object Model - Java


In this Framework, we will have a Page Class and for every Page class we will have respective Test Class associated with it.

Page class will have all the WebElement's of that particular page loaded inside and also the actions to perform on that respective WebElement's.
Ex: Google Home Page is considered to be one page and we call it as GooglePage class.


Test class will take all the actions methods from Page class and make it as one Test Method.

Ex: Scenario 
  • Open Google Chrome
  • Enter www.google.com
  • Enter "Hello" in search box 
  • Hit Enter button in keyboard.
In the above Scenario all the bulletins are Actions and all this Actions as a whole/together is called as Test(Test Method).

Below is the complete Framework.

// This is a Test Class, which is inside a Package called "purush_TestPackage".
// All the Test Methods will sit inside this Package or we can group them according to our requirements alternatively.  

GoogleTest class

package purush_TestPackage;


import org.testng.annotations.Test; 
import purush_PageClass.GooglePage; 
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;


public class GoogleTest
{
    //public WebDriver driver = new InternetExplorerDriver();
      public WebDriver driver = new ChromeDriver();

  GooglePage objLoginPage = new GooglePage(driver);
 
  @Test
  public void EnterKeyword()
  {
        objLoginPage.EnterSearchKeyword();
       
  }
 
  @Test
  public void PressEnterKey()
  {
        objLoginPage.PressEnterKey();
  }
 
  @BeforeTest
  public void beforeTest()
  {
        driver.navigate().to("http://www.google.com");
        driver.manage().window().maximize();
  }
  
  @AfterTest
  public void afterTest()
  {
        driver.quit();
  }

}

// This is the Page Class, where all the WebElement's of particular page sits, and also the action's to perform on those WebElement's. 

GooglePage Class

package purush_PageClass;

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

public  class GooglePage
{
      WebDriver driver;
      By SearchBox= By.xpath("/html//input[@id='lst-ib']");
     
      public GooglePage(WebDriver driver)
      {
           
            this.driver=driver;
      }
     
      public  void EnterSearchKeyword()
      {
            driver.findElement(SearchBox).sendKeys("Hello");
           
      }

      public  void PressEnterKey()
      {
            driver.findElement(SearchBox).sendKeys(Keys.ENTER);   
      }

}


This is how Framework Structure will look like.



1 comment: