Page Object Model in Selenium

POM -Page Object Model in Selenium

i) What is POM – Page Object Model?
ii) Advantages of Page Object Model
iii) Implement Page Object model
a) Create Page Class
b) Create Test Cases

i) What is POM – Page Object Model?

> Page Object Model is an Object/Element design pattern to create Repositories for Web elements  (Example: Link, Button, Edit Box etc…)

> We create Object Repositories for centralized maintenance of Objects/Elements

> 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

> No Object Repository facility in Selenium Webdriver, It is a Programming Interface, In UFT/QTP we have Object Repository facility (Local and Shared Object Repositories)

> Under this POM model, for each web page in the application, there should be corresponding Page Class…

> This Page class we find the web elements of the web page and also contain the methods which perform Operations on those web elements

> If no Object Repository / Page Object Model then it is difficult to maintain objects/elements in Test Cases…

ii) Advantages of Page Object Model

> Reusability of Elements, reduces code size

> It is efficient and scalable

> Centralized Maintenance of Elements/Objects

iii) Implement Page Object model

> In this approach all page/s elements of the application and user actions on those web elements are maintained as methods inside a class file, by importing the page class we can create test cases…

a) Create Page Class:

package abcd;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class LoginPage {
WebDriver driver;
//Create elements using element locators
By User = By.name(“username”);
By Password = By.name(“password”);
By LoginButton = By.id(“tdb1”);
//this keyword is used to invoke on instantiate current class constructor
// A Constructor in Java is a block of code similar to a method
// that’s called an instance of Object is created
public LoginPage(WebDriver driver){
this.driver=driver;
}
//Create user actions
public void typeUsername(String Uname){
abcd.findElement(User).sendKeys(Uname);
}
public void captureUsername(){
driver.findElement(User).getAttribute(“value”);
}

public void clearUsername(){
driver.findElement(User).clear();
}
public void typePassword(String Pwd){
driver.findElement(Password).sendKeys(Pwd);
}
public void clickLoginButton(){
driver.findElement(LoginButton).click();
}
}
———————————————–
b) Create Test Case Class:

package abcd;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class TestCases {

public static void main(String[] args) {
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
WebDriver xyz = new ChromeDriver();
LoginPage login= new LoginPage(xyz);

//Create Admin Login Test Case
xyz.get(“http://www.gcrit.com/build3/admin/”);
login.typeUsername(“admin”);
login.typePassword(“admin@123”);
login.clickLoginButton();

String url=xyz.getCurrentUrl();

if (url.contains(“http://www.gcrit.com/build3/admin/index.php”)){
System.out.println(“Admin Login is Successful -Passed”);
}
else{
System.out.println(“Admin Login is Unsuccessful -Failed”);
}
xyz.close();
}
}

 

Page Object Model in Selenium

Requirement: Create a Page Class/Object Repository using some elements
(username password, login button, errormessage, online catalog line etc….)

Create Test Cases,
i) Admin Login
ii) Verify Error Message
iii) Verify Redirection from Admin to Login
——————————————————–
Page Class:
package abcd;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class LoginPage {
WebDriver abcd;

By Uname = By.name(“username”);
By Password = By.name(“password”);
By LoginButton=By.id(“tdb1”);
By ErrorMessage = By.className(“messageStackError”);
By link = By.linkText(“Online Catalog”);

public LoginPage(WebDriver abcd2){
this.abcd=abcd2;
}

public void typeUsername (String User){
abcd.findElement(Uname).sendKeys(User);
}
public void typePassword(String Pwd){
abcd.findElement(Password).sendKeys(Pwd);
}
public void captureError(){
String message=abcd.findElement(ErrorMessage).getText();
}
public void clikButton(){
abcd.findElement(LoginButton).click();
}
public void clickLink(){
abcd.findElement(link).click();
}
}
———————————————————-
Test Case 1:

package abcd;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class TestCase1 {

public static void main(String[] args) throws InterruptedException {
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
WebDriver xyz= new ChromeDriver();
LoginPage test=new LoginPage(xyz);

//Test Case: verify Redirection Functionality from Admin to User Interface
xyz.get(“http://www.gcrit.com/build3/admin/”);
test.typeUsername(“admin”);
test.typePassword(“admin@123”);
test.clikButton();
Thread.sleep(4000);
String url=xyz.getCurrentUrl();

if (url.contains(“http://www.gcrit.com/build3/admin/index.php”)){
test.clickLink();
System.out.println(“1st Verification Point : Admin Login is Successful -Passed”);
}
else{
System.out.println(“1st Verification Point : Admin Login is unsuccessful -Failed”);
}

String url2=xyz.getCurrentUrl();

if (url2.equals(“http://www.gcrit.com/build3/”)){
System.out.println(“2nd Verification Point : Application was redirected from Admin to User Interface -Passed”);
}
else {
System.out.println(“2nd Verification Point : Application was redirected from Admin to User Interface -Passed”);
}
xyz.close();
}
}
———————————————————-
Test Case 2:

package abcd;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Testcase2 {

public static void main(String[] args) {
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
WebDriver xyz2= new ChromeDriver();
LoginPage test2=new LoginPage(xyz2);

//Create Admin Login Test Case
xyz2.get(“http://www.gcrit.com/build3/admin/”);
test2.typeUsername(“admin”);
test2.typePassword(“admin@123”);
test2.clikButton();

String url=xyz2.getCurrentUrl();

if (url.contains(“http://www.gcrit.com/build3/admin/index.php”)){
System.out.println(“Admin Login is Successful -Passed”);
}
else{
System.out.println(“Admin Login is unsuccessful -Failed”);
}
xyz2.close();

}
}
—————————————————————————–

(1354)

You may also like...