
web.ByHandler Maven / Gradle / Ivy
package web;
import core.reports.TestReporter;
import org.openqa.selenium.By;
import org.testng.Assert;
/**
* Created by Ismail on 12/27/2017.
* This class contains generate SelectorBuilder Type as By object
*/
public class ByHandler {
/*************** Class Methods Section ***************/
// This method to build By(selector type) for element and return By Object
// public static By getSelector(String selector){
// if (selector.startsWith("/"))
// return getSelector(selector, "xpath");
// else if (selector.startsWith(""))
// return getSelector(selector, "");
// else
// return null;
// }
// This method to build By(selector type) for element with format selectorType=selectorValue
public static By getSelector(String selector) {
// Define selector splitter between selector type and value
String splitter = "=";
// Define String format length after splitting
int selectorSplitLength = 2;
//Check String format is correct
try {
Assert.assertTrue(selector.split(splitter).length >= selectorSplitLength, "The provided SelectorBuilder string format is invalid, example: (xpath=XPathValue...), Please check selector string: " + selector);
} catch (Throwable throwable) {
// If fail will add step to report and fail test case
TestReporter.error(throwable.getMessage() + "OR maybe you forget to add selectorsFile", true);
}
// Define selector Type and Value
String selectorType, selectorValue;
// Check if SelectorBuilder String split length more than 2
if (selector.split(splitter).length > 2) {
// Extract Type as always will be first index
selectorType = selector.split(splitter)[0];
// get all other indexes to be SelectorBuilder Value
// by removing selectorType string and = from all string
// selector string so in this way will get remaining as value
selectorValue = selector.replace(selectorType + "=", "");
} else {
// Extract Type and value from string if length just 2
selectorType = selector.split(splitter)[0];
selectorValue = selector.split(splitter)[1];
}
// Return By object of provided selector
return getSelector(selectorType, selectorValue);
}
// This method to build By(selector type) for element
// and return By object to be used in finding WebElement
public static By getSelector(String selectorType, String selectorValue) {
// Define By object
By by = null;
// Check selector type and build By object according
switch (selectorType.toLowerCase()) {
case "id": {
by = By.id(selectorValue);
break;
}
case "name": {
by = By.name(selectorValue);
break;
}
case "class":
case "classname": {
by = By.className(selectorValue);
break;
}
case "css":
case "cssselector":
case "selector": {
by = By.cssSelector(selectorValue);
break;
}
case "linktext": {
by = By.linkText(selectorValue);
break;
}
case "partiallinktext": {
by = By.partialLinkText(selectorValue);
break;
}
case "tagname": {
by = By.tagName(selectorValue);
break;
}
case "xpath": {
by = By.xpath(selectorValue);
break;
}
// If selector type is wrong will fail test case
default: {
TestReporter.fail("The provided selector Type" + " isn't exist, Please check selector Type is: " + selectorType, true);
}
}
// Return result as By object
return by;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy