表の配置

5816 ワード

表の位置付けは一般的な要素の位置付けと少し違います.ここでmarkでテストしたページコードは以下の通りです.


    TableTest


    
User Name E-mail Access
test1 [email protected]
test2 [email protected]

1.表の行と列の配置
    @Test
    public void tableTest() throws InterruptedException {

//         
        WebElement table = driver.findElement(By.className("tb"));
//         
        List rows = table.findElements(By.tagName("tr"));
        Assert.assertEquals(3,rows.size());
        for (WebElement row : rows){
//             
            List 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を処理する操作を一つの方法にカプセル化して、後で直接方法を呼び出すことができて、比較的に便利に1つの類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 tableRows = webTable.findElements(By.tagName("tr"));
        return tableRows.size();
    }

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

//     , 
    public WebElement getCellEditor(int rowIdx, int colIdx, int editorIdx) {
        List tableRows = webTable.findElements(By.tagName("tr"));
        WebElement currentRow = tableRows.get(rowIdx - 1);
        List 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 tableRows = webTable.findElements(By.tagName("tr"));
        WebElement currentRow = tableRows.get(rowIdx - 1);
        List 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();
    }

}