Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20



Using Object Map in Selenium Web Driver Automation

In Selenium test automation, defining web page objects in a central place is very important and helpful to maintain the application for any kind of UI changes over time. In this example, we will create an Object Map for Selenium Login Test. We will create a properties file named "" under a folder named "ObjectPropertiesFile" and store all the page element object definition in this file. When tests are running, the test application reads object locator type and locator value from the file and interact with the web elements. 

 Login Test Case:

1. Go to
2. Enter "test" in the Username field
3. Enter "XXXX" in the Password filed
4. Click on the Login button
5. Verify that the text "Selenium Test" is present.

Step 1: on Java Eclipse, create a project named "SeleniumMasterObjectMap" and add the two classes below. Make sure add Selenium Java webdriver jar files as references in the project build path. 


(2) (TestNG Class)

and also create a folder named "ObjectPropertiesFile" and add a file named "". 

Step 2: write the following code in the class


package com.seleniummaster.ObjectMapProject;
import java.util.Properties;
import org.openqa.selenium.By;

Creating an object map for Selenium tests
1) We will create a properties file named "".
Format :  [logical_name]=[locator_type]:[locator_value]
                 Username_field = id:login_login_username
        Password_field = id:login_login_password
        Login_button = id:login_submit
        online_user=cssSelector:#sb-onlineusers > h3

public class ObjectMap {
  //property file and provide the locator information to the test.
  Properties properties;
  public ObjectMap(String mapFile)
      properties = new Properties();
      try {
      FileInputStream Master = new FileInputStream(mapFile);
          }catch (IOException e) {

   public By getLocator(String ElementNamethrows Exception {
         //Read value using the logical name as Key
         String locator = properties.getProperty(ElementName);
         //Split the value which contains locator type and locator value
         String locatorType = locator.split(":")[0];
         String locatorValue = locator.split(":")[1];
         //Return a instance of By class based on type of locator
           else if(locatorType.toLowerCase().equals("name"))
           else if((locatorType.toLowerCase().equals("classname")) || (locatorType.toLowerCase().equals("class")))
                 return By.className(locatorValue);
           else if((locatorType.toLowerCase().equals("tagname")) || (locatorType.toLowerCase().equals("tag")))
                 return By.className(locatorValue);
           else if((locatorType.toLowerCase().equals("linktext")) || (locatorType.toLowerCase().equals("link")))
                 return By.linkText(locatorValue);
           else if(locatorType.toLowerCase().equals("partiallinktext"))
                 return By.partialLinkText(locatorValue);
           else if((locatorType.toLowerCase().equals("cssselector")) || (locatorType.toLowerCase().equals("css")))
                 return By.cssSelector(locatorValue);
           else if(locatorType.toLowerCase().equals("xpath"))
                 return By.xpath(locatorValue);
                   throw new Exception("Locator type '" + locatorType + "' not defined!!");

Step 3: write the following code in the

package com.seleniummaster.ObjectMapProject;
import static org.testng.AssertJUnit.assertEquals;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.openqa.selenium.WebElement;

public class ObjectMapLoginTest {
  public WebDriver driver;
  public ObjectMap objmap;
  public void setUp() throws Exception {
  // Create a new instance of the Firefox driver
  driver = new FirefoxDriver();
  public void login() throws Exception{
    //Get current working directory
    String workingDir=System.getProperty("user.dir");
    //Get object map file
     objmap = new ObjectMap (workingDir+"\\objectpropertiesfile\\");
      //Get the username element
     WebElement username = driver.findElement(objmap.getLocator("Username_field"));
    //Get the password element
     WebElement password = driver.findElement(objmap.getLocator("Password_field"));
     password.sendKeys("XXXXXX");//password is omitted
    //Click on the login button
     WebElement login = driver.findElement(objmap.getLocator("Login_button"));;
     //Assert the user login by checking the Online user
     WebElement onlineuser=driver.findElement(objmap.getLocator("online_user"));
     assertEquals("Online users", onlineuser.getText());
  public void tearDown() throws Exception {
             //Close the browser

Step 4: add the content below to the file

Username_field =id:login_login_username
Password_field =id:login_login_password
Login_button =id:login_submit
online_user=cssSelector:#sb-onlineusers > h3

Step 5: right click on the class and select Run As "TestNG Test" command. 

Step 6: verify test result on the console or in the test-output folder. The console below shows that the test passed. test-output folder should be located under the project folder.