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.vaadin.tests.testbenchapi.components.tabsheet.TabSheetElementTabWithoutCaptionIT.java

License:Apache License

private void checkIconTabOpen(int index) {
    List<WebElement> tabs = $(TabSheetElement.class).get(1)
            .findElements(By.className("v-tabsheet-tabitemcell"));
    boolean tabsOpen = false;
    for (int i = 0; i < tabs.size(); i++) {
        WebElement tab = tabs.get(i);
        boolean isOpened = tab.findElements(By.className("v-tabsheet-tabitem-selected")).size() > 0;
        if (isOpened) {
            tabsOpen = true;// www  .j a  v  a  2s.  c  om
            assertEquals("The wrong tab is open.", index, i);
        }
    }
    if (!tabsOpen) {
        fail("There are no tabs open, but tab with index " + index + " should be open.");
    }
}

From source file:com.vaadin.tutorial.addressbook.AddressbookIT.java

License:Apache License

@Test
public void tableWithData() {
    WebElement table = findElement(By.id("contactstable"));

    List<WebElement> headers = table.findElements(By.tagName("th"));
    Assert.assertEquals(5, headers.size());

    hasText(headers.get(0), "First Name");
    hasText(headers.get(1), "Last Name");
    hasText(headers.get(2), "Email");
    hasText(headers.get(3), "Created Timestamp");
    hasText(headers.get(4), "Do Not Call");

    List<WebElement> bodies = table.findElements(By.tagName("tbody"));
    List<WebElement> rows = bodies.get(bodies.size() - 1).findElements(By.tagName("tr"));
    List<Contact> contacts = ContactService.getDemoService().findAll("");
    int size = rows.size();
    Assert.assertTrue(size > 0);//from ww w .j a  va 2  s  .c o  m

    for (int i = 0; i < size; i++) {
        assertRowData(i, rows, contacts);
    }
}

From source file:com.vaadin.tutorial.addressbook.AddressbookIT.java

License:Apache License

private List<WebElement> getRows() {
    WebElement table = findElement(By.id("contactstable"));

    List<WebElement> bodies = table.findElements(By.tagName("tbody"));
    List<WebElement> rows = bodies.get(bodies.size() - 1).findElements(By.tagName("tr"));
    return rows;/*from  w  w  w .  jav a 2  s. c o  m*/
}

From source file:com.vaadin.v7.tests.components.grid.GridInitiallyHiddenColumnsTest.java

License:Apache License

/**
 * Returns the toggle inside the sidebar for hiding the column at the given
 * index, or null if not found./*from  w  w w .  jav  a2s.  c o m*/
 */
protected WebElement getColumnHidingToggle(GridElement grid, String caption) {
    WebElement sidebar = getSidebar(grid);
    List<WebElement> elements = sidebar.findElements(By.className("column-hiding-toggle"));
    for (WebElement e : elements) {
        if (caption.equalsIgnoreCase(e.getText())) {
            return e;
        }
    }
    return null;
}

From source file:com.vaadin.v7.tests.components.grid.GridResizeHiddenColumnTest.java

License:Apache License

@Test
public void testDragResizeHiddenColumnSize() {
    GridElement grid = $(GridElement.class).first();
    Actions action = new Actions(getDriver());

    // Check if column 'Gender' hidden
    List<GridCellElement> headerCells = grid.getHeaderCells(0);
    Assert.assertEquals("There should be two visible columns", 2, headerCells.size());
    Assert.assertFalse("Gender column should be hidden", containsText("Gender", headerCells));

    // Resize first column
    int dragOffset = -100;
    int headerCellWidth = headerCells.get(0).getSize().getWidth();
    dragResizeColumn(headerCells.get(0), 1, dragOffset);

    // When dragging the resizer on IE8, the final offset will be smaller
    // (might be an issue with the feature that doesn't start resizing until
    // the cursor moved a few pixels)
    double delta = BrowserUtil.isIE8(getDesiredCapabilities()) ? 5d : 0;
    Assert.assertEquals("Column width should've changed by " + dragOffset + "px", headerCellWidth + dragOffset,
            headerCells.get(0).getSize().getWidth(), delta);

    // Make column 'Gender' visible
    WebElement menuButton = grid.findElement(By.className("v-contextmenu")).findElement(By.tagName("button"));
    action.click(menuButton).perform(); // Click on menu button

    WebElement sidebarPopup = findElement(By.className("v-grid-sidebar-popup"));
    WebElement visibilityToggle = findElementByText("Gender",
            sidebarPopup.findElements(By.className("gwt-MenuItem")));
    action.click(visibilityToggle).perform(); // Click on 'Gender' menu item

    // Check if column 'Gender' is visible
    headerCells = grid.getHeaderCells(0);
    Assert.assertEquals("There should be three visible columns", 3, headerCells.size());
    Assert.assertTrue("Gender column should be visible", containsText("Gender", headerCells));

    // Check if column 'Gender' has expanded width
    int widthSum = 0;
    for (GridCellElement e : headerCells) {
        widthSum += e.getSize().getWidth();
    }//from   w  ww  . j ava2 s. co m
    Assert.assertEquals("Gender column should take up the remaining space",
            grid.getHeader().getSize().getWidth(), widthSum, 1d);
}

From source file:com.virtusa.isq.vtaf.runtime.SeleniumTestBase.java

License:Apache License

/**
 * Validate table row count function./*from  ww w .j av a2 s  .c o  m*/
 * 
 * @param element
 *            the element
 * @param tableName
 *            the Table name
 * @param expectedValue
 *            the expected value
 * @param fail
 *            the fail
 * @return the int
 */
private int validateTableRowCount(final WebElement element, final String tableName, final String expectedValue,
        final boolean fail, final Object[] customError) {
    /* TableName = TableName.replace("\"", "\\\""); */
    int rowCount = 0;
    try {
        /*
         * JS = "this.browserbot.findElement(\"" + TableName + "\")" +
         * ".rows.length.toString()";
         */
        rowCount = element.findElements(By.tagName("tr")).size();
        /* rowCount = selenium.getEval(JS); */

        if (rowCount == Integer.parseInt(expectedValue)) {
            reportresult(true, "CHECK TABLE :ROW COUNT", "PASSED",
                    "CHECK TABLE :ROW COUNT" + tableName + "| Input Value = " + expectedValue);

        } else {

            if (customError != null && !(customError[0].equals("null") || customError[0].equals(""))) {
                reportresult(true, "CHECK TABLE :ROW COUNT", "FAILED",
                        " Custom Error :" + generateCustomError(customError)
                                + " System generated Error : CHECK TABLE :ROW COUNT" + tableName
                                + "| Expected :" + expectedValue + " |Actual : " + rowCount);
                checkTrue(false, fail,
                        " Custom Error :" + generateCustomError(customError)
                                + " System generated Error : CHECK TABLE :ROW COUNT" + tableName
                                + "| Expected :" + expectedValue + " |Actual : " + rowCount);

            } else {

                reportresult(true, "CHECK TABLE :ROW COUNT", "FAILED", "CHECK TABLE :ROW COUNT" + tableName
                        + "| Expected :" + expectedValue + " |Actual : " + rowCount);
                checkTrue(false, fail, "CHECK TABLE [ROWCOUNT]  : Row count mismatch Expected:" + expectedValue
                        + " Actual:" + rowCount);
            }

        }

    } catch (Exception e) {
        e.printStackTrace();

        if (customError != null && !(customError[0].equals("null") || customError[0].equals(""))) {
            reportresult(fail, "CHECK TABLE :ROW COUNT :", "FAILED", " Custom Error :"
                    + generateCustomError(customError) + " System generated Error : " + e.getMessage());
            checkTrue(false, fail, " Custom Error :" + generateCustomError(customError)
                    + " System generated Error : " + e.getMessage());

        } else {
            reportresult(fail, "CHECK TABLE :ROW COUNT :", "FAILED", e.getMessage());
            checkTrue(false, fail, "CHECK TABLE [ROWCOUNT]  : " + e.getMessage());
        }

    }
    return rowCount;
}

From source file:com.virtusa.isq.vtaf.runtime.SeleniumTestBase.java

License:Apache License

/**
 * validate Table Column count function.
 * //  w  w  w .  j a v  a  2s  . com
 * @param element
 *            the element
 * @param tableName
 *            the Table name
 * @param expectedValue
 *            the expected value
 * @param fail
 *            the fail
 * @return the int
 */
private int validateTableColCount(final WebElement element, final String tableName, final String expectedValue,
        final boolean fail, final Object[] customError) {

    List<WebElement> rowElements = null;
    int actualdValue = 0;
    /* TableName = TableName.replace("\"", "\\\""); */
    try {
        /*
         * JS = "this.browserbot.findElement(\"" + TableName + "\")" +
         * ".rows[0].cells.length.toString()";
         */
        // String colCount = selenium.getEval(JS);
        rowElements = element.findElements(By.tagName("tr"));
        actualdValue = rowElements.get(1).findElements(By.tagName("td")).size();

        // actualdValue = selenium.getEval(JS);

        if (actualdValue == Integer.parseInt(expectedValue)) {
            reportresult(true, "CHECK TABLE :COLUMN COUNT ", "PASSED",
                    " CHECK TABLE COLUMN COUNT" + tableName + " Input Value = " + expectedValue);

        } else {

            if (customError != null && !(customError[0].equals("null") || customError[0].equals(""))) {

                reportresult(fail, "CHECK TABLE :COLUMN COUNT", "FAILED",
                        " Custom Error :" + generateCustomError(customError)
                                + " System generated Error : Expected :" + expectedValue + " |Actual : "
                                + actualdValue);
                checkTrue(false, fail,
                        " Custom Error :" + generateCustomError(customError)
                                + " System generated Error : Expected :" + expectedValue + " |Actual : "
                                + actualdValue);

            } else {

                reportresult(fail, "CHECK TABLE :COLUMN COUNT", "FAILED",
                        "Expected :" + expectedValue + " |Actual : " + actualdValue);
                checkTrue(false, fail, "checkTable [COLUMNCOUNT]  : Column count mismatch Expected:"
                        + expectedValue + " Actual:" + actualdValue);
            }

        }

    } catch (Exception e) {
        e.printStackTrace();
        reportresult(fail, "CHECK TABLE :COLUMN COUNT", "FAILED", e.getMessage());
        checkTrue(false, fail, e.getMessage());
    }
    return actualdValue;

}

From source file:com.virtusa.isq.vtaf.runtime.SeleniumTestBase.java

License:Apache License

/**
 * Reads the online table and load the contents to an arraylist.
 * //from   ww  w . j  ava 2 s. c o  m
 * @param element
 *            the element
 * @return the app table
 * @throws Exception
 *             the exception
 */
private ArrayList<String> getAppTable(final WebElement element) throws Exception {

    WebElement rowElement;
    List<WebElement> columnElements;
    List<WebElement> rowElements;

    ArrayList<String> htmlTable = new ArrayList<String>();

    /* locator = locator.replace("\"", "\\\""); */

    /*
     * JS = "this.browserbot.findElement(\"" + locator +
     * "\") .rows.length.toString()";
     * 
     * Integer rowNum = Integer.parseInt(selenium.getEval(JS));
     */

    rowElements = element.findElements(By.tagName("tr"));
    int rowNum = rowElements.size();

    if (rowNum > 0) {
        /*
         * JS = "this.browserbot.findElement(\"" + locator + "\")" +
         * ".rows[0].cells.length.toString()";
         * 
         * Integer colNum = Integer.parseInt(selenium.getEval(JS));
         */
        /* locator = locator.replace("\\\"", "\""); */
        String value = "";
        for (int i = 0; i < rowNum; i++) {

            rowElement = rowElements.get(i);

            columnElements = rowElement.findElements(By.tagName("td"));
            if (columnElements.isEmpty()) {
                columnElements = rowElement.findElements(By.tagName("th"));
            }

            int colNum = columnElements.size();

            for (int j = 0; j < colNum; j++) {

                value = columnElements.get(j).getText();
                // value = selenium.getTable(locator + "." + i + "." + j);

                if (value != null) {
                    htmlTable.add(value);
                } else {
                    htmlTable.add("");
                }
            }
        }
    } else {
        throw new Exception();

    }
    return htmlTable;
}

From source file:com.virtusa.isq.vtaf.runtime.SeleniumTestBase.java

License:Apache License

/**
 * Reads the online table and load the contents to an arraylist.
 * /*from www .  j av  a 2s .c  om*/
 * @param element
 *            the element
 * @param row
 *            the row
 * @return the app table row
 * @throws Exception
 *             the exception
 */
private ArrayList<String> getAppTableRow(final WebElement element, final int row) throws Exception {

    List<WebElement> rowElements;
    List<WebElement> colElements;
    WebElement rowElement;

    ArrayList<String> htmlTable = new ArrayList<String>();

    /* locator = locator.replace("\"", "\\\""); */

    /*
     * JS = "this.browserbot.findElement(\"" + locator + "\")" + ".rows[" +
     * row + "].cells.length.toString()"; Integer colNum =
     * Integer.parseInt(selenium.getEval(JS));
     */

    rowElements = element.findElements(By.tagName("tr"));
    rowElement = rowElements.get(row);
    colElements = rowElement.findElements(By.tagName("th"));
    colElements.addAll(rowElement.findElements(By.tagName("td")));
    int colNum = colElements.size();

    /* locator = locator.replace("\\\"", "\""); */
    String value = "";
    for (int j = 0; j < colNum; j++) {

        value = colElements.get(j).getText();
        /* value = selenium.getTable(locator + "." + row + "." + j); */

        if (value != null) {
            htmlTable.add(value);
        } else {
            htmlTable.add("");
        }
    }

    return htmlTable;
}

From source file:com.waku.mmdataextract.IMEI.java

License:Open Source License

private static void readForEachPage(FileWriter fw, WebDriver driver, String url) throws Exception {
    try {//from  ww w  . jav  a 2 s . c o m
        driver.get(url);
        WebElement table = driver.findElement(By.xpath("//table[@bgcolor='BFBEC3']"));
        List<WebElement> items = table.findElements(By.xpath("./tbody/tr"));
        for (int i = 1; i < items.size() - 1; i++) {
            List<WebElement> cols = items.get(i).findElements(By.tagName("td"));
            for (WebElement col : cols) {
                System.out.print(col.getText() + ",");
                fw.write(col.getText() + ",");
            }
            System.out.println();
            fw.write("\n");
        }
    } catch (Exception e) {
        System.out.println("---------Retry--------------------------");
        System.out.println(e.getMessage());
        System.out.println("---------Retry---------------------------");
        readForEachPage(fw, driver, url);
    }
}