Posted by: Kiran Thurimerla
(SQA Module Lead working at Ebix Software India PVT Ltd).
Contact Author: kiran.thurimerla@gmail.com
(SQA Module Lead working at Ebix Software India PVT Ltd).
Contact Author: kiran.thurimerla@gmail.com
Creating Selenium test cases can result in not maintainable
project. One of the reasons is that too many duplicated code is used.
Duplicated code could be caused by duplicated functionality and this will
result in duplicated usage of locators. The disadvantage of duplicated code is
that the project is less maintainable. If some locator will change, you have to
walk through the whole test code to adjust locators where necessary. By using
the page object model we can make non-brittle test code and reduce or eliminate
duplicate test code. Beside of that it improves the readability and allows us
to create interactive documentation. Last but not least, we can create tests
with less keystroke. An implementation of the page object model can be achieved
by separating the abstraction of the test object and the test scripts.
Example (General):
import
org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.ie.InternetExplorerDriver;
public class SampleOne {
public static void main(String a[]) throws InterruptedException
{
System.setProperty("webdriver.ie.driver",".\\IEDriverServer.exe");
WebDriver
driver = new
InternetExplorerDriver();
driver.get("http://obesityfoundationindia.com/bmi.htm");
WebElement
wt = driver.findElement(By.name("wg"));
wt.sendKeys("68");
WebElement
kglbs = driver.findElement(By.name("opt1"));
kglbs.sendKeys("kilograms");
WebElement
hft = driver.findElement(By.name("opt2"));
hft.sendKeys("5'");
WebElement
hinc = driver.findElement(By.name("opt3"));
hinc.sendKeys("9\"");
WebElement
cal = driver.findElement(By.name("cc"));
cal.click();
Thread.sleep(3000);
driver.quit();
}
}
In the above code,
the Web elements declarations are not reusable. If we want to build any other
scenarios with the same elements we need to once again write all the objects
declarations.
Using Page Object Model (POM), we can reuse the web elements
declarations. In POM, we will separate
Web elements declarations from main selenium scripting file so that, the same
web elements declarations will be reusable in other scripts.
It’s just like an Objects Repository. For each web page we will write a separate
object repository file which contains all the Web elements objects.
In the above given example, all the web elements are given
in a single file. But, as per POM, we
will a separate objects repository file.
Example (POI): ( Screenshot1)
Main
Scripting class: (Screenshot2)
Example (POI): (Code)
package pageObjects;
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.WebElement;
public class BMICalcHome {
//wt, kglbs,
hft, hinc, cal
public static WebElement wg(WebDriver driver) {
WebElement
wt = driver.findElement(By.name("wg"));
return wt;
}
public static WebElement
kglbs(WebDriver driver) {
WebElement
kglbs = driver.findElement(By.name("opt1"));
return kglbs;
}
public static WebElement
hft(WebDriver driver) {
WebElement
hft = driver.findElement(By.name("opt2"));
return hft;
}
public static WebElement
hinc(WebDriver driver) {
WebElement
hinc = driver.findElement(By.name("opt3"));
return hinc;
}
public static WebElement
cal(WebDriver driver) {
WebElement
cal = driver.findElement(By.name("cc"));
return cal;
}
}
Main
Scripting class: (Code)
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.ie.InternetExplorerDriver;
import
pageObjects.BMICalcHome;
import pageObjects.BMIMemberReg;
public class POM_Samp1 {
public static void main(String[] args) throws InterruptedException
{
// TODO Auto-generated
method stub
System.setProperty("webdriver.ie.driver",".\\IEDriverServer.exe");
WebDriver
driver = new
InternetExplorerDriver();
driver.get("http://obesityfoundationindia.com/bmi.htm");
//Home Page
BMICalcHome.wg(driver).sendKeys("75");
BMICalcHome.kglbs(driver).sendKeys("kilograms");
BMICalcHome.hft(driver).sendKeys("5'");
BMICalcHome.hinc(driver).sendKeys("9\"");
BMICalcHome.cal(driver).click();
Thread.sleep(3000);
driver.quit();
}
}