Webdriver PageObject Implementation using PageFactory in Java
- by kamal
here is what i have so far:
A working Webdriver based Java class, which logs-in to the application and goes to a Home page:
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.AssertJUnit;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class MLoginFFTest {
private WebDriver driver;
private String baseUrl;
private String fileName = "screenshot.png";
@BeforeMethod
public void setUp() throws Exception {
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.http.phishy-userpass-length", 255);
profile.setAssumeUntrustedCertificateIssuer(false);
driver = new FirefoxDriver(profile);
baseUrl = "https://a.b.c.d/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testAccountLogin() throws Exception {
driver.get(baseUrl + "web/certLogon.jsp");
driver.findElement(By.name("logonName")).clear();
AssertJUnit.assertEquals(driver.findElement(By.name("logonName"))
.getTagName(), "input");
AssertJUnit.assertEquals(driver.getTitle(), "DA Logon");
driver.findElement(By.name("logonName")).sendKeys("username");
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("password")).sendKeys("password");
driver.findElement(By.name("submit")).click();
driver.findElement(By.linkText("Account")).click();
AssertJUnit.assertEquals(driver.getTitle(), "View Account");
}
@AfterMethod
public void tearDown() throws Exception {
File screenshot = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(screenshot, new File(fileName));
} catch (IOException e) {
e.printStackTrace();
}
driver.quit();
}
}
Now as we see there are 2 pages:
1. Login page, where i have to enter username and password, and homepage, where i would be taken, once the authentication succeeds.
Now i want to implement this as PageObjects using Pagefactory: so i have :
package com.example.pageobjects;
import static com.example.setup.SeleniumDriver.getDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
public abstract class MPage<T> {
private static final String BASE_URL = "https://a.b.c.d/";
private static final int LOAD_TIMEOUT = 30;
private static final int REFRESH_RATE = 2;
public T openPage(Class<T> clazz) {
T page = PageFactory.initElements(getDriver(), clazz);
getDriver().get(BASE_URL + getPageUrl());
ExpectedCondition pageLoadCondition = ((MPage) page).getPageLoadCondition();
waitForPageToLoad(pageLoadCondition);
return page;
}
private void waitForPageToLoad(ExpectedCondition pageLoadCondition) {
Wait wait = new FluentWait(getDriver())
.withTimeout(LOAD_TIMEOUT, TimeUnit.SECONDS)
.pollingEvery(REFRESH_RATE, TimeUnit.SECONDS);
wait.until(pageLoadCondition);
}
/**
* Provides condition when page can be considered as fully loaded.
*
* @return
*/
protected abstract ExpectedCondition getPageLoadCondition();
/**
* Provides page relative URL/
*
* @return
*/
public abstract String getPageUrl();
}
And for login Page not sure how i would implement that, as well as the Test, which would call these pages.