table类封装了各种表格操作方法。
被测试的html代码:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <table width="400" border="1" id="table"> <tr> <td align="left"><p>第一行第一列</p> <input type="text"></input></td> <td align="left"><p>第一行第二列</p> <input type="text"></input></td> <td align="left"><p>第一行第三列</p> <input type="text"></input></td> </tr> <tr> <td align="left"><p>第二行第一列</p> <input type="text"></input></td> <td align="left"><p>第二行第二列</p> <input type="text"></input></td> <td align="left"><p>第二行第三列</p> <input type="text"></input></td> </tr> <tr> <td align="left"><p>第三行第一列</p> <input type="text"></input></td> <td align="left"><p>第三行第二列</p> <input type="text"></input></td> <td align="left"><p>第三行第三列</p> <input type="text"></input></td> </tr> </table> </body> </html>
工具类Table类:
package cn.om.TestTable; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebElement; public class Table { // 声明一个WebEelment对象,用于存储页面的表格元素对象 private WebElement _table; // 为构造函数传入页面表格元素对象参数,调用table的set_table方法,将页面表格元素复制给_table public Table(WebElement table) { set_table(table); } public WebElement get_table() { return _table; } public void set_table(WebElement _table) { this._table = _table; } // 返回表格的行数 public int getRowCount() { List<WebElement> tableRow = _table.findElements(By.tagName("tr")); return tableRow.size(); } public int getColumnCount() { List<WebElement> tableRows = _table.findElements(By.tagName("tr")); return tableRows.get(0).findElements(By.tagName("td")).size(); } // 获取表格中某行某列的单元格对象 public WebElement getCell(int rowNo, int colNo) { try { List<WebElement> tableRows = _table.findElements(By.tagName("tr")); System.out.println("行总数:" + tableRows.size()); System.out.println("行号:" + rowNo); List<WebElement> tableColumns = tableRows.get(rowNo - 1).findElements(By.tagName("td")); System.out.println("列总数:" + tableColumns.size()); System.out.println("列号:" + colNo); return tableColumns.get(colNo - 1); } catch (NoSuchElementException e) { // TODO: handle exception throw new NoSuchElementException("没有找到相关的元素"); } } // 获取表格中某行某列的单元格中的某个页面元素对象,by参数用于定位某个表 public WebElement getWebElementInCell(int rowNo, int colNo, By by) { try { List<WebElement> tableRows = _table.findElements(By.tagName("tr")); WebElement currentRow = tableRows.get(rowNo - 1); List<WebElement> tablecols = currentRow.findElements(By.tagName("td")); WebElement cell = tablecols.get(colNo - 1); return cell.findElement(by); } catch (NoSuchElementException e) { // TODO: handle exception throw new NoSuchElementException("没有找到相关的元素"); } } }
对table类进行测试:
package cn.om.TestTable; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; 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.AfterMethod; public class TestTableDemo { WebDriver driver; String url; @Test public void testTable() { WebElement webTable=driver.findElement(By.tagName("table")); Table table=new Table(webTable); WebElement cell=table.getCell(3, 2); Assert.assertEquals(cell.getText(), "第三行第二列"); WebElement cellInut=table.getWebElementInCell(3, 2, By.tagName("input")); cellInut.sendKeys("第三行的第二列表格被找到"); } @BeforeMethod public void beforeMethod() { url = "file:///F:/workspace/WebDriver%20API/src/cn/gloryroad/TestTable/table.html"; System.setProperty("webdriver.firefox.bin", "D:/Mozilla Firefox/firefox.exe"); driver = new FirefoxDriver(); } @AfterMethod public void afterMethod() { driver.quit(); } }