表格的定位

2017-02-16  本文已影响0人  默默的joy

表格的定位跟普遍元素的定位会有点差别,特此mark下
测试的页面代码如下:

<html>
<head>
    <title>TableTest</title>
</head>
<body>
    <div>
    <table width="600" height="100" border="1" cellpadding="0" cellspacing="0" class="tb">
     <tr>
       <td>User Name</td>
       <td>E-mail</td>
       <td>Access</td>
     </tr>
      <tr>
       <td>test1</td>
       <td>
            <a href="test1@qq.com">test1@qq.com</a>
       </td>
       <td>
            <div>
                <label for="user128_admin">Admin</label>
                <input type="checkbox" id="user128_admin" checked="true"/>
                <label for="user128_cm">Content Manager</label>
                <input type="checkbox" id="user128_cm"/>
                <label for="user128_browser">Browser</label>
                <input type="checkbox" id="user128_browser"/>
            </div>
       </td>
     </tr>
      <tr>
       <td>test2</td>
       <td>
            <a href="test1@qq.com">test2@qq.com</a>
       </td>
       <td>
            <div>
                <label for="user128_admin">Admin</label>
                <input type="checkbox" id="user128_admin" checked="true"/>
                <label for="user128_cm">Content Manager</label>
                <input type="checkbox" id="user128_cm"/>
                <label for="user128_browser">Browser</label>
                <input type="checkbox" id="user128_browser"/>
            </div>
       </td>
     </tr>
    </table>
    </div>
</body>
</html>

1.定位表格中的行和列

    @Test
    public void tableTest() throws InterruptedException {

//        定位到整个表格
        WebElement table = driver.findElement(By.className("tb"));
//        获取表格中的所有行
        List<WebElement> rows = table.findElements(By.tagName("tr"));
        Assert.assertEquals(3,rows.size());
        for (WebElement row : rows){
//            获取某一行中的所有列
            List<WebElement> cols = row.findElements(By.tagName("td"));
            for (WebElement col:cols){
                System.out.print(col.getText()+" ");
            }
            System.out.println();
        }
    }

当然也可以直接利用xpath或是css直接定位到某个单元格

    @Test
    public void tableTest() throws InterruptedException {
        WebElement cell00 = driver.findElement(By.xpath("html/body/div[1]/table/tbody/tr[1]/td[1]"));
        System.out.print(cell00.getText());
    }

2.将处理table的操作封装成一个方法,以后就可以直接调用方法来做,比较方便
构造一个类WebTable,具体内容如下:

package Table;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.util.List;

/**
 * Created by Administrator on 2016/12/16.
 */
public class WebTable {
    private WebElement webTable;

    public WebTable(WebElement webTable){
        setWebTable(webTable);
    }

    public WebElement getWebTable() {
        return webTable;
    }

    public void setWebTable(WebElement webTable) {
        this.webTable = webTable;
    }

//    获取行数
    public int getRowCount(){
        List<WebElement> tableRows = webTable.findElements(By.tagName("tr"));
        return tableRows.size();
    }

//    获取列数
    public int getColumnCount(){
        List<WebElement> tableRows = webTable.findElements(By.tagName("tr"));
        WebElement headerRow = tableRows.get(0);
        List<WebElement> tableCols = headerRow.findElements(By.tagName("td"));
        return tableCols.size();
    }

//    获取某个单元格,单元格中有多个可编辑的地方
    public WebElement getCellEditor(int rowIdx, int colIdx, int editorIdx) {
        List<WebElement> tableRows = webTable.findElements(By.tagName("tr"));
        WebElement currentRow = tableRows.get(rowIdx - 1);
        List<WebElement> tableCols = currentRow.findElements(By.tagName("td"));
        WebElement cell = tableCols.get(colIdx - 1);
        WebElement cellEditor = cell.findElements(By.tagName("input")).get(editorIdx-1);
        return cellEditor;
    }

    //    获取某个可以编辑的单元格
    public WebElement getCellEditor(int rowIdx, int colIdx) {
        List<WebElement> tableRows = webTable.findElements(By.tagName("tr"));
        WebElement currentRow = tableRows.get(rowIdx - 1);
        List<WebElement> tableCols = currentRow.findElements(By.tagName("td"));
        WebElement cell = tableCols.get(colIdx - 1);
        WebElement cellEditor = cell.findElements(By.tagName("input")).get(0);
        return cellEditor;
}
}

创建一个测试类进行测试:

package table;

import Table.WebTable;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.List;

/**
 * Created by Administrator on 2016/12/15.
 */
public class TableTest {
    WebDriver driver;

    @BeforeClass
    public void openBrowser(){
        driver = new FirefoxDriver();
        driver.get("file:///C:/Users/Administrator/Desktop/tabletest.html");

    }

    @Test
    public void tableTest() throws InterruptedException {
        WebTable webTable = new WebTable(driver.findElement(By.className("tb")));
//        计算行数
        int rows = webTable.getRowCount();
        System.out.println(rows);
//        计算列数
        int cols = webTable.getColumnCount();
        System.out.println(cols);
//        点击第二行第三列的第2个复选框
        webTable.getCellEditor(2,3,2).click();
        Thread.sleep(5000);

    }

    @AfterClass
    public void closeBrowser(){
        driver.quit();
    }

}
上一篇 下一篇

猜你喜欢

热点阅读