Last active
July 10, 2016 09:40
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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