Selenium Page Object Model

Selenium Page Object Model, Creating Object Repositories using Page Object Model, Creating Test Case classes, and reuse & maintain elements.

Selenium Testing

Page Object Model in Selenium

Page Object Model is an Object / Element design pattern to create Object Repositories for web elements

We create Object Repositories for reusing objects and centralized maintenance of elements

Under this POM 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, but in UFT/QTP we have Object Repository facility.

Under this POM, for each web page, there should be corresponding page class

This Page class finds the web elements of the web page and also contains customized methods that perform operations on web elements.

Implementing Page Object Model

In this approach, all pages of the applications and user actions on web elements are maintained as methods, by importing the page class we can create test cases.

1. Create Page class

WebDriver pageClassdriver;

//Create Elements using Element Locators
By Username = By.name(“username”);
By Password = By.name(“password”);
By LoginButton = By.id(“tdb1”);

//Create Constructor
//A Constructor in Java is a block of code similar to a method
//this keyword is invoked an instance of the current class constructor that is called an instance is created
public LoginPage(WebDriver orgdriver) {
this.pageClassdriver= orgdriver;
}

// Create User Actions/Customized commands
public void typeUsername (String Uname) {
pageClassdriver.findElement(Username).sendKeys(Uname);
}

public String CaptureUsername() {
String val = pageClassdriver.findElement(Username).getAttribute(“value”);
return val;
}

public void typePassword(String Pwd) {
pageClassdriver.findElement(Password).sendKeys(Pwd);
}

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

Create Test Case Class:

System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\user\\Downloads\\chromedriver.exe”);
WebDriver testCaseclassdriver = new ChromeDriver();
testCaseclassdriver.manage().window().maximize();
LoginPage login = new LoginPage(testCaseclassdriver);

//Create Admin Login Test Case
testCaseclassdriver.get(“https://gcreddy.com/project/admin/login.php”);
login.typeUsername(“gcreddy”);
login.typePassword(“[email protected]”);
login.clickLoginButton();

String url = testCaseclassdriver.getCurrentUrl();

if (url.contains(“https://gcreddy.com/project/admin/index.php”)) {
System.out.println(“Pass”);
}
else {
System.out.println(“Fail”);
}
testCaseclassdriver.close();
}
}


2. Create a Page class/Object Repository (Using some elements of Login Page)
(Username, Password edit box, Login button, Errormessage, Online catalog link)

Create Test Cases
i. Verify Admin Login using valid input
ii. Verify Error Message in Login functionality using invalid input
iii. Verify Page redirecting functionality (Admin to User interface) after successful login

2. Create Page Class:

public class LoginPage {
WebDriver PageClassdriver;

public LoginPage(WebDriver orgdriver) {
this.PageClassdriver = orgdriver;
}

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

public void typeUserName(String Uname) {
PageClassdriver.findElement(Username).sendKeys(Uname);
}

public void typePassword(String Pass) {
PageClassdriver.findElement(Password).sendKeys(Pass);
}

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

public String captureErrorMessage() {
String message = PageClassdriver.findElement(ErrorMessage).getText();
return message;
}

public void clickLink() {
PageClassdriver.findElement(OnlineCatalogLink).click();
}

}

Test Case 1 Class:

public class TestCase1 {

public static void main(String[] args) {
System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\user\\Downloads\\chromedriver.exe”);
WebDriver testCase1driver = new ChromeDriver();
testCase1driver.manage().window().maximize();

LoginPage login = new LoginPage(testCase1driver);

testCase1driver.get(“https://gcreddy.com/project/admin/login.php”);
login.typeUserName(“gcreddy”);
login.typePassword(“[email protected]”);
login.clickLoginButton();

if (testCase1driver.getCurrentUrl().contains(“https://gcreddy.com/project/admin/index.php”)) {
System.out.println(“Pass”);
}
else {
System.out.println(“Fail”);
}
testCase1driver.close();
}
}

Test Case 2 Class:

public class TestCase2 {

public static void main(String[] args) {
System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\user\\Downloads\\chromedriver.exe”);
WebDriver testCase2driver = new ChromeDriver();
testCase2driver.manage().window().maximize();

LoginPage errorMes = new LoginPage(testCase2driver);

testCase2driver.get(“https://gcreddy.com/project/admin/login.php”);
errorMes.typeUserName(“abcdef”);
errorMes.typePassword(“[email protected]”);
errorMes.clickLoginButton();

String errorM = errorMes.captureErrorMessage();

if (errorM.contains(“Error: Invalid administrator login attempt.”)) {
System.out.println(“Pass”);
}
else {
System.out.println(“Fail”);
}
testCase2driver.close();
}
}

Test Case 3 Class:

public class TestCase3 {

public static void main(String[] args) {
System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\user\\Downloads\\chromedriver.exe”);
WebDriver testCase3driver = new ChromeDriver();
testCase3driver.manage().window().maximize();

LoginPage redirect = new LoginPage(testCase3driver);
testCase3driver.get(“https://gcreddy.com/project/admin/login.php”);
redirect.typeUserName(“gcreddy”);
redirect.typePassword(“[email protected]”);
redirect.clickLoginButton();

String url = testCase3driver.getCurrentUrl();

if (url.contains(“https://gcreddy.com/project/admin/index.php”)) {
redirect.clickLink();
}

String url2= testCase3driver.getCurrentUrl();

if (url2.equals(“https://gcreddy.com/project/”)){
System.out.println(“Pass”);
}
else {
System.out.println(“Fail”);
}
testCase3driver.close();
}
}

 

G C Reddy

G C Reddy, Working as a Software Testing Trainer in Hyderabad, India.

You may also like...