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.synapticpath.naica.selenium.SeleniumUtils.java

License:Open Source License

/**
 * Polls through Selenium driver until an element matched by given {@link SeleniumSelector} can no longer be found.
 * //from  w w  w.j a v  a  2 s  . co m
 * @param elementSelector
 * @param timeout
 *            number of seconds to wait if positive number, otherwise
 *            default will be applied.
 * @return true when element disappears, false otherwise
 */

public static boolean waitUntilElementGoneWithTimeout(final SeleniumSelector elementSelector,
        final int timeout) {

    final WebDriver driver = SeleniumTestContext.getInstance().getDriver();

    FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)
            .withTimeout(timeout > -1 ? timeout : MAX_WAIT, TimeUnit.SECONDS)
            .pollingEvery(POLL_INTERVAL_MILLIS, TimeUnit.MILLISECONDS).ignoring(NoSuchElementException.class);

    try {
        return wait.until((WebDriver d) -> d.findElements(elementSelector.toBySelector()).size() == 0);

    } catch (TimeoutException te) {
        logger.severe(
                format("Element selected by %s did not go away in time.", elementSelector.toBySelector()));
    }

    return false;
}

From source file:com.tascape.qa.th.webui.comm.WebBrowser.java

License:Apache License

public void waitForNoElement(final By by, int seconds) {
    LOG.debug("Wait for element {} to disappear", by);
    WebDriverWait wait = new WebDriverWait(this, seconds);
    wait.until((WebDriver t) -> {
        List<WebElement> es = t.findElements(by);
        if (es.isEmpty()) {
            return true;
        } else {//  w  w w .  j  av a  2  s . com
            return es.stream().noneMatch((e) -> (!e.getCssValue("display").equals("none")));
        }
    });
}

From source file:com.thoughtworks.inproctester.tests.InProcessHtmlUnitDriverTest.java

License:Apache License

@Test
public void shouldSupportCookies() {
    WebDriver htmlUnitDriver = new InProcessHtmlUnitDriver(httpAppTester);
    htmlUnitDriver.manage().deleteAllCookies();

    htmlUnitDriver.get("http://localhost/contacts/add");
    htmlUnitDriver.findElement(By.name("contactName")).sendKeys("My Contact");
    htmlUnitDriver.findElement(By.tagName("form")).submit();

    assertThat(htmlUnitDriver.findElement(By.className("message")).getText(), is("Success"));

    htmlUnitDriver.get("http://localhost/contacts/1");

    Cookie flashMessageCookie = htmlUnitDriver.manage().getCookieNamed("FLASH_MESSAGE");
    assertThat(flashMessageCookie, is(nullValue()));

    assertThat(htmlUnitDriver.findElements(By.className("message")), is(Matchers.<WebElement>empty()));
}

From source file:com.thoughtworks.selenium.corerunner.CoreTest.java

License:Apache License

public void run(Results results, WebDriver driver, Selenium selenium) {
    if (!driver.getCurrentUrl().equals(url)) {
        driver.get(url);/*from  ww  w.  ja  v a2 s .  co  m*/
    }

    // Are we running a suite or an individual test?
    List<WebElement> allTables = driver.findElements(By.id("suiteTable"));

    if (allTables.isEmpty()) {
        new CoreTestCase(url).run(results, driver, selenium);
    } else {
        new CoreTestSuite(url).run(results, driver, selenium);
    }
}

From source file:com.thoughtworks.selenium.corerunner.CoreTestSuite.java

License:Apache License

public void run(Results results, WebDriver driver, Selenium selenium) {
    if (!url.equals(driver.getCurrentUrl())) {
        driver.get(url);/*w  w  w  . j  av  a  2  s  . c o m*/
    }

    List<WebElement> allTables = driver.findElements(By.id("suiteTable"));
    if (allTables.isEmpty()) {
        throw new SeleniumException("Unable to locate suite table: " + url);
    }

    List<String> allTestUrls = (List<String>) ((JavascriptExecutor) driver).executeScript(
            "var toReturn = [];\n" + "for (var i = 0; i < arguments[0].rows.length; i++) {\n"
                    + "  if (arguments[0].rows[i].cells.length == 0) {\n" + "    continue;\n" + "  }\n"
                    + "  var cell = arguments[0].rows[i].cells[0];\n" + "  if (!cell) { continue; }\n"
                    + "  var allLinks = cell.getElementsByTagName('a');\n" + "  if (allLinks.length > 0) {\n"
                    + "    toReturn.push(allLinks[0].href);\n" + "  }\n" + "}\n" + "return toReturn;\n",
            allTables.get(0));

    for (String testUrl : allTestUrls) {
        new CoreTest(testUrl).run(results, driver, selenium);
    }
}

From source file:com.thoughtworks.selenium.webdriven.commands.GetAllButtons.java

License:Apache License

@Override
protected String[] handleSeleneseCommand(WebDriver driver, String ignored, String alsoIgnored) {
    List<WebElement> allInputs = driver.findElements(By.xpath("//input"));
    List<String> ids = new ArrayList<>();

    for (WebElement input : allInputs) {
        String type = input.getAttribute("type").toLowerCase();
        if ("button".equals(type) || "submit".equals(type) || "reset".equals(type))
            ids.add(input.getAttribute("id"));
    }/*from   www.j a va2 s.  co  m*/

    return ids.toArray(new String[ids.size()]);
}

From source file:com.thoughtworks.selenium.webdriven.commands.GetAllFields.java

License:Apache License

@Override
protected String[] handleSeleneseCommand(WebDriver driver, String locator, String value) {
    List<WebElement> allInputs = driver.findElements(By.xpath("//input"));
    List<String> ids = new ArrayList<>();

    for (WebElement input : allInputs) {
        String type = input.getAttribute("type").toLowerCase();
        if ("text".equals(type))
            ids.add(input.getAttribute("id"));
    }//from w ww  .  j av a 2 s  . co m

    return ids.toArray(new String[ids.size()]);
}

From source file:com.thoughtworks.selenium.webdriven.commands.GetAllLinks.java

License:Apache License

@Override
protected String[] handleSeleneseCommand(WebDriver driver, String locator, String value) {
    List<WebElement> allLinks = driver.findElements(By.xpath("//a"));
    List<String> links = new ArrayList<>();
    for (WebElement link : allLinks) {
        String id = link.getAttribute("id");
        links.add(id == null ? "" : id);
    }// w w  w .  j  av a 2  s .com

    return links.toArray(new String[links.size()]);

}

From source file:com.thoughtworks.selenium.webdriven.commands.GetCssCount.java

License:Apache License

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

From source file:com.thoughtworks.selenium.webdriven.commands.GetXpathCount.java

License:Apache License

@Override
protected Number handleSeleneseCommand(WebDriver driver, String xpath, String ignored) {
    return driver.findElements(By.xpath(xpath)).size();
}