0% found this document useful (0 votes)
12 views5 pages

Selenium 29

The document discusses the page object model (POM) in Selenium, which is an object-oriented design pattern for web application testing. It involves creating page class files that initialize web elements and expose methods to interact with them. This improves test maintenance by centralizing element locators and actions. Examples are provided to demonstrate creating page classes and test cases that utilize the page objects.

Uploaded by

prashanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Selenium 29

The document discusses the page object model (POM) in Selenium, which is an object-oriented design pattern for web application testing. It involves creating page class files that initialize web elements and expose methods to interact with them. This improves test maintenance by centralizing element locators and actions. Examples are provided to demonstrate creating page classes and test cases that utilize the page objects.

Uploaded by

prashanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Selenium Class 29 � POM � Page Object Model in Selenium

---------------------------------------------------------------
> Page Object Model is an Object / Element design pattern to create Repositories
for web elements
(Ex: Link, Edit box, Button etc�)

> We create Object Repositories for centralized maintenance of elements.

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

> We can create POM in two 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, for each web page there should be corresponding Page Class�

This Page class will find the web elements of the web page and also contains
customized methods which
perform operations on those web elements.

If no Object Repository / Page Object Model then it is difficult to maintain


elements in Test Cases�

Advantages of Page Object Model

Reusability of Elements, reduces code size

Centralized maintenance of Elements

Implement Page Object Model

In this approach all pages/elements of the application and user actions on those
web elements are
maintained as methods inside class file/s, by importing the page class/s we can
create test cases
----------------------------------------------------------------------------
Create Page Class/es by providing Elements information, they act as Object
Repositories
Create Test Cases (Classes) by importing the page classes

1) Create a Page Class for Login Page in gcrShop Admin Interface


-----------------------------------------------------------------------
public class LoginPage {
WebDriver driver;

//Create Elements using Element Locators


By User = By.name("username");
By Pwd = By.name("password");
By LoginButton = By.id("tdb1");

//this keyword is invoke an instance current class constructor


//A Constructor in Java is a block of code similar to a method
//that is called an instance of Object is created
public LoginPage(WebDriver driver) {
this.driver = driver;
}

//Create User Actions / Customized Commands


public void typeUserName (String name) {
driver.findElement(User).sendKeys(name);
}

public void clearUserName() {


driver.findElement(User).clear();
}

public void typePassword (String Password) {


driver.findElement(Pwd).sendKeys(Password);
}

public void clickLoginButton() {


driver.findElement(LoginButton).click();
}

}
---------------------------------------------------
Create Test Case Class:

public class TestCase {

public static void main(String[] args) {


System.setProperty("webdriver.chrome.driver", "F:/chromedriver.exe");
WebDriver abc = new ChromeDriver();
abc.manage().window().maximize();

LoginPage login = new LoginPage(abc);

//Create Login Test Case


abc.get("http://www.gcrit.com/build3/admin/");
login.typeUserName("admin");
login.typePassword("admin@123");
login.clickLoginButton();

String url = abc.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");
}
}
}
-------------------------------------------------------------------------
2) Create a Page Class / Object Repository using elements in Login Page (Username,
Password, Login, Error Message,
Online Catalog link etc,)
-----------------------------------------------------------------------
Create Test Cases
1) Admin Login
2) Verify "Error" Message"
3) Verifty "Redirect" functionality after Login
.
------------------------------------------------------------------
Page Class:

public class LoginPage {


WebDriver abcd;

//Create Web Elements


By User = By.name("username");
By Pwd = By.name("password");
By LoginButton = By.id("tdb1");
By ErrorMessage = By.className("messageStackError");
By CatalogLink = By.linkText("Online Catalog");

public LoginPage (WebDriver driver1) {


this.abcd=driver1;
}
//Create Customized Commands
public void typeUserName(String name) {
abcd.findElement(User).sendKeys(name);
}

public void typePassword(String password) {


abcd.findElement(Pwd).sendKeys(password);
}

public void clickLogonButton() {


abcd.findElement(LoginButton).click();
}

public String cpatureErrorMessage() {


String message = abcd.findElement(ErrorMessage).getText();
return message;
}

public void clickLink() {


abcd.findElement(CatalogLink).click();
}

}
-----------------------------------------------------
Test Case 1 (Verify Admin Login)

System.setProperty("webdriver.chrome.driver", "F:/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

LoginPage login = new LoginPage(driver);

driver.get("http://www.gcrit.com/build3/admin/");
login.typeUserName("admin1");
login.typePassword("admin@123");
login.clickLoginButton();

String url = driver.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");
}
driver.close();

}
}
------------------------------------------------------------------------
Test Case 2 (Verify Error Message in Admin Login Functionalily)

public class ErrorMessage {

public static void main(String[] args) {


System.setProperty("webdriver.chrome.driver", "F:/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

LoginPage ErrorMessage = new LoginPage(driver);

driver.get("http://www.gcrit.com/build3/admin/");
ErrorMessage.typeUserName("admin1");
ErrorMessage.typePassword("admin@321");
ErrorMessage.clickLoginButton();

String error=ErrorMessage.cpatureErrorMessage();

if (error.contains(" Error: Invalid administrator login attempt.")) {


System.out.println("Login Unsuccessful, but showing correct error message -
Passed");
}
else {
System.out.println("Login Unsuccessful, but Not showing correct error message -
Failed");
}
driver.close();
}
}
--------------------------------------------------------------
Test Case 3 (Verify "Redirect Functionalily)

public class Redirect {

public static void main(String[] args) {


System.setProperty("webdriver.chrome.driver", "F:/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

LoginPage redirect=new LoginPage(driver);

driver.get("http://www.gcrit.com/build3/admin/");
redirect.typeUserName("admin");
redirect.typePassword("admin@123");
redirect.clickLoginButton();

String url = driver.getCurrentUrl();

if (url.contains("http://www.gcrit.com/build3/admin/index.php")) {
redirect.clickLink();
}
String url2=driver.getCurrentUrl();

if (url2.equals("http://www.gcrit.com/build3/")) {
System.out.println("Page is Redirecting from Admin to User Interface -Passed");
}
else {
System.out.println("Page is Not Redirecting from Admin to User Interface -Passed");
}

driver.close();

}
}
------------------------------------------------------

You might also like