Frameworks - Page Object Model
Page Object Model is an object design pattern in Selenium, where web pages are represented as classes, and the various elements on the page are defined as variables on the class. All possible user interactions can then be implemented as methods in the class. Since well-named methods in classes are easy to read, this works as an elegant way to implement test routines that are both readable and easier to maintain or update in the future.
There are two ways, this is the first one and the other involves the use of PageFactory library.
First way:
Generally, two packages are created - objectrepository and testcases.
object repository: For each web page, we create a different class and in each class, we write methods to return the web elements.
package objectrepository;
public class Loginpage {
WebDriver driver;
public Loginpage(WebDriver driver) {
// TODO Auto-generated constructor stub
this.driver=driver;
}
By username=By.xpath(".//*[@id='login1']");
By Password=By.name("passwd");
By go=By.name("proceed");
By home=By.linkText("Home");
public WebElement Emaild()
{ return driver.findElement(username); }
public WebElement Password()
{return driver.findElement(Password);}test cases : Here we create a class and write the automation script in test cases, and we directly call the objects of the webpages to access element locators
public class Loginapplication {
// Login page class implemented in normal page object pattern style
// Home page class implemented in Page object factory methdos
@Test
public void Login()
{
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://mail.rediff.com/cgi-bin/login.cgi");
RediffLoginpage rd=new RediffLoginpage(driver);
rd.Emaild().sendKeys("hello");
rd.Password().sendKeys("hello");
//rd.submit().click();
rd.Home().click();
RediffHomepage rh=new RediffHomepage(driver);
rh.Search().sendKeys("rediff");
rh.Submit().click();
}The other way
Here we use the org.openqa.selenium.support.PageFactory library, wherein we use FindBy tag to provide locator value for a web element. This is the only difference from the previous method, rest all is the same.
package objectrepository;
import org.openqa.selenium.support.PageFactory;
public class LoginpagePF {
WebDriver driver;
public LoginpagePF(WebDriver driver) {
this.driver=driver;
PageFactory.initElements(driver, this);
}
@FindBy(xpath=".//*[@id='login1']")
WebElement username;
@FindBy(name="passwd")
WebElement Password;
public WebElement Emaild()
{return username;}
public WebElement Password()
{return Password;}
public WebElement Home()
{return home;}
} Advantages of POM
- Page Object Pattern says operations and flows in the UI should be separated from verification. This concept makes our code cleaner and easy to understand.
- The Second benefit is the object repository is independent of test cases, so we can use the same object repository for a different purpose with different tools. For example, we can integrate POM with TestNG/JUnit for functional Testing and at the same time with JBehave/Cucumber for acceptance testing.
- Code becomes less and optimized because of the reusable page methods in the POM classes.
- Methods get more realistic names which can be easily mapped with the operation happening in UI. i.e. if after clicking on the button we land on the home page, the method name will be like 'gotoHomePage()'.