Sunday, October 7, 2012

Webdriver - Page Object Pattern

What I am going to discuss here is Page Object Pattern with Page Factory for Webdriver. Let's not talk much about theory and let's see how to implement it. let's take one scenario and see how we can automate it without and with Page Object Pattern Scenario:
  1. Open http://newtours.demoaut.com/ site 
  2. Login to the site. 
  3. Log out. Now if we are not using any design patten the testcase will look like below
@Test
public void testLogin() {
 driver.findElement(By.id("userName")).clear();
 driver.findElement(By.id("userName")).sendKeys("testuser33");
 
 driver.findElement(By.id("password")).clear();
 driver.findElement(By.id("password")).sendKeys("password123");
 
 driver.findElement(By.id("login")).click();
 
 driver.findElement(By.linkText("SIGN-OFF")).click();

}
Now let's see how the code will look like if we will have to use the Page Object Pattern with Page Factory. LoginPage.java - contains Login page web elements and functions

package Pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;

public class LoginPage {
 
 final WebDriver driver;
 
 @FindBy(how = How.NAME, using = "userName")
 private WebElement usernameEditbox;
 
 @FindBy(how = How.NAME, using = "password")
 private WebElement passwordEditbox;
 
 @FindBy(how = How.NAME, using = "login")
 private WebElement singinButton;

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

 public void enterUsername(String login) {
  usernameEditbox.clear();
  usernameEditbox.sendKeys(login);
 }

 public void enterPassword(String password) {
  passwordEditbox.clear();
  passwordEditbox.sendKeys(password);
 }

 public void clickSigninButton() {
  singinButton.click();
 }

 public FindFlightPage login(String login, String password) {
  enterUsername(login);
  enterPassword(password);
  clickSigninButton();
  return PageFactory.initElements(driver, FindFlightPage.class);
 }
}

FindFlightPage.java - contains Find Flight page web elements and functions
package Pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;

public class FindFlightPage {

 final WebDriver driver;
 
 public FindFlightPage(WebDriver driver) {
  this.driver = driver;
 }
 
 @FindBy(how = How.LINK_TEXT, using="SIGN-OFF")
 private WebElement signOff;
 
 public LoginPage singOff(){
  signOff.click();  
  return PageFactory.initElements(driver, LoginPage.class);
 }
}

LoginTest.java  - Actual class which contains tests for login page
package Tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import Pages.FindFlightPage;
import Pages.LoginPage;

public class LoginTest {

 WebDriver driver;
 private static String login = "testuser33";
 private static String pass = "password123";

 @BeforeClass
 public void setUp() throws Exception {
  driver = new FirefoxDriver();
  driver.get("http://newtours.demoaut.com/mercuryreservation.php");
 }

 @Test
 public void testLogin() {
 
  LoginPage loginPage = PageFactory.initElements(driver, LoginPage.class);
  FindFlightPage findPage = loginPage.login(login, pass);
  
  loginPage = findPage.singOff();
 }
 
 @AfterClass
 public void tearDown() throws Exception {
  driver.quit();
 }
}

4 comments:

Anonymous said...

Thanks for that!
proved really really helpful to understand page object pattern!
best so far for a rookie like me :)

Unknown said...

Hello,
Thanks for the details. If the login fails, how do you handle it. Could you please explain how the flow works? Also if the login page has additional functionalities other than login (eg, new user sign up etc), do we have to write a new class or a new method in same class?

Thanks!
-a confused new user :-)

rohitnirantar4u said...

Can we use webdriver listeners with Page Object Factory?

Anonymous said...

Well explained Gaurang.

Thanks,
Dhiraj

Post a Comment