...3import java.util.NoSuchElementException;4import com.google.common.base.Function;5import org.apache.commons.lang.NotImplementedException;6import org.openqa.selenium.By;7import org.openqa.selenium.ElementNotVisibleException;8import org.openqa.selenium.SearchContext;9import org.openqa.selenium.WebDriver;10import org.openqa.selenium.WebElement;11import org.openqa.selenium.support.ByIdOrName;12import org.openqa.selenium.support.locators.RelativeLocator;13import org.openqa.selenium.support.locators.RelativeLocator.RelativeBy;14import org.openqa.selenium.support.pagefactory.ByChained;15import org.openqa.selenium.support.ui.WebDriverWait;16import github.isanjeevkumar.enums.RelativeByLocator;17public class WebElementFinder {18 public static WebElement getElement(WebDriver driver, final By elementLocator, int timeOutInSecs)19 throws Exception {20 try {21 WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeOutInSecs));22 wait.ignoring(NoSuchElementException.class);23 wait.ignoring(ElementNotVisibleException.class);24 return wait.until(new Function<WebDriver, WebElement>() {25 public WebElement apply(WebDriver driver) {26 return getElement(driver, elementLocator);27 }28 });29 } catch (Exception ex) {30 System.err.println("Error:" + ex.getMessage());31 throw ex;32 }33 }34 public static WebElement getElementByIdOrName(WebDriver driver, final String byIdOrName, int timeOutInSecs)35 throws Exception {36 try {37 WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeOutInSecs));38 wait.ignoring(NoSuchElementException.class);39 wait.ignoring(ElementNotVisibleException.class);40 return wait.until(new Function<WebDriver, WebElement>() {41 public WebElement apply(WebDriver driver) {42 return getByIdOrName(driver, byIdOrName);43 }44 });45 } catch (Exception ex) {46 System.err.println("Error:" + ex.getMessage());47 throw ex;48 }49 }50 public static WebElement getElementByChained(WebDriver driver, final ByChained chainedValue, int timeOutInSecs)51 throws Exception {52 try {53 WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeOutInSecs));54 wait.ignoring(NoSuchElementException.class);55 wait.ignoring(ElementNotVisibleException.class);56 return wait.until(new Function<WebDriver, WebElement>() {57 public WebElement apply(WebDriver driver) {58 return getByChained(driver, chainedValue);59 }60 });61 } catch (Exception ex) {62 System.err.println("Error:" + ex.getMessage());63 throw ex;64 }65 }66 public static WebElement getElementByRelativeLocator(WebDriver driver, WebElement referenceElement,67 final By elementLocator, RelativeByLocator relativeBy,68 int timeOutInSecs)69 throws Exception {70 try {71 WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeOutInSecs));72 wait.ignoring(NoSuchElementException.class);73 wait.ignoring(ElementNotVisibleException.class);74 return wait.until(new Function<WebDriver, WebElement>() {75 public WebElement apply(WebDriver driver) {76 return getRelativeElement(driver, referenceElement, elementLocator, relativeBy);77 }78 });79 } catch (Exception ex) {80 System.err.println("Error:" + ex.getMessage());81 throw ex;82 }83 }84 private static WebElement getElement(final SearchContext searchContext, final By elementLocator) {85 WebElement element = searchContext.findElement(elementLocator);86 if (element.isDisplayed() && element.isEnabled()) {87 return element;88 }89 throw new ElementNotVisibleException("WebElement with locator " + elementLocator + " is not visible.");90 }91 private static WebElement getByIdOrName(final SearchContext searchContext, final String elementLocatorValue) {92 WebElement element = searchContext.findElement(new ByIdOrName(elementLocatorValue));93 if (element.isDisplayed() && element.isEnabled()) {94 return element;95 }96 throw new ElementNotVisibleException("WebElement with locator " + elementLocatorValue + " is not visible.");97 }98 private static WebElement getByChained(final SearchContext searchContext, final ByChained getByChained) {99 WebElement element = searchContext.findElement(getByChained);100 if (element.isDisplayed() && element.isEnabled()) {101 return element;102 }103 throw new ElementNotVisibleException("WebElement with locator " + getByChained.toString() + " is not visible.");104 }105 private static WebElement getRelativeElement(final SearchContext searchContext, WebElement referenceElement,106 final By elementLocator, RelativeByLocator relativeLocator) {107 RelativeBy relativeBy = RelativeLocator.with(elementLocator);108 WebElement element = null;109 switch (relativeLocator) {110 case ABOVE:111 element = searchContext.findElement(relativeBy.above(referenceElement));112 break;113 case BELOW:114 element = searchContext.findElement(relativeBy.below(referenceElement));115 break;116 case LEFT_OF:117 element = searchContext.findElement(relativeBy.toLeftOf(referenceElement));118 break;119 case NEAR:120 element = searchContext.findElement(relativeBy.near(referenceElement));121 break;122 case RIGHT_OF:123 element = searchContext.findElement(relativeBy.toRightOf(referenceElement));124 break;125 default:126 throw new NotImplementedException("relativeLocatorBy is not implemented. Please check.");127 }128 if (element.isDisplayed() && element.isEnabled()) {129 return element;130 }131 throw new ElementNotVisibleException(132 "WebElement with locator " + elementLocator.toString() + " is not visible.");133 }134}...