Skip to content

Instantly share code, notes, and snippets.

@4M01
Last active July 10, 2016 09:40
Show Gist options
  • Save 4M01/10e311c98054aaac8164d121b85d8cb5 to your computer and use it in GitHub Desktop.
Save 4M01/10e311c98054aaac8164d121b85d8cb5 to your computer and use it in GitHub Desktop.
ObjectMap java is class that helps us to build Object Repository in selenium webdriver.
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class ObjectMap extends ActionUtilities{
Properties properties;
/**
* This is Constructor Method accepts Absolute path of object map file and load it into memory.
* @param mapFilePath
*/
public ObjectMap(String mapFilePath){
properties = new Properties();
try {
FileInputStream fileInputStream = new FileInputStream(mapFilePath);
properties.load(fileInputStream);
fileInputStream.close();
}catch (IOException e) {
System.out.println(e.getMessage());
}
}
/**
* This is Method accepts LogicalElementName present in object map file and returns By of type webdriver.
* @param logicalElementName
*/
public By getLocator(String logicalElementName) {
try {
String locator = properties.getProperty(logicalElementName);
String locatorType = locator.split("~")[0];
String locatorValue = locator.split("~")[1];
switch (locatorType.toUpperCase()) {
case "ID":
return By.id(locatorValue);
case "NAME":
return By.name(locatorValue);
case "CLASSNAME":
return By.className(locatorValue);
case "TAGNAME":
return By.tagName(locatorValue);
case "LINKTEXT":
return By.linkText(locatorValue);
case "PARTIALLINKTEXT":
return By.partialLinkText(locatorValue);
case "CSSSELECTOR":
return By.cssSelector(locatorValue);
case "XPATH":
return By.xpath(locatorValue);
default:
return null;
}
}catch (NullPointerException e){
System.out.println(e.getMessage());
return null;
}
}
/**
* This is Method accepts LogicalElementName present in object map file and returns webelement of type webdriver.
* @param logicalElementName
*/
public WebElement getElement(String logicalElementName){
return driver.findElement(getLocator(logicalElementName));
}
/**
* Property getter
* @param propertyName
*/
public String getProperty(String propertyName){
return properties.getProperty(propertyName);
}
/**
* Property setter.
*/
public void setProperty(String key, String value) {
properties.setProperty(key,value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment