Page Object Model in Selenium

Page Object Model in Selenium

1) What is Page Object Model?

> Page Object model is an object/Element design pattern to create Object Repository for web elements/objects (Ex: Links, Buttons, Edit boxes etc…).

> Under this model, container classes for web elements are created that behave as object repositories.

> We can create POM in 2 ways, 1) POM without Page Factory, 2) POM with Page Factory In this session I am going to explain POM without Page Factory.

> No Object Repository facility in Selenium WebDriver, It is a programming
Interface, If it is UFT/QTP We have Object Repository facility (local and shared
Object repositories)

> Under this model, for each web page in the application, there should be
corresponding page class.

> This Page class will find the Web Elements of that web page and also contains the methods which perform operations on those Web Elements.

> If no Object Repository/Page Object Model then it is difficult to maintaing
objects/Elements in Test cases…

2) Advantages of Page Object Model

> Centralized maintenance of elements/objects

> Reusability, reduces duplication of code

> It is Efficient and Scalable

> It makes the framework user friendly.

3) Disadvantages of Page Object Model

> Initial effort investment in development of Automation Framework is high.

> It is not a generic model, automation framework developed using POM approach is specific to the application.

4) Implement Page Object Model

> In this approach all pages web elements of the application and the user actions on these Web Elements are maintained as methods inside a class file. By importing the page classes then we can create Test cases easily.

Page Object Model Pattern Architecture

Create Page Class…

public class LoginPage {
WebDriver driver;

By Username=By.name(“username”);
By Password=By.name(“password”);
By LoginButton=By.id(“tdb1”);

public LoginPage(WebDriver driver){
this.driver=driver;
}
public void typeUsername(String uname){
driver.findElement(Username).sendKeys(uname);
}

public void typePassword(String pass){
driver.findElement(Password).sendKeys(pass);
}

public void clickLoginButton(){
driver.findElement(LoginButton).click();
}

public void clearUsername(){
driver.findElement(Username).clear();
}

public String captureUsername(){
return driver.findElement(Username).getAttribute(“value”);
}

}

Test Cases Class

public class TestCases {

public static void main(String[] args) throws InterruptedException {

System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
WebDriver driver=new ChromeDriver();
LoginPage login=new LoginPage(driver);

driver.get(“http://www.gcrit.com/build3/admin/login.php”);
login.typeUsername(“admin”);
Thread.sleep(2000);
login.typePassword(“admin@123”);
Thread.sleep(2000);
login.clickLoginButton();

String URL=driver.getCurrentUrl();

if (URL.contains(“http://www.gcrit.com/build3/admin/index.php”)){
System.out.println(“Admin Login Sucessful-Passed”);
}
else{
System.out.println(“Admin Login Sucessful-Failed”);
}
driver.close();
}
}

(79)

You may also like...

%d bloggers like this: