Search in sources :

Example 6 with SearchContext

use of org.openqa.selenium.SearchContext in project acceptance-test-harness by jenkinsci.

the class ByFactory method areaPath.

/**
 * "/foo/bar" matches div elements with path attribute "/foo/bar" or "/foo/bar[n]". Does not match "/foo/bar/baz" or "/foo/bar[1]/baz".
 */
public By areaPath(final String pathPrefix) {
    final int prefixLength = pathPrefix.length();
    final By xpath = ByFactory.this.xpath("//div[starts-with(@path, '%s')]", pathPrefix);
    return new By() {

        @Override
        public List<WebElement> findElements(SearchContext context) {
            ArrayList<WebElement> ret = new ArrayList<>();
            List<WebElement> allPrefixed = context.findElements(xpath);
            for (WebElement webElement : allPrefixed) {
                String path = webElement.getAttribute("path");
                // Ensure /foo matches /foo and /boo[bar], but not /foo/bar or /foolish/bartender
                if (path.substring(prefixLength).matches("^(\\[[^\\]]+\\]|)$")) {
                    ret.add(webElement);
                }
            }
            return ret;
        }

        @Override
        public String toString() {
            return "By page area name: " + pathPrefix;
        }
    };
}
Also used : By(org.openqa.selenium.By) ArrayList(java.util.ArrayList) SearchContext(org.openqa.selenium.SearchContext) WebElement(org.openqa.selenium.WebElement)

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