Tuesday 30 July 2013

Webdriver - Page Object Pattern


Page Object Pattern, the term that selenium users keep buzzing. Page object is a design pattern that can be implemented as a selenium best practices. The functionality classes (PageObjects) in this design represent a logical relationship between the pages of the application.
  • The Page Object pattern represents the screens of your web app as a series of objects and encapsulates the features represented by a page.
  • It allows us to model the UI in our tests.
  • A page object is an object-oriented class that serves as an interface to a page of your AUT.
More on Page Object Pattern at Selenium wiki. Some of the advantages of page object pattern as listed below,
  • Reduces the duplication of code
  •  Makes tests more readable and robust
  • Improves the maintainability of tests, particularly when there is frequent change in the AUT. (Useful in Agile methodology based projects)
Enough of theory, lets get into practical implementation.
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 
01.@Test
02.public void testLogin() {
03.driver.findElement(By.id("userName")).clear();
04.driver.findElement(By.id("userName")).sendKeys("testuser33");
05. 
06.driver.findElement(By.id("password")).clear();
07.driver.findElement(By.id("password")).sendKeys("password123");
08. 
09.driver.findElement(By.id("login")).click();
10. 
11.driver.findElement(By.linkText("SIGN-OFF")).click();
12. 
13.}
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

01.package Pages;
02. 
03.import org.openqa.selenium.WebDriver;
04.import org.openqa.selenium.WebElement;
05.import org.openqa.selenium.support.FindBy;
06.import org.openqa.selenium.support.How;
07.import org.openqa.selenium.support.PageFactory;
08. 
09.public class LoginPage {
10. 
11.final WebDriver driver;
12. 
13.@FindBy(how = How.NAME, using = "userName")
14.private WebElement usernameEditbox;
15. 
16.@FindBy(how = How.NAME, using = "password")
17.private WebElement passwordEditbox;
18. 
19.@FindBy(how = How.NAME, using = "login")
20.private WebElement singinButton;
21. 
22.public LoginPage(WebDriver driver) {
23.this.driver = driver;
24.}
25. 
26.public void enterUsername(String login) {
27.usernameEditbox.clear();
28.usernameEditbox.sendKeys(login);
29.}
30. 
31.public void enterPassword(String password) {
32.passwordEditbox.clear();
33.passwordEditbox.sendKeys(password);
34.}
35. 
36.public void clickSigninButton() {
37.singinButton.click();
38.}
39. 
40.public FindFlightPage login(String login, String password) {
41.enterUsername(login);
42.enterPassword(password);
43.clickSigninButton();
44.return PageFactory.initElements(driver, FindFlightPage.class);
45.}
46.}
FindFlightPage.java - contains Find Flight page web elements and functions  


01.package Pages;
02. 
03.import org.openqa.selenium.WebDriver;
04.import org.openqa.selenium.WebElement;
05.import org.openqa.selenium.support.FindBy;
06.import org.openqa.selenium.support.How;
07.import org.openqa.selenium.support.PageFactory;
08. 
09.public class FindFlightPage {
10. 
11.final WebDriver driver;
12. 
13.public FindFlightPage(WebDriver driver) {
14.this.driver = driver;
15.}
16. 
17.@FindBy(how = How.LINK_TEXT, using="SIGN-OFF")
18.private WebElement signOff;
19. 
20.public LoginPage singOff(){
21.signOff.click(); 
22.return PageFactory.initElements(driver, LoginPage.class);
23.}
24.}
LoginTest.java  - Actual class which contains tests for login page 

01.package Tests;
02. 
03.import org.openqa.selenium.WebDriver;
04.import org.openqa.selenium.firefox.FirefoxDriver;
05.import org.openqa.selenium.support.PageFactory;
06.import org.testng.annotations.AfterClass;
07.import org.testng.annotations.BeforeClass;
08.import org.testng.annotations.Test;
09. 
10.import Pages.FindFlightPage;
11.import Pages.LoginPage;
12. 
13.public class LoginTest {
14. 
15.WebDriver driver;
16.private static String login = "testuser33";
17.private static String pass = "password123";
18. 
19.@BeforeClass
20.public void setUp() throws Exception {
21.driver = new FirefoxDriver();
23.}
24. 
25.@Test
26.public void testLogin() {
27. 
28.LoginPage loginPage = PageFactory.initElements(driver, LoginPage.class);
29.FindFlightPage findPage = loginPage.login(login, pass);
30. 
31.loginPage = findPage.singOff();
32.}
33. 
34.@AfterClass
35.public void tearDown() throws Exception {
36.driver.quit();
37.}
38.}