Search in sources :

Example 1 with ByXPath

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());
        }
    }
}
Also used : CustomEventFiringWebDriver(com.seleniumtests.driver.CustomEventFiringWebDriver) CustomSeleniumTestsException(com.seleniumtests.customexception.CustomSeleniumTestsException) ScenarioException(com.seleniumtests.customexception.ScenarioException) InvalidElementStateException(org.openqa.selenium.InvalidElementStateException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CustomSeleniumTestsException(com.seleniumtests.customexception.CustomSeleniumTestsException) TimeoutException(org.openqa.selenium.TimeoutException) ScreenshotException(org.openqa.selenium.remote.ScreenshotException) NoSuchFrameException(org.openqa.selenium.NoSuchFrameException) ElementNotVisibleException(org.openqa.selenium.ElementNotVisibleException) WebDriverException(org.openqa.selenium.WebDriverException) ConfigurationException(com.seleniumtests.customexception.ConfigurationException) IOException(java.io.IOException) NoSuchElementException(org.openqa.selenium.NoSuchElementException) Field(java.lang.reflect.Field) ByXPath(org.openqa.selenium.By.ByXPath) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) TimeoutException(org.openqa.selenium.TimeoutException)

Aggregations

ConfigurationException (com.seleniumtests.customexception.ConfigurationException)1 CustomSeleniumTestsException (com.seleniumtests.customexception.CustomSeleniumTestsException)1 ScenarioException (com.seleniumtests.customexception.ScenarioException)1 CustomEventFiringWebDriver (com.seleniumtests.driver.CustomEventFiringWebDriver)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ByXPath (org.openqa.selenium.By.ByXPath)1 ElementNotVisibleException (org.openqa.selenium.ElementNotVisibleException)1 InvalidElementStateException (org.openqa.selenium.InvalidElementStateException)1 NoSuchElementException (org.openqa.selenium.NoSuchElementException)1 NoSuchFrameException (org.openqa.selenium.NoSuchFrameException)1 TimeoutException (org.openqa.selenium.TimeoutException)1 WebDriverException (org.openqa.selenium.WebDriverException)1 ScreenshotException (org.openqa.selenium.remote.ScreenshotException)1 WebDriverWait (org.openqa.selenium.support.ui.WebDriverWait)1