Test Design Patterns
Created by Kavan Sheth
Best viewed in Chrome, Firefox, Opera or A browser supporting HTML5.
Use arrow keys ( up ↑, down ↓, left ←, right ↑) to navigate. Uses Reveal.js by Hakim El Hattab
Press ESC to enter the slide overview.
Create a class for UI mapping
import java.io.FileInputStream;
import java.util.Properties;
public class AdminUiMap {
public String username;
public AdminUiMap(String fileName) {
Properties props = new Properties();
props.load(new FileInputStream(fileName));
this.username = props.getProperty("admin.username");
}
}
And then create admin object of AdminUiMap Class
AdminUiMap admin = new AdminUiMap("adminLocators.properties");
Now you can use strings from your property file as
selenium.type(admin.username, "xxxxxxxx");
public class AllUIMaps {
public static class LoginPage
{
private static String username ="user_Name";
public String getUsername() {
return username;
}
public void setUsername(String username) {
LoginPage.username = username;
}
}
public static class userDetails{
private static String userphone="user_phone";
private static String useraddr="user_addr1";
public String getUserphone() {
return userphone;
}
public void setUserphone(String userphone) {
userDetails.userphone = userphone;
}
public String getUseraddr() {
return useraddr;
}
public void setUseraddr(String useraddr) {
userDetails.useraddr = useraddr;
}
}
}
Here we will directly start with one example and I think it will help you to understand how page object model developed and how can you use it for your project.
We will use http://newtours.demoaut.com/, it is dummy site for QTP.
Scenario:find flights for a target
Steps:
Try It!!
One might write code like following:
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class WithoutPageObject{
@Test
public void testFindFlightForMe()
{
WebDriver driver;
File file = new File("C:\\Users\\xxxxx\\Desktop\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
driver = new ChromeDriver();
String baseUrl = "http://newtours.demoaut.com/";
driver.manage().timeouts().implicitlyWait(180, TimeUnit.SECONDS);
driver.get(baseUrl);
driver.findElement(By.name("userName")).sendKeys("test");
driver.findElement(By.name("password")).sendKeys("test");
driver.findElement(By.name("login")).click();
driver.findElement(By.xpath("//input[@name='tripType'][@value='oneway']")).sendKeys(Keys.SPACE);
new Select(driver.findElement(By.name("fromPort"))).selectByValue("New York");
new Select(driver.findElement(By.name("toPort"))).selectByValue("London");
new Select(driver.findElement(By.name("fromDay"))).selectByValue("25");
driver.findElement(By.xpath("//input[@name='servClass'][@value='Business']")).sendKeys(Keys.SPACE);
new Select(driver.findElement(By.name("airline"))).selectByVisibleText("Unified Airlines");
driver.findElement(By.name("findFlights")).click();
//here you can have some verification of retrieved result
driver.close();
}
}
From Coding point of view nothing wrong with above code, but there are few points one must consider
Here we are dealing with three pages
Here in LoginPage Source. you can see that:
class LoginPage{
protected WebDriver driver;
public LoginPage(WebDriver d)
{
driver = d;
}
public LoginPage setUserName(String user)
{
driver.findElement(By.name("userName")).sendKeys(user);
return this;
}
public LoginPage setPassword(String pass)
{
driver.findElement(By.name("password")).sendKeys(pass);
return this;
}
public FlightFinderPage clickLoginButton()
{
driver.findElement(By.name("login")).click();
return new FlightFinderPage(driver);
}
}
class FlightFinderPage{
protected WebDriver driver;
public FlightFinderPage(WebDriver d) {
driver = d;
}
public FlightFinderPage selectOneWayTrip()
{
driver.findElement(By.xpath("//input[@name='tripType'][@value='oneway']")).sendKeys(Keys.SPACE);
return this;
}
public FlightFinderPage setFromPort(String fromPort)
{
new Select(driver.findElement(By.name("fromPort"))).selectByValue(fromPort);
return this;
}
public FlightFinderPage setToPort(String toPort)
{
new Select(driver.findElement(By.name("toPort"))).selectByValue(toPort);
return this;
}
public FlightFinderPage selectFromDay(String fromDay)
{
new Select(driver.findElement(By.name("fromDay"))).selectByValue(fromDay);
return this;
}
public FlightFinderPage setBusinessClass()
{
driver.findElement(By.xpath("//input[@name='servClass'][@value='Business']")).sendKeys(Keys.SPACE);
return this;
}
public FlightFinderPage selectAirline(String air)
{
new Select(driver.findElement(By.name("airline"))).selectByVisibleText(air);
return this;
}
public FlightFinderResultPage clickFindFlights()
{
driver.findElement(By.name("findFlights")).click();
return new FlightFinderResultPage(driver);
}
}
class FlightFinderResultPage{
protected WebDriver driver;
public FlightFinderResultPage(WebDriver d) {
driver = d;
}
//Services to be defined
}
After creating page object you can writing your test as following, which seems more elegant.
public class WithPageObject{
String baseUrl;
@Before
public void beforeTest()
{
File file = new File("C:\\Users\\kavan.sheth\\Desktop\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
baseUrl = "http://newtours.demoaut.com/";
}
@Test
public void test1()
{
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(180, TimeUnit.SECONDS);
driver.get(baseUrl);
LoginPage loginPage = new LoginPage(driver);
loginPage.setUserName("test")
.setPassword("test");
FlightFinderPage flightFinderPage=loginPage.clickLoginButton();
flightFinderPage.selectOneWayTrip()
.setFromPort("New York")
.setToPort("London")
.selectFromDay("25")
.setBusinessClass()
.selectAirline("Unified Airlines");
FlightFinderResultPage flightFinderResultPage = flightFinderPage.clickFindFlights();
//here you can have some verification of retrieved result
driver.close();
}
}
private WebElement userName;
private WebElement password;
private WebElement login;
LoginPage loginPage=PageFactory.initElements(driver, LoginPage.class);
public LoginPage setUserName(String user)
{
userName.sendKeys(user);
return this;
}
java.lang.RuntimeException: java.lang.IllegalAccessException:
Class org.openqa.selenium.support.PageFactory can not access a member
of class *.LoginPage with modifiers "public"
Though here I have shown, all classes at same place, don't forget to separate page object classes in different files.
package net.mylearning.pagefactory;
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class WithPageFactory{
String baseUrl;
WebDriver driver;
@Before
public void beforeTest()
{
File file = new File("C:\\Users\\xxxxxx\\Desktop\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
baseUrl = "http://newtours.demoaut.com/";
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
}
@Test
public void test1()
{
driver.get(baseUrl);
LoginPage loginPage= PageFactory.initElements(driver, LoginPage.class);
//you can also place this in constructor like PageFactory.initElements(driver, this), but any ways we will need to create pageObject so kept it here.
FlightFinderPage flightFinderPage=loginPage.setUserName("test")
.setPassword("test")
.clickLoginButton();
FlightFinderResultPage flightFinderResultPage=flightFinderPage.selectOneWayTrip()
.setFromPort("New York")
.setToPort("London")
.selectFromDay("25")
.setBusinessClass()
.selectAirline("Unified Airlines")
.clickFindFlights();
//here you can have some verification of retrieved result
}
@After
public void afterTest()
{
driver.close();
}
}
public class LoginPage{
private WebElement userName;
private WebElement password;
private WebElement login;
public WebDriver driver;
public LoginPage(WebDriver driver )
{
this.driver= driver;
}
public LoginPage setUserName(String user)
{
userName.sendKeys(user);
return this;
}
public LoginPage setPassword(String pass)
{
password.sendKeys(pass);
return this;
}
public FlightFinderPage clickLoginButton()
{
login.click();
return PageFactory.initElements(driver, FlightFinderPage.class);
}
}
public class FlightFinderPage{
public WebDriver driver;
private WebElement fromPort;
private WebElement toPort;
private WebElement fromDay;
private WebElement airline;
private WebElement findFlights;
public FlightFinderPage(WebDriver d) {
driver = d;
}
public FlightFinderPage selectOneWayTrip()
{
driver.findElement(By.xpath("//input[@name='tripType'][@value='oneway']")).sendKeys(Keys.SPACE);
return this;
}
public FlightFinderPage setFromPort(String from)
{
new Select(fromPort).selectByValue(from);
return this;
}
public FlightFinderPage setToPort(String to)
{
new Select(toPort).selectByValue(to);
return this;
}
public FlightFinderPage selectFromDay(String fDay)
{
new Select(fromDay).selectByValue(fDay);
return this;
}
public FlightFinderPage setBusinessClass()
{
driver.findElement(By.xpath("//input[@name='servClass'][@value='Business']")).sendKeys(Keys.SPACE);
return this;
}
public FlightFinderPage selectAirline(String air)
{
new Select(airline).selectByVisibleText(air);
return this;
}
public FlightFinderResultPage clickFindFlights()
{
findFlights.click();
return PageFactory.initElements(driver, FlightFinderResultPage.class);
}
}
public class FlightFinderResultPage{
//Services to be defined
}
@FindBy(how = How.NAME, using = "fromPort")
private WebElement fPort;
@FindBy(how = How.XPATH, using = "//input[@name='tripType'][@value='oneway']")
private WebElement oneWayTrip;
@FindAll({@FindBy(xpath = “yourfirstxpath”)
,@FindBy(xpath = “yoursecondxpath”),@FindBy(xpath = “yourThirddxpath”)})
public List resultElements;
@FindBy(how = How.NAME, using = "fromPort")
@CacheLookup
private WebElement fPort;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class LoginPage{
@FindBy(name="userName")
private WebElement userName;
@FindBy(name="password")
private WebElement password;
@FindBy(name="login")
private WebElement login;
public WebDriver driver;
public LoginPage(WebDriver driver )
{
this.driver= driver;
}
public LoginPage setUserName(String user)
{
userName.sendKeys(user);
return this;
}
public LoginPage setPassword(String pass)
{
password.sendKeys(pass);
return this;
}
public FlightFinderPage clickLoginButton()
{
login.click();
return PageFactory.initElements(driver, FlightFinderPage.class);
}
}
public class FlightFinderPage{
public WebDriver driver;
@FindBy(how = How.NAME, using = "fromPort")
private WebElement fPort;
@FindBy(how = How.XPATH, using = "//input[@name='tripType'][@value='oneway']")
private WebElement oneWayTrip;
@FindBy(name="toPort")
private WebElement toPort;
@FindBy(name="fromDay")
private WebElement fromDay;
@FindBy(name="airline")
private WebElement airline;
@FindBy(name="findFlights")
private WebElement findFlights;
@FindBy(how = How.XPATH, using = "//input[@name='servClass'][@value='Business']")
private WebElement businessClass;
public FlightFinderPage(WebDriver d) {
driver = d;
}
public FlightFinderPage selectOneWayTrip()
{
oneWayTrip.sendKeys(Keys.SPACE);
return this;
}
public FlightFinderPage setFromPort(String from)
{
new Select(fPort).selectByValue(from);
return this;
}
public FlightFinderPage setToPort(String to)
{
new Select(toPort).selectByValue(to);
return this;
}
public FlightFinderPage selectFromDay(String fDay)
{
new Select(fromDay).selectByValue(fDay);
return this;
}
public FlightFinderPage setBusinessClass()
{
businessClass.sendKeys(Keys.SPACE);
return this;
}
public FlightFinderPage selectAirline(String air)
{
new Select(airline).selectByVisibleText(air);
return this;
}
public FlightFinderResultPage clickFindFlights()
{
findFlights.click();
return PageFactory.initElements(driver, FlightFinderResultPage.class);
}
}
public class FlightFinderResultPage{
//Services to be defined
}