Example usage for org.openqa.selenium WebDriver findElements

List of usage examples for org.openqa.selenium WebDriver findElements

Introduction

In this page you can find the example usage for org.openqa.selenium WebDriver findElements.

Prototype

@Override
List<WebElement> findElements(By by);

Source Link

Document

Find all elements within the current page using the given mechanism.

Usage

From source file:com.pentaho.ctools.utils.ElementHelper.java

License:Apache License

/**
 * The method pretends to wait for an element reach the expected attribute
 * value, specifying a timeout./*from  w  ww .  j  a v  a  2s .co m*/
 *
 * @param driver
 * @param locator
 * @param attributeName
 * @param attributeValue - attribute value to wait.
 */
public void WaitForAttributeValue(final WebDriver driver, final By locator, final String attributeName,
        final String attributeValue, final Integer timeout) {
    this.log.debug("WaitForAttributeValue::Enter");
    this.log.debug("Locator: " + locator.toString());
    this.log.debug("Attribute: " + attributeName);
    this.log.debug("AttributeValue: " + attributeValue);
    ExecutorService executor = null;
    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);

    try {

        class RunnableObject implements Runnable {

            @Override
            public void run() {
                Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(timeout, TimeUnit.SECONDS)
                        .pollingEvery(50, TimeUnit.MILLISECONDS);

                // Wait for element visible
                wait.until(new Function<WebDriver, Boolean>() {

                    @Override
                    public Boolean apply(WebDriver d) {
                        try {
                            List<WebElement> listElements = d.findElements(locator);
                            if (listElements.size() > 0) {
                                WebElement element = listElements.get(0);
                                String attrValue = element.getAttribute(attributeName).toLowerCase();
                                String attrValueFor = attributeValue.toLowerCase();
                                return attrValue.contains(attrValueFor);
                            }
                            return false;
                        } catch (StaleElementReferenceException sere) {
                            return true;
                        }
                    }
                });
            }
        }

        RunnableObject r = new RunnableObject();
        executor = Executors.newSingleThreadExecutor();
        executor.submit(r).get(timeout + 2, TimeUnit.SECONDS);
    } catch (InterruptedException ie) {
        this.log.warn("Interrupted Exception");
        this.log.warn(ie.toString());
    } catch (ExecutionException ee) {
        if (ee.getCause().getClass().getCanonicalName()
                .equalsIgnoreCase(TimeoutException.class.getCanonicalName())) {
            this.log.warn("WebDriver timeout exceeded! Looking for: " + locator.toString());
        } else {
            this.log.warn("Execution Exception");
            this.log.warn(ee.toString());
        }
    } catch (java.util.concurrent.TimeoutException cte) {
        this.log.warn("Thread timeout exceeded! Looking for: " + locator.toString());
        this.log.warn(cte.toString());
    } catch (Exception e) {
        this.log.error("Exception");
        this.log.catching(e);
    }

    if (executor != null) {
        executor.shutdown();
    }

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    this.log.debug("WaitForAttributeValue::Exit");
}

From source file:com.pentaho.ctools.utils.ElementHelper.java

License:Apache License

/**
 * The method pretends to wait for an element reach the expected attribute
 * value, specifying a timeout.//from  w w w  .j a  v a 2s .c om
 *
 * @param driver
 * @param locator
 * @param attributeName
 * @param attributeValue - attribute value to wait.
 */
public void WaitForAttributeValueEqualsTo(final WebDriver driver, final By locator, final String attributeName,
        final String attributeValue, final Integer timeout) {
    this.log.debug("WaitForAttributeValue::Enter");
    this.log.debug("Locator: " + locator.toString());
    this.log.debug("Attribute: " + attributeName);
    this.log.debug("AttributeValue: " + attributeValue);
    ExecutorService executor = null;
    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);

    try {

        class RunnableObject implements Runnable {

            @Override
            public void run() {
                Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(timeout, TimeUnit.SECONDS)
                        .pollingEvery(50, TimeUnit.MILLISECONDS);

                // Wait for element visible
                wait.until(new Function<WebDriver, Boolean>() {

                    @Override
                    public Boolean apply(WebDriver d) {
                        try {
                            List<WebElement> listElements = d.findElements(locator);
                            if (listElements.size() > 0) {
                                WebElement element = listElements.get(0);
                                String attrValue = element.getAttribute(attributeName).toLowerCase();
                                String attrValueFor = attributeValue.toLowerCase();
                                return attrValue.equals(attrValueFor);
                            }
                            return false;
                        } catch (StaleElementReferenceException sere) {
                            return true;
                        }
                    }
                });
            }
        }

        RunnableObject r = new RunnableObject();
        executor = Executors.newSingleThreadExecutor();
        executor.submit(r).get(timeout + 2, TimeUnit.SECONDS);
    } catch (InterruptedException ie) {
        this.log.warn("Interrupted Exception");
        this.log.warn(ie.toString());
    } catch (ExecutionException ee) {
        if (ee.getCause().getClass().getCanonicalName()
                .equalsIgnoreCase(TimeoutException.class.getCanonicalName())) {
            this.log.warn("WebDriver timeout exceeded! Looking for: " + locator.toString());
        } else {
            this.log.warn("Execution Exception");
            this.log.warn(ee.toString());
        }
    } catch (java.util.concurrent.TimeoutException cte) {
        this.log.warn("Thread timeout exceeded! Looking for: " + locator.toString());
        this.log.warn(cte.toString());
    } catch (Exception e) {
        this.log.error("Exception");
        this.log.catching(e);
    }

    if (executor != null) {
        executor.shutdown();
    }

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    this.log.debug("WaitForAttributeValue::Exit");
}

From source file:com.qmetry.qaf.automation.ui.selenium.webdriver.GetCssCountCommand.java

License:Open Source License

@Override
protected Number handleSeleneseCommand(WebDriver driver, String locator, String ignored) {
    return driver.findElements(By.cssSelector(locator.replaceFirst("css=", ""))).size();
}

From source file:com.raja.anucarita.SeCustomUtils.java

License:Open Source License

public static List<WebElement> elementsReturn(WebDriver driver, String locator) throws Exception {
    byMethod = locator.split("=", 2)[0];
    actualLocator = locator.split("=", 2)[1];

    if (byMethod.equalsIgnoreCase("css")) {
        elements = driver.findElements(By.cssSelector(actualLocator));
    } else if (byMethod.equalsIgnoreCase("jQuery")) {
        /*//from   ww  w .  jav  a 2 s .c  o m
        Need to write code for iterating multiple elements with jQuery
        */
        String Timeout = values.getProperty("timeout");

        final String LocatorTwo = actualLocator;
        try {
            wait = new WebDriverWait(driver, Integer.parseInt(Timeout));
            wait.until(new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver driver) {
                    Result = false;
                    try {
                        element = findElementByjQuery(driver, LocatorTwo);
                        if (element instanceof WebElement) {
                            Result = true;
                        } else {
                            Result = false;
                        }
                    } catch (Exception e) {
                    }
                    return Result;
                }
            });
        } catch (Exception e) {
        }

        element = findElementByjQuery(driver, actualLocator);
        elements.add(element);
        /**/
    } else if (byMethod.equalsIgnoreCase("linkText")) {
        elements = driver.findElements(By.linkText(actualLocator));
    } else if (byMethod.equalsIgnoreCase("id")) {
        elements = driver.findElements(By.id(actualLocator));
    } else if (byMethod.equalsIgnoreCase("name")) {
        elements = driver.findElements(By.name(actualLocator));
    } else if (byMethod.equalsIgnoreCase("ByIDorName")) {
        driver.findElements(new ByIdOrName(actualLocator));
    } else if (byMethod.equalsIgnoreCase("partialLinkText")) {
        elements = driver.findElements(By.partialLinkText(actualLocator));
    } else if (byMethod.equalsIgnoreCase("xpath")) {
        elements = driver.findElements(By.xpath(actualLocator));
    } else {
    }
    return elements;
}

From source file:com.sios.stc.coseng.test.Base.java

License:Open Source License

public void acceptSslCertificate(final WebDriver webDriver) throws InterruptedException {
    // For Internet Explorer
    if (!browser.equals(Browser.FIREFOX) || !browser.equals(Browser.CHROME)) {
        final Boolean title = webDriver.getTitle().equals("Certificate Error: Navigation Blocked");
        final int count = webDriver.findElements(By.id("overridelink")).size();
        if (title && (count == 1)) {
            final WebElement overrideLink = webDriver.findElement(By.id("overridelink"));
            if (overrideLink.isDisplayed()) {
                new Actions(driver).moveToElement(overrideLink).click().build().perform();
            }//from  w w w  .  jav a 2 s . c  o  m
        }
    }
}

From source file:com.smash.revolance.ui.model.helper.BotHelper.java

License:Open Source License

public static List<WebElement> getRawElements(Bot bot, Page page) throws Exception {
    UserHelper.browseTo(page);/*from   w ww  . j a v  a  2 s .  c o m*/
    WebDriver browser = bot.getBrowser();
    return browser.findElements(By.xpath("//body//*"));
}

From source file:com.smash.revolance.ui.model.helper.BotHelper.java

License:Open Source License

public static List<WebElement> getRawLinks(Bot bot, Page page) throws Exception {
    UserHelper.browseTo(page);/*from  w  ww  .j  a v  a  2  s  .co m*/
    WebDriver browser = bot.getBrowser();

    List<WebElement> elements = new ArrayList();

    By selector = By.xpath("//body//a");
    elements.addAll(browser.findElements(selector));

    return elements;
}

From source file:com.smash.revolance.ui.model.helper.BotHelper.java

License:Open Source License

public static List<WebElement> getRawButtons(Bot bot, Page page) throws Exception {
    UserHelper.browseTo(page);//from w ww. j  av a  2  s.c o  m
    WebDriver browser = bot.getBrowser();

    List<WebElement> elements = new ArrayList();

    By selector = By.xpath("//body//input");
    elements.addAll(browser.findElements(selector));

    selector = By.xpath("//body//button");
    elements.addAll(browser.findElements(selector));

    return elements;
}

From source file:com.smash.revolance.ui.model.helper.BotHelper.java

License:Open Source License

public static List<WebElement> getRawImages(Bot bot, Page page) throws Exception {
    UserHelper.browseTo(page);/* ww w . j a va2  s .  co  m*/
    WebDriver browser = bot.getBrowser();

    List<WebElement> elements = new ArrayList();

    By selector = By.xpath("//body//*");
    elements.addAll(browser.findElements(selector));

    return elements;
}

From source file:com.springer.omelet.common.ExpectedConditionExtended.java

License:Apache License

/**
 * An expectation for checking that an element is either invisible or not
 * present on the DOM.//from   w w w . j a  v a  2  s  .  c  o m
 * 
 * @param locator
 *            used to find the element
 */
public static ExpectedCondition<Boolean> invisibilityOfElementLocated(final By locator) {
    return new ExpectedCondition<Boolean>() {

        public Boolean apply(WebDriver driver) {
            try {
                return driver.findElements(locator).size() == 0;
            } catch (NoSuchElementException e) {
                return true;
            } catch (StaleElementReferenceException e) {
                // Returns true , need to check if stale means invisible
                return true;
            }
        }

        @Override
        public String toString() {
            return "element to no longer be visible: ";
        }
    };
}