Example usage for org.openqa.selenium WebElement findElements

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

Introduction

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

Prototype

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

Source Link

Document

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

Usage

From source file:com.hotwire.test.steps.search.air.AirSearchModelWebApp.java

License:Open Source License

@Override
public void validateNumberOfPassengers() {
    WebElement fareFinderForm = new AirSearchFragment(getWebdriverInstance()).getFareFinderForm();
    //Clicking to make options visible.
    fareFinderForm.findElement(By.cssSelector("a#ui-id-1-button")).click();
    List<WebElement> options = fareFinderForm.findElements(By.cssSelector(".ui-menu li"));
    for (WebElement option : options) {
        assertThat(option.getText()).isNotIn("7", "8")
                .as("AMP14 verison test should reduce maximum number of passengers to 6");
    }/*from w  w  w  .  ja  v  a2  s.  c  o m*/
}

From source file:com.hp.test.framework.jelly.ClickCheckboxBasedonTextLinkinTableTag.java

@Override
public void doTag(XMLOutput arg0) throws MissingAttributeException, JellyTagException {
    logger.info("Started Execution of ClickCheckboxBasedonTextLinkinTableTag");
    WebDriver driver = getSelenium();/*ww w  .j  av  a  2  s .c  om*/
    int loc = id.indexOf("=");
    String locator = id.substring(loc + 1);
    String bylocator = id.substring(0, loc);
    String findelementby = "";

    switch (bylocator) {
    case "id": {
        logger.info("Executing case id");
        try {
            WebElement table = driver.findElement(By.id(locator));
            findelementby = "WebElement table = driver.findElement(By.id(" + locator + "));\n";
            // Now get all the TR elements from the table 
            List<WebElement> allRows = table.findElements(By.tagName("tr"));

            // And iterate over them, getting the cells 
            for (WebElement row : allRows) {
                List<WebElement> cells = row.findElements(By.tagName("td"));

                // iterate each cell and match value
                for (WebElement cell : cells) {

                    if (cell.getText().equalsIgnoreCase(value)) {
                        System.out.println(cell.getText());
                        WebElement checkbox = row.findElement(By.xpath(".//td/input[@type='checkbox']"));
                        checkbox.click();
                        break;

                    }
                }

            }
        } catch (NoSuchElementException e) {
            System.out.println("Element not found" + "\n" + e.getMessage());

        }
    }
    case "name": {
        logger.info("Executing case name");
        try {
            WebElement table = driver.findElement(By.name(locator));
            findelementby = "WebElement table = driver.findElement(By.name(" + locator + "));\n";
            // Now get all the TR elements from the table 
            List<WebElement> allRows = table.findElements(By.tagName("tr"));

            // And iterate over them, getting the cells 
            for (WebElement row : allRows) {
                List<WebElement> cells = row.findElements(By.tagName("td"));

                // iterate each cell and match value
                for (WebElement cell : cells) {

                    if (cell.getText().equalsIgnoreCase(value)) {
                        System.out.println(cell.getText());
                        WebElement checkbox = row.findElement(By.xpath(".//td/input[@type='checkbox']"));
                        checkbox.click();
                        break;

                    }
                }

            }
        } catch (NoSuchElementException e) {
            System.out.println("Element not found" + "\n" + e.getMessage());

        }
    }
    case "xpath": {
        logger.info("Executing case xpath");
        try {
            WebElement table = driver.findElement(By.xpath(locator));
            findelementby = "WebElement table = driver.findElement(By.xpath(" + locator + "));\n";
            // Now get all the TR elements from the table 
            List<WebElement> allRows = table.findElements(By.tagName("tr"));

            // And iterate over them, getting the cells 
            for (WebElement row : allRows) {
                List<WebElement> cells = row.findElements(By.tagName("td"));

                // iterate each cell and match value
                for (WebElement cell : cells) {

                    if (cell.getText().equalsIgnoreCase(value)) {
                        System.out.println(cell.getText());
                        WebElement checkbox = row.findElement(By.xpath(".//td/input[@type='checkbox']"));
                        checkbox.click();
                        break;

                    }
                }

            }
        } catch (NoSuchElementException e) {
            System.out.println("Element not found" + "\n" + e.getMessage());

        }

    }

    case "css": {
        logger.info("Executing case css");
        try {
            WebElement table = driver.findElement(By.cssSelector(locator));
            findelementby = "WebElement table = driver.findElement(By.cssSelector(" + locator + "));\n";
            // Now get all the TR elements from the table 
            List<WebElement> allRows = table.findElements(By.tagName("tr"));

            // And iterate over them, getting the cells 
            for (WebElement row : allRows) {
                List<WebElement> cells = row.findElements(By.tagName("td"));

                // iterate each cell and match value
                for (WebElement cell : cells) {

                    if (cell.getText().equalsIgnoreCase(value)) {
                        System.out.println(cell.getText());
                        WebElement checkbox = row.findElement(By.xpath(".//td/input[@type='checkbox']"));
                        checkbox.click();
                        break;

                    }
                }

            }
        } catch (NoSuchElementException e) {
            System.out.println("Element not found" + "\n" + e.getMessage());

        }

    }

        logger.info("Completed Execution of ClickCheckboxBasedonTextLinkinTableTag");
    }

    String Command = "try { \n " + findelementby
            + " List<WebElement> allRows = table.findElements(By.tagName(\"tr\"));\n";
    Command = Command
            + "for (WebElement row : allRows) {\n  List<WebElement> cells = row.findElements(By.tagName(\"td\"));\n";
    Command = Command + "for (WebElement cell : cells) {\n ";
    Command = Command + "   if (cell.getText().equalsIgnoreCase(value)) {\n";
    Command = Command + "   System.out.println(cell.getText());\n";
    Command = Command
            + " WebElement checkbox = row.findElement(By.xpath(\".//td/input[@type='checkbox']\"));\n";
    Command = Command + " checkbox.click();\nbreak\n";
    Command = Command + "}\n}\n}\n";
    Command = Command
            + "} catch (NoSuchElementException e) { System.out.println(\"Element not found\" + e.getMessage());}";

    if (GenerateperformanceTests) {
        try {
            StartBrowserTag.JunitTestcase_fw.write(Command);
        } catch (IOException ex) {
            System.out.println("Exception in Writing Junit testcase file" + "\n" + ex.getMessage());
        }
    }

}

From source file:com.hp.test.framework.jelly.ClickLinkinTableTag.java

@Override
public void doTag(XMLOutput arg0) throws MissingAttributeException, JellyTagException {
    logger.info("Started Execution of ClickLinkinTableTag");
    WebDriver driver = getSelenium();/* ww w .  j ava 2 s . com*/
    int loc = id.indexOf("=");
    String locator = id.substring(loc + 1);
    String bylocator = id.substring(0, loc);
    String elelocator = "";
    switch (bylocator) {
    case "id": {
        logger.info("Executing case id");
        try {
            WebElement table = driver.findElement(By.id(locator));
            elelocator = "WebElement table = driver.findElement(By.id(\"" + locator + "\"));\n";
            List<WebElement> links = table.findElements(By.tagName("a"));
            for (WebElement link : links) {
                //               String s = link.getText();
                //               s=s.trim();
                //               System.out.println(s);
                if (link.getText().equalsIgnoreCase(value)) {
                    logger.info("Executing if condition for each link");
                    link.click();
                    logger.info("Clicked the link successfully");
                    break;
                }
            }
        } catch (NoSuchElementException e) {
            System.out.println("Element not found" + "\n" + e.getMessage());

        }
    }
    case "name": {
        logger.info("Executing case name");
        try {
            WebElement table = driver.findElement(By.name(locator));
            elelocator = "WebElement table = driver.findElement(By.name(\"" + locator + "\"));\n";
            List<WebElement> links = table.findElements(By.tagName("a"));
            for (WebElement link : links) {
                //               String s = link.getText();
                //               s=s.trim();
                //               System.out.println(s);
                if (link.getText().equalsIgnoreCase(value)) {
                    logger.info("Executing if condition for each link");
                    link.click();
                    logger.info("Clicked the link successfully");
                    break;
                }
            }
        } catch (NoSuchElementException e) {
            System.out.println("Element not found" + "\n" + e.getMessage());

        }
    }
    case "xpath": {
        logger.info("Executing case xpath");
        try {
            WebElement table = driver.findElement(By.xpath(locator));
            elelocator = "WebElement table = driver.findElement(By.xpath(\"" + locator + "\"));\n";
            List<WebElement> links = table.findElements(By.tagName("a"));
            for (WebElement link : links) {
                //               String s = link.getText();
                //               s=s.trim();
                //               System.out.println(s);
                if (link.getText().equalsIgnoreCase(value)) {
                    logger.info("Executing if condition for each link");
                    link.click();
                    logger.info("Clicked the link successfully");
                    break;
                }
            }
        } catch (NoSuchElementException e) {
            System.out.println("Element not found" + "\n" + e.getMessage());

        }
    }

    case "css": {
        logger.info("Executing case css");
        try {
            WebElement table = driver.findElement(By.cssSelector(locator));
            elelocator = "WebElement table = driver.findElement(By.cssSelector(\"" + locator + "\"));\n";
            List<WebElement> links = table.findElements(By.tagName("a"));
            for (WebElement link : links) {
                //               String s = link.getText();
                //               s=s.trim();
                //               System.out.println(s);
                if (link.getText().equalsIgnoreCase(value)) {
                    logger.info("Executing if condition for each link");
                    link.click();
                    logger.info("Clicked the link successfully");
                    break;
                }
            }
        } catch (NoSuchElementException e) {
            System.out.println("Element not found" + e.getMessage());

        }
    }
        logger.info("Completed Execution of ClickLinkinTableTag");
    }

    String Command = "try { \n " + elelocator
            + " List<WebElement> links = table.findElements(By.tagName(\"a\"));\n ";
    Command = Command + "for (WebElement link : links) {\n if (link.getText().equalsIgnoreCase(\"" + value
            + "\")) { \n";
    Command = Command + " link.click();\n break;\n}\n}\n";
    Command = Command
            + "}catch (NoSuchElementException e) { System.out.println(\"Element not found\" + e.getMessage());}";
    if (GenerateperformanceTests) {
        try {
            StartBrowserTag.JunitTestcase_fw.write(Command);
        } catch (IOException ex) {
            System.out.println("Exception in Writing Junit testcase file" + "\n" + ex.getMessage());
        }
    }
}

From source file:com.hp.test.framework.jelly.ClickRadioBasedonTextLinkinTableTag.java

@Override
public void doTag(XMLOutput arg0) throws MissingAttributeException, JellyTagException {
    logger.info("Started Execution of ClickCheckboxBasedonTextLinkinTableTag");
    WebDriver driver = getSelenium();// w w  w . j a v a2s . c  om
    int loc = id.indexOf("=");
    String locator = id.substring(loc + 1);
    String bylocator = id.substring(0, loc);
    String findelementby = "";

    switch (bylocator) {
    case "id": {
        logger.info("Executing case id");
        try {
            WebElement table = driver.findElement(By.id(locator));
            findelementby = "WebElement table = driver.findElement(By.id(" + locator + "));\n";
            // Now get all the TR elements from the table 
            List<WebElement> allRows = table.findElements(By.tagName("tr"));

            // And iterate over them, getting the cells 
            for (WebElement row : allRows) {
                List<WebElement> cells = row.findElements(By.tagName("td"));

                // iterate each cell and match value
                for (WebElement cell : cells) {

                    if (cell.getText().equalsIgnoreCase(value)) {
                        System.out.println(cell.getText());
                        WebElement radio = row.findElement(By.xpath(".//td/input[@type='radio']"));
                        radio.click();
                        break;

                    }
                }

            }
        } catch (NoSuchElementException e) {
            System.out.println("Element not found" + "\n" + e.getMessage());

        }
    }
    case "name": {
        logger.info("Executing case name");
        try {
            WebElement table = driver.findElement(By.name(locator));
            findelementby = "WebElement table = driver.findElement(By.name(" + locator + "));\n";
            // Now get all the TR elements from the table 
            List<WebElement> allRows = table.findElements(By.tagName("tr"));

            // And iterate over them, getting the cells 
            for (WebElement row : allRows) {
                List<WebElement> cells = row.findElements(By.tagName("td"));

                // iterate each cell and match value
                for (WebElement cell : cells) {

                    if (cell.getText().equalsIgnoreCase(value)) {
                        System.out.println(cell.getText());
                        WebElement radio = row.findElement(By.xpath(".//td/input[@type='radio']"));
                        radio.click();
                        break;

                    }
                }

            }
        } catch (NoSuchElementException e) {
            System.out.println("Element not found" + "\n" + e.getMessage());

        }
    }
    case "xpath": {
        logger.info("Executing case xpath");
        try {
            WebElement table = driver.findElement(By.xpath(locator));
            findelementby = "WebElement table = driver.findElement(By.xpath(" + locator + "));\n";
            // Now get all the TR elements from the table 
            List<WebElement> allRows = table.findElements(By.tagName("tr"));

            // And iterate over them, getting the cells 
            for (WebElement row : allRows) {
                List<WebElement> cells = row.findElements(By.tagName("td"));

                // iterate each cell and match value
                for (WebElement cell : cells) {

                    if (cell.getText().equalsIgnoreCase(value)) {
                        System.out.println(cell.getText());
                        WebElement radio = row.findElement(By.xpath(".//td/input[@type='radio']"));
                        radio.click();
                        break;

                    }
                }

            }
        } catch (NoSuchElementException e) {
            System.out.println("Element not found" + "\n" + e.getMessage());

        }

    }

    case "css": {
        logger.info("Executing case css");
        try {
            WebElement table = driver.findElement(By.cssSelector(locator));
            findelementby = "WebElement table = driver.findElement(By.cssSelector(" + locator + "));\n";
            // Now get all the TR elements from the table 
            List<WebElement> allRows = table.findElements(By.tagName("tr"));

            // And iterate over them, getting the cells 
            for (WebElement row : allRows) {
                List<WebElement> cells = row.findElements(By.tagName("td"));

                // iterate each cell and match value
                for (WebElement cell : cells) {

                    if (cell.getText().equalsIgnoreCase(value)) {
                        System.out.println(cell.getText());
                        WebElement radio = row.findElement(By.xpath(".//td/input[@type='radio']"));
                        radio.click();
                        break;

                    }
                }

            }
        } catch (NoSuchElementException e) {
            System.out.println("Element not found" + "\n" + e.getMessage());

        }

    }

        logger.info("Completed Execution of ClickCheckboxBasedonTextLinkinTableTag");
    }

    String Command = "try { \n " + findelementby
            + " List<WebElement> allRows = table.findElements(By.tagName(\"tr\"));\n";
    Command = Command
            + "for (WebElement row : allRows) {\n  List<WebElement> cells = row.findElements(By.tagName(\"td\"));\n";
    Command = Command + "for (WebElement cell : cells) {\n ";
    Command = Command + "   if (cell.getText().equalsIgnoreCase(value)) {\n";
    Command = Command + "   System.out.println(cell.getText());\n";
    Command = Command + " WebElement radio = row.findElement(By.xpath(\".//td/input[@type='radio']\"));\n";
    Command = Command + " radio.click();\nbreak\n";
    Command = Command + "}\n}\n}\n";
    Command = Command
            + "} catch (NoSuchElementException e) { System.out.println(\"Element not found\" + e.getMessage());}";

    if (GenerateperformanceTests) {
        try {
            StartBrowserTag.JunitTestcase_fw.write(Command);
        } catch (IOException ex) {
            System.out.println("Exception in writing Junit testcase file" + "\n" + ex.getMessage());
        }
    }

}

From source file:com.htmlhifive.pitalium.core.model.SelectorType.java

License:Apache License

/**
 * ????????????/*w  ww .ja va 2 s  .c  o  m*/
 * 
 * @param element ?
 * @param selector 
 * @return ?????
 */
public List<WebElement> findElements(WebElement element, String selector) {
    return element.findElements(by(selector));
}

From source file:com.ibm.sbt.automation.core.environment.TestEnvironment.java

License:Open Source License

protected void handleOAuth10(BaseTest baseTest) {
    String loginFormId = baseTest.getProperty(PROP_OAUTH10_LOGINFORMID);
    String usernameId = baseTest.getProperty(PROP_OAUTH10_USERNAMEID);
    String passwordId = baseTest.getProperty(PROP_OAUTH10_PASSWORDID);
    String submitId = baseTest.getProperty(PROP_OAUTH10_SUBMITID);
    String username = baseTest.getProperty(PROP_OAUTH10_USERNAME);
    String password = baseTest.getProperty(PROP_OAUTH10_PASSWORD);

    WebElement loginForm = waitForLoginForm(loginTimeout, loginFormId, null, OAuth10LoginTitle, baseTest);
    if (baseTest.isResultsReady())
        return;//ww w.j  ava2  s  .  co  m
    if (loginForm != null) {
        WebElement continueButton = loginForm.findElement(By.id("continue"));

        WebElement usernameEl = loginForm.findElement(By.name(usernameId));
        WebElement passwordEl = loginForm.findElement(By.name(passwordId));
        WebElement submitEl = loginForm.findElements(By.id(submitId)).get(0);
        usernameEl.sendKeys(username);
        if (continueButton != null) {
            continueButton.click();
            WebDriverWait wait = new WebDriverWait(webDriver, 10);
            WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(submitId)));
        }
        passwordEl.sendKeys(password);
        submitEl.click();
    } else {
        fail("Unable to locate OAuth1.0 login form");
    }
}

From source file:com.ibm.sbt.automation.core.environment.TestEnvironment.java

License:Open Source License

protected void handleOAuth20(BaseTest baseTest) {
    String loginFormXPath = baseTest.getProperty(PROP_OAUTH20_LOGINFORMXPATH);
    String usernameXPath = baseTest.getProperty(PROP_OAUTH20_USERNAMEXPATH);
    String passwordXPath = baseTest.getProperty(PROP_OAUTH20_PASSWORDXPATH);
    String submitXPath = baseTest.getProperty(PROP_OAUTH20_SUBMITXPATH);
    String grantXPath = baseTest.getProperty(PROP_OAUTH20_GRANTXPATH);
    String username = baseTest.getProperty(PROP_OAUTH20_USERNAME);
    String password = baseTest.getProperty(PROP_OAUTH20_PASSWORD);

    WebElement loginForm = waitForLoginForm(loginTimeout, null, loginFormXPath, OAuth20LoginTitle, baseTest);
    if (baseTest.isResultsReady())
        return;// www  . jav  a  2 s. c o m

    if (loginForm != null) {
        WebElement usernameEl = loginForm.findElement(By.xpath(usernameXPath));
        WebElement passwordEl = loginForm.findElement(By.xpath(passwordXPath));
        WebElement submitEl = loginForm.findElements(By.xpath(submitXPath)).get(0);
        usernameEl.sendKeys(username);
        passwordEl.sendKeys(password);
        submitEl.click();

        // wait for authorization popup
        WebElement authPage = waitForPopup(loginTimeout, OAuth20AuthTitle);
        if (authPage != null) {
            WebElement grantEl = authPage.findElement(By.xpath(grantXPath));
            grantEl.click();
        } else {
            fail("Unable to locate OAuth2.0 authorization page");
        }
    } else {
        fail("Unable to locate OAuth2.0 login form");
    }
}

From source file:com.ibm.sbt.automation.core.environment.TestEnvironment.java

License:Open Source License

/**
 * Wait the specified interval for the specified web element to be available
 *///  w  w w  .  j av a  2 s . co  m
public WebElement waitForElement(final String match, final int secs, final String condition) {
    try {
        return (new WebDriverWait(getPageObject().getWebDriver(), secs))
                .until(new ExpectedCondition<WebElement>() {
                    @Override
                    public WebElement apply(WebDriver webDriver) {
                        failIfPageCrashed(webDriver);
                        webDriver = getPageObject(webDriver).getWebDriver();
                        if (condition.equalsIgnoreCase("id")) {
                            return webDriver.findElement(By.id(match));
                        } else if (condition.equalsIgnoreCase("linkText")) {
                            return webDriver.findElement(By.linkText(match));
                        } else if (condition.equalsIgnoreCase("tagName")) {
                            return webDriver.findElement(By.tagName(match));
                        } else if (condition.equalsIgnoreCase("name")) {
                            return webDriver.findElement(By.name(match));
                        } else if (condition.equalsIgnoreCase("idWithText")) {
                            WebElement element = webDriver.findElement(By.id(match));
                            String text = element.getText();
                            if (StringUtil.isNotEmpty(text)) {
                                return element;
                            }
                            String value = element.getAttribute("value");
                            if (StringUtil.isNotEmpty(value)) {
                                return element;
                            }
                            return null;
                        } else if (condition.equalsIgnoreCase("idWithChild")) {
                            WebElement element = webDriver.findElement(By.id(match));
                            List<WebElement> children = element.findElements(By.xpath("*"));
                            if (!children.isEmpty()) {
                                return element;
                            }
                            return null;
                        } else {
                            return webDriver.findElement(By.name(match));
                        }
                    }
                });
    } catch (Exception e) {
        return null;
    }
}

From source file:com.ibm.sbt.automation.core.environment.TestEnvironment.java

License:Open Source License

/**
 * Dump the specified result page to the trace log
 *///from www  .  j a va  2  s.c  om
public void dumpWebElement(WebElement webElement) {
    Trace.log(webElement + " tagName:" + webElement.getTagName() + " text:" + webElement.getText() + " id:"
            + webElement.getAttribute("id") + " displayed:" + webElement.isDisplayed());

    List<WebElement> webElements = webElement.findElements(By.xpath("*"));
    if (webElements.size() > 0) {
        Trace.log("Children size: " + webElements.size());
        for (int i = 0; i < webElements.size(); i++) {
            WebElement nextElement = webElements.get(i);
            Trace.log("[" + i + "]" + nextElement + " tagName:" + webElement.getTagName() + " text:"
                    + nextElement.getText() + " id:" + nextElement.getAttribute("id") + " displayed:"
                    + nextElement.isDisplayed());
        }
    }
}

From source file:com.ibm.sbt.automation.core.test.pageobjects.GridResultPage.java

License:Open Source License

/**
 * Return a list of tr WebElement for the Grid that was created on this page
 * /* ww w . j  a  v a  2 s . c  o m*/
 * @return {List<WebElement>}
 */
public List<WebElement> getTableRows() {
    WebElement resultEl = getWebElement();
    return resultEl.findElements(By.tagName("tr"));
}