About - Selenium

Definition

Development and Versions

Pre-requisites

Languages supported

Manual vs Automation

Choosing Selenium

Selenium - Components

Selenium IDE

Selenium RC

Selenium WebDriver

Selenium Grid

QTP

Selenium vs QTP

Selenium - IDE

Features

Install and configure

Selenium - Webdriver

About WebDriver

install and configure

First test case

Web element

Element Locators

Example of locators

Chropath

Web driver commands

Eclipse and TestNG

About Eclipse

Choose and download

About TestNG

Testng installation on eclipse

Testng Annotations

Pass Fail and Skip test cases

Run test cases in batch

testng.xml - Explanation

testng grouping

Running tests parallely

dependsOnMethods

dependsOnGroups

Prioritization

Passing parameters

Data providers

Read data from excel

Ant - XSLT reports

Testng listeners

Cross browser testing

Page Object Model



Selenium - Page Object Model
 


POM (Page Object Model) is a Java design pattern. In our case, in one Page(a Java class), we identify elements and create methods that can be invoked from other classes called Test cases. The java class which consists of methods and identification of elements is also called repository.

For example sample code in Page repository class would look like this:
By username=By.name("username" );
Public void typeUserName(String UName) {
Driver.findElement(username).sendKeys(UName);

Explanation:
1) The first line identifies and locates username in the page.
2) In the second line. We create a method called typeUserName to send the data from other java files.This can be done by importing and creating the instance of Page and calling the method in repository page.
3) The java file/class that contains the methods like typeUserName is called Object/element/centralized repository
4) The java file/class from where we call the method is called a Test case class.
5) Object Repositories also called container class is for centralized maintenance of Objects/Elements.
6) For every page there should be a corresponding Object repository class/ that holds the identification of elements and operations on elements.

ii) Advantages of Page Object Model
1) Code handling effort and amount of code can be reduced.
2) Centralized Maintenance of Elements/Objects
3) Reusability of methods on elements.

iii) Implement Page Object model

1) Sample code for Page object repository as follows

package PomDemo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class RegisterPage {
WebDriver driver;

//Create elements using element locators
By User = By.name("username" );
By Password = By.name("password" );
By ConfirmPassword = By.name("confirmPassword" );

By RegisterButton = By.id("registerButton" );

public RegisterPage(WebDriver driver){
this.driver=driver;
}

public void typeUsername(String Uname1){
abcd.findElement(User).sendKeys(Uname1);
}

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

public void typeConfirmPassword(String cpnfirmpassword){
driver.findElement(Password).sendKeys(cpnfirmpassword);
}

public void clickButton(){
driver.findElement(RegisterButton).click();
}
}



------------------------------------- b) The test case for the above page container is as follows.


package PomDemo;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestCaseDemo {
public static void main(String[] args) {
//Test case 1
System.setProperty("webdriver.chrome.driver" , "C:/chromedriver.exe" );
WebDriver driver = new ChromeDriver();
RegisterPage register = new RegisterPage (driver);
//Test cases for registration as follows
driver.get("http://jobs.slsns.tech" );
register.typeUsername("ananda1" );
register.typePassword("ananda1" );
register.clickRegisterButton();
Thread.sleep(4000);
driver.close();
//Test case 2
//Test case 2
//System.setProperty("webdriver.chrome.driver" , "C:/chromedriver.exe" );
WebDriver driver = new ChromeDriver();
RegisterPage register= new RegisterPage (driver);
//Test cases for registration as follows
driver.get("http://jobs.slsns.tech" );
register.typeUsername("ananda1" );
register.typePassword("ananda1" );
register.typeConfirmPassword("ananda1" );
register.clickRegisterButton();
Thread.sleep(4000);
driver.close();

}
}

Explation of code for page/class RegisterPage.
It's pretty simple. Instead of creating the whole process of identifying element and performing operations on element in one line code, we separate the code of identifying and allows some methods to be invoked from other classes(here test cases).
One line code:
Driver.findElement(By.id("username" )).sendKeys("Ananda" );


Separated code:
By username=By.name("username" );
Public void typeUserName(String UName) {
Driver.findElement(username).sendKeys(UName);
}


We create all the methods(operations on elements) in one specific page as one class called repository, here in our above example, registration page is the repository. Seperated code will be present in repository page.


I conduct online tutions on Manual Testing, Selenium and the following. For more details contact anandaibmindia@gmail.com
Manual Testing - 1600/-           Selenium - 1600/-
           Jsp and Servlet -2000/-           Java training - 2000/-