25
Jul

Page Object Model – framework to organize page elements and actions on page objects

One can always interact with page objects using Selenium webdriver something like this:

public class myFirstWebDriver()
{

@Test
public void validate_login_page()
{
WebDriver wd = new FirefoxDriver();
wd.manage().timeouts().implicitlywait(10, TimeUnit.SECONDS);
wd.get(“http://www.google.co.in”);

wd.findElement(By.name(“uid”)).sendKeys(“Defectracker”);
we.findElement(By.xpath(“xpath”)).click();
}
}

With above approach, i can work with same page objects, which may be used by other tests. Thus “webdriver.findElement(…” is scripted multiple times across test cases.

This approach of working with page objects, make it difficult to update test cases, should there be change in object identification.

One of the solution to overcome this approach is Page Object Model (POM) framework which defines specific rules to be followed while automating tests using Selenium WebDriver. Key highlights of POM are:

1. It suggests creation of “Page classes” which defines all page objects and operations on such elements on a specific page. eg.

public class loginPage()
{
WebDriver driver;

public HomePage(WebDriver driver) {
this.driver = driver;
}

// identify all the elements on this page eg.
@FindBy(id=”home-menu-entry”)
WebElement homeMenuEntry;

and so on…..

// define actions on above page objects eg.
public void clickHomeMenuEntry() {
homeMenuEntry.click();
}

and so on….
}

2. It suggests Operations defined for webelements / objects of a page needs to be structured in workflow class eg.

public class loginWorkflow()
{
// Initialize the webdriver eg.
WebDriver driver;
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(“URL to application under test”);

// Instantiate Page objects
loginPage pageObjectLoginPage = PageFactory.initElements(driver, loginPage.class);

// Structure page objects actions to execute script workflow eg.
pageObjectLoginPage.setUserName();
pageObjectLoginPage.setPassword();
pageObjectLoginPage.clickLogin();

and so on…..

}

with this approach all scripts will still work if there is change in any of page object.