use of org.openqa.selenium.By.ByXPath in project seleniumRobot by bhecquet.
the class HtmlElement method findElement.
/**
* Finds the element using By type. Implicit Waits is built in createWebDriver()
* in WebUIDriver to handle dynamic element problem. This method is invoked
* before all the basic operations like click, sendKeys, getText, etc. Use
* waitForPresent to use Explicit Waits to deal with special element which needs
* long time to present.
*
* @param waitForVisibility wait for element to be visible
* @param makeVisible whether we try to make the element visible. Should
* be true except when trying to know if element is
* displayed
*/
public void findElement(boolean waitForVisibility, boolean makeVisible) {
// TODO:
// https://discuss.appium.io/t/how-can-i-scroll-to-an-element-in-appium-im-using-android-native-app/10618/14
// String DESTINATION_ELEMENT_TEXT= "KUBO";
// ((AndroidDriver) driver).findElementByAndroidUIAutomator("new
// UiScrollable(new UiSelector())
// .scrollIntoView(new UiSelector().text(DESTINATION_ELEMENT_TEXT))");
ElementInfo elementInfo = null;
// search element information. Do not stop if something goes wrong here
if (SeleniumTestsContextManager.getThreadContext().getAdvancedElementSearch() != ElementInfo.Mode.FALSE) {
try {
elementInfo = ElementInfo.getInstance(this);
} catch (Exception e) {
logger.info("Error getting element info");
}
}
// if a parent is defined, search for it before getting the sub element
setDriver(updateDriver());
if (parent != null) {
parent.findElement(false, false);
// inside a parent
if (by instanceof ByXPath) {
try {
Field xpathExpressionField = ByXPath.class.getDeclaredField("xpathExpression");
xpathExpressionField.setAccessible(true);
String xpath = (String) xpathExpressionField.get(by);
if (xpath.startsWith("//")) {
by = By.xpath("." + xpath);
}
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
throw new CustomSeleniumTestsException(e);
}
}
setElement(findSeleniumElement(parent.getRealElementNoSearch(), elementInfo));
} else {
setElement(findSeleniumElement(getDriver(), elementInfo));
}
if (makeVisible) {
makeWebElementVisible(getRealElementNoSearch());
if (scrollToElementBeforeAction) {
((CustomEventFiringWebDriver) getDriver()).scrollToElement(getRealElementNoSearch(), OPTIMAL_SCROLLING);
}
}
// element
if (waitForVisibility && makeVisible) {
try {
new WebDriverWait(getDriver(), 1).until(ExpectedConditions.visibilityOf(getRealElementNoSearch()));
} catch (TimeoutException e) {
logger.error(String.format("Element %s has never been made visible", toString()));
}
}
// If we are here, element has been found, update elementInformation
if (elementInfo != null) {
try {
elementInfo.updateInfo(this);
elementInfo.exportToJsonFile(false, this);
} catch (Exception e) {
logger.warn("Error storing element information: " + e.getMessage());
}
}
}
Aggregations