Monday, May 25, 2020

Page Object Model (POM) | Design Pattern | Page Factory

Page Object Model (POM) is a design pattern, popularly used in test automation that creates
 Object Repository for web UI elements. The advantage of the model is that it reduces code duplication and improves and enhance test maintenance.
Design Patterns in Framework defines the standard of writing code. They help you in maintaining re-usability and readability. POM (Page Object Model) is one such design pattern.
Page object model (POM) can be used in any kind of framework such as modular, data-driven, keyword driven, hybrid framework etc. A page object is an object-oriented class that serves as an interface to a page of your Application Under Test(AUT). 
 Page Object Model, all element locators are being managed in separate directory and can be updated easily without any change in test cases.

What is the difference between Page Object Model (POM) and Page Factory:

Page Object is a class that represents a web page and hold the functionality and members. Page Factory is a way to initialize the web elements you want to interact with within the page object when you create an instance of it.

Advantages of Page Object Model Framework:

  • Code re usability – We could achieve code reusability by writing the code once and use it in different tests.
  • Code maintainability – There is a clean separation between test code and page specific code such as locators and layout which becomes very easy to maintain code. Code changes only on Page Object Classes when a UI change occurs. It enhances test maintenance and reduces code duplication.
  • Object Repository – Each page will be defined as a java class. All the fields in the page will be defined in an interface as members. The class will then implement the interface.
  • Readability – Improves readability due to clean separation between test code and page specific code

Create a package and java class as shown as above.
Object-Home page
package objectRepositoryPages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class JavaQaEng {
public WebDriver driver;
By searchBox = By.xpath("//input[@class='gsc-input']");
By enterSearchBox = By.xpath("//input[@name='q']");
By clickSearchBox = By.xpath("//input[@value='Search']");
//Creating constructor
public JavaQaEng(WebDriver driver) {
this.driver= driver;
}
public void searchBox(String Box) {
driver.findElement(searchBox).sendKeys(Box);
}
public void enterSearchBox(String Content) {
driver.findElement(enterSearchBox).sendKeys(Content);
}
public void clickSearch() {
driver.findElement(clickSearchBox).click();
}
}

Base class -
package objectRepositoryPages;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import io.github.bonigarcia.wdm.WebDriverManager;
public class TestBase {
public static WebDriver driver ;
@BeforeMethod
public void invokeBrowser() {
//setup the chromedriver using WebDriverManager
WebDriverManager.chromedriver().setup();
//Create driver object for Chrome
driver = new ChromeDriver();
// Maximize the browser window
driver.manage().window().maximize();
//Delete all cookies
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//Navigate to a URL
driver.get("https://javaqaeng.blogspot.com/");
}
@AfterMethod(enabled=false)
public void tearDown() {
driver.close();
}
}

Test class:

package objectRepositoryPages;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.testng.annotations.BeforeMethod;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertTrue;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;

public class JavaQaEngTest extends TestBase {


  @Test
  public void VerifyTest() {
  
  JavaQaEng jq = PageFactory.initElements(driver, JavaQaEng.class);
//   JavaQaEng jq = new JavaQaEng(driver);
  jq.searchBox (" ");
  jq.enterSearchBox("Automate me");
  jq.clickSearch();
  
// //Compare page title
String actualTitle = driver.getTitle();
String expectedTitle = "FREE Professional Java Developer, Automation Engineer, Business Analyst Training : Search results for Automate me";
//Get title of page - printing title of page
System.out.println("My page title is :" +  actualTitle);


//Assert my title
assertEquals(expectedTitle,actualTitle);
//Another way of assertion - it does not need to all title for assertion
assertTrue(driver.getTitle().contains("Automate me"));
// OR Verify expected page title and actual page title is same  
Assert.assertTrue(actualTitle.equalsIgnoreCase(expectedTitle ),"FREE Professional Java Developer, Automation Engineer, Business Analyst Training : Search results for Automate me");  
//OR you can use if/else - please do not confuse - it is optional
if(driver.getTitle().contains("FREE Professional Java Developer"))
    //Pass
    System.out.println("Page title contains \"FREE Professional Java Developer\" ");
else
    //Fail
    System.out.println("Page title doesn't contains \"FREE Professional Java Developer\" ");
  
  }
  
  

}
/*
 * My page title is :FREE Professional Java Developer, Automation Engineer, Business Analyst Training : Search results for Automate me
Page title contains "FREE Professional Java Developer" 
PASSED: VerifyTest

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================/
 */



No comments:

Post a Comment