The table class encapsulates various table manipulation methods.
Tested html code:
<!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>First row, first column</p> <input type="text"></input></td> <td align="left"><p>First row second column</p> <input type="text"></input></td> <td align="left"><p>First row third column</p> <input type="text"></input></td> </tr> <tr> <td align="left"><p>2nd row 1st column</p> <input type="text"></input></td> <td align="left"><p>2nd row 2nd column</p> <input type="text"></input></td> <td align="left"><p>2nd row 3rd column</p> <input type="text"></input></td> </tr> <tr> <td align="left"><p>third row first column</p> <input type="text"></input></td> <td align="left"><p>third row second column</p> <input type="text"></input></td> <td align="left"><p>3rd row 3rd column</p> <input type="text"></input></td> </tr> </table> </body> </html>
Tool class Table class:
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 { // Declare a WebEelment object to store the table element object of the page private WebElement _table; // Pass in the page table element object parameter for the constructor, call the table's set_table method, and copy the page table element to _table public Table(WebElement table) { set_table(table); } public WebElement get_table() { return _table; } public void set_table(WebElement _table) { this._table = _table; } // return the number of rows in the 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(); } // Get the cell object of a row and column in the table public WebElement getCell(int rowNo, int colNo) { try { List<WebElement> tableRows = _table.findElements(By.tagName("tr")); System.out.println("Total number of rows: " + tableRows.size()); System.out.println("行号:" + rowNo); List<WebElement> tableColumns = tableRows.get(rowNo - 1).findElements(By.tagName("td")); System.out.println("Total number of columns: " + tableColumns.size()); System.out.println("列号:" + colNo); return tableColumns.get(colNo - 1); } catch (NoSuchElementException e) { // TODO: handle exception throw new NoSuchElementException("No related element found"); } } // Get a page element object in a cell of a row and column in the table, the by parameter is used to locate a table 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("No related element found"); } } }
Test the table class:
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(), "third row second column"); WebElement cellInut=table.getWebElementInCell(3, 2, By.tagName("input")); cellInut.sendKeys("The second column of the third row was found"); } @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(); } }