Search in sources :

Example 1 with SearchContext

use of org.openqa.selenium.SearchContext in project selenium_java by sergueik.

the class TripAdvisorTest method findElements.

private List<WebElement> findElements(String selectorKind, String selectorValue, WebElement parent) {
    SearchContext finder;
    String parent_css_selector = null;
    String parent_xpath = null;
    List<WebElement> elements = null;
    Hashtable<String, Boolean> selectorStrategies = new Hashtable<String, Boolean>();
    selectorStrategies.put("css_selector", true);
    selectorStrategies.put("xpath", true);
    if (selectorKind == null || !selectorStrategies.containsKey(selectorKind) || !selectorStrategies.get(selectorKind)) {
        return null;
    }
    if (parent != null) {
        parent_css_selector = cssSelectorOfElement(parent);
        parent_xpath = xpathOfElement(parent);
        finder = parent;
    } else {
        finder = driver;
    }
    if (selectorKind == "css_selector") {
        String extended_css_selector = String.format("%s  %s", parent_css_selector, selectorValue);
        try {
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(extended_css_selector)));
        } catch (RuntimeException timeoutException) {
            return null;
        }
        elements = finder.findElements(By.cssSelector(selectorValue));
    }
    if (selectorKind == "xpath") {
        String extended_xpath = String.format("%s/%s", parent_xpath, selectorValue);
        try {
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(extended_xpath)));
        } catch (RuntimeException timeoutException) {
            return null;
        }
        elements = finder.findElements(By.xpath(selectorValue));
    }
    return elements;
}
Also used : Hashtable(java.util.Hashtable) SearchContext(org.openqa.selenium.SearchContext) WebElement(org.openqa.selenium.WebElement)

Example 2 with SearchContext

use of org.openqa.selenium.SearchContext in project selenium_java by sergueik.

the class AppTest method findElements.

private List<WebElement> findElements(String selectorKind, String selectorValue, WebElement parent) {
    SearchContext finder;
    String parent_css_selector = null;
    String parent_xpath = null;
    List<WebElement> elements = null;
    Hashtable<String, Boolean> supportedSelectorStrategies = new Hashtable<String, Boolean>();
    supportedSelectorStrategies.put("css_selector", true);
    supportedSelectorStrategies.put("xpath", true);
    if (selectorKind == null || !supportedSelectorStrategies.containsKey(selectorKind) || !supportedSelectorStrategies.get(selectorKind)) {
        return null;
    }
    if (parent != null) {
        parent_css_selector = cssSelectorOfElement(parent);
        parent_xpath = xpathOfElement(parent);
        finder = parent;
    } else {
        finder = driver;
    }
    if (selectorKind == "css_selector") {
        String extended_css_selector = String.format("%s  %s", parent_css_selector, selectorValue);
        try {
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(extended_css_selector)));
        } catch (RuntimeException timeoutException) {
            return null;
        }
        elements = finder.findElements(By.cssSelector(selectorValue));
    }
    if (selectorKind == "xpath") {
        String extended_xpath = String.format("%s/%s", parent_xpath, selectorValue);
        try {
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(extended_xpath)));
        } catch (RuntimeException timeoutException) {
            return null;
        }
        elements = finder.findElements(By.xpath(selectorValue));
    }
    return elements;
}
Also used : RuntimeException(java.lang.RuntimeException) Hashtable(java.util.Hashtable) SearchContext(org.openqa.selenium.SearchContext) WebElement(org.openqa.selenium.WebElement) Boolean(java.lang.Boolean)

Example 3 with SearchContext

use of org.openqa.selenium.SearchContext in project selenium_java by sergueik.

the class App method find_elements.

private static List<WebElement> find_elements(String selector_type, String selector_value, WebElement parent) {
    int flexible_wait_interval = 5;
    SearchContext finder;
    long wait_polling_interval = 500;
    String parent_css_selector = null;
    String parent_xpath = null;
    WebDriverWait wait = new WebDriverWait(driver, flexible_wait_interval);
    wait.pollingEvery(wait_polling_interval, TimeUnit.MILLISECONDS);
    List<WebElement> elements = null;
    Hashtable<String, Boolean> supported_selectors = new Hashtable<String, Boolean>();
    supported_selectors.put("css_selector", true);
    supported_selectors.put("xpath", true);
    if (selector_type == null || !supported_selectors.containsKey(selector_type) || !supported_selectors.get(selector_type)) {
        return null;
    }
    if (parent != null) {
        parent_css_selector = css_selector_of(parent);
        parent_xpath = xpath_of(parent);
        finder = parent;
    } else {
        finder = driver;
    }
    if (selector_type == "css_selector") {
        String extended_css_selector = String.format("%s  %s", parent_css_selector, selector_value);
        try {
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(extended_css_selector)));
        } catch (RuntimeException timeoutException) {
            return null;
        }
        elements = finder.findElements(By.cssSelector(selector_value));
    }
    if (selector_type == "xpath") {
        String extended_xpath = String.format("%s/%s", parent_xpath, selector_value);
        try {
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(extended_xpath)));
        } catch (RuntimeException timeoutException) {
            return null;
        }
        elements = finder.findElements(By.xpath(selector_value));
    }
    return elements;
}
Also used : RuntimeException(java.lang.RuntimeException) Hashtable(java.util.Hashtable) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) SearchContext(org.openqa.selenium.SearchContext) WebElement(org.openqa.selenium.WebElement)

Example 4 with SearchContext

use of org.openqa.selenium.SearchContext in project java-client by appium.

the class ByChained method findElement.

@Override
public WebElement findElement(SearchContext context) {
    AppiumFunction<SearchContext, WebElement> searchingFunction = null;
    for (By by : bys) {
        searchingFunction = Optional.ofNullable(searchingFunction != null ? searchingFunction.andThen(getSearchingFunction(by)) : null).orElse(getSearchingFunction(by));
    }
    FluentWait<SearchContext> waiting = new FluentWait<>(context);
    try {
        checkNotNull(searchingFunction);
        return waiting.until(searchingFunction);
    } catch (TimeoutException e) {
        throw new NoSuchElementException("Cannot locate an element using " + toString());
    }
}
Also used : FluentWait(org.openqa.selenium.support.ui.FluentWait) By(org.openqa.selenium.By) SearchContext(org.openqa.selenium.SearchContext) WebElement(org.openqa.selenium.WebElement) NoSuchElementException(org.openqa.selenium.NoSuchElementException) TimeoutException(org.openqa.selenium.TimeoutException)

Example 5 with SearchContext

use of org.openqa.selenium.SearchContext in project java-client by appium.

the class AppiumElementLocatorFactory method createLocator.

@Override
@Nullable
public CacheableLocator createLocator(AnnotatedElement annotatedElement) {
    TimeOutDuration customDuration;
    if (annotatedElement.isAnnotationPresent(WithTimeout.class)) {
        WithTimeout withTimeout = annotatedElement.getAnnotation(WithTimeout.class);
        customDuration = new TimeOutDuration(withTimeout.time(), withTimeout.unit());
    } else {
        customDuration = duration;
    }
    builder.setAnnotated(annotatedElement);
    By byResult = builder.buildBy();
    return ofNullable(byResult).map(by -> new AppiumElementLocator(searchContext, by, builder.isLookupCached(), customDuration)).orElse(null);
}
Also used : CacheableLocator(io.appium.java_client.pagefactory.locator.CacheableLocator) SearchContext(org.openqa.selenium.SearchContext) CacheableElementLocatorFactory(io.appium.java_client.pagefactory.locator.CacheableElementLocatorFactory) Optional.ofNullable(java.util.Optional.ofNullable) By(org.openqa.selenium.By) Field(java.lang.reflect.Field) Nullable(javax.annotation.Nullable) AppiumByBuilder(io.appium.java_client.pagefactory.bys.builder.AppiumByBuilder) AnnotatedElement(java.lang.reflect.AnnotatedElement) By(org.openqa.selenium.By) Optional.ofNullable(java.util.Optional.ofNullable) Nullable(javax.annotation.Nullable)

Aggregations

SearchContext (org.openqa.selenium.SearchContext)6 WebElement (org.openqa.selenium.WebElement)5 Hashtable (java.util.Hashtable)3 By (org.openqa.selenium.By)3 RuntimeException (java.lang.RuntimeException)2 AppiumByBuilder (io.appium.java_client.pagefactory.bys.builder.AppiumByBuilder)1 CacheableElementLocatorFactory (io.appium.java_client.pagefactory.locator.CacheableElementLocatorFactory)1 CacheableLocator (io.appium.java_client.pagefactory.locator.CacheableLocator)1 Boolean (java.lang.Boolean)1 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 Optional.ofNullable (java.util.Optional.ofNullable)1 Nullable (javax.annotation.Nullable)1 NoSuchElementException (org.openqa.selenium.NoSuchElementException)1 TimeoutException (org.openqa.selenium.TimeoutException)1 FluentWait (org.openqa.selenium.support.ui.FluentWait)1 WebDriverWait (org.openqa.selenium.support.ui.WebDriverWait)1