package com.gloryroad.testcase;
import java.util.List;
import java.util.NoSuchElementException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
public class Table {
private WebElement _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> tableRows=_table.findElements(By.tagName("tr"));
return tableRows.size();
}
/**
* 获取列数
* @return
*/
public int getColumnCount(){
List<WebElement> tableRows=_table.findElements(By.tagName("tr"));
return tableRows.get(0).findElements(By.tagName("td")).size();
}
/**
* 获取表格中的某个单元格
* @param row
* @param cols
* @return
*/
public WebElement getCell(int row,int cols){
try {
List<WebElement> tableRows=_table.findElements(By.tagName("tr"));
System.out.println("行总数:"+tableRows.size());
System.out.println("行号:"+row);
WebElement currentRow=tableRows.get(row-1);
List<WebElement> tableCols=currentRow.findElements(By.tagName("td"));
System.out.println("列总数:"+tableCols.size());
WebElement cell=tableCols.get(cols-1);
System.out.println("列号:"+cols);
return cell;
} catch (Exception e) {
// TODO: handle exception
throw new NoSuchElementException("么有找到相关元素");
}
}
public WebElement getWebElementInCell(int row, int cols,By by){
WebElement cell = null;
try {
List<WebElement> tableRows=_table.findElements(By.tagName("tr"));
System.out.println("行总数:"+tableRows.size());
System.out.println("行号:"+row);
WebElement currentRow=tableRows.get(row-1);
List<WebElement> tableCols=currentRow.findElements(By.tagName("td"));
System.out.println("列总数:"+tableCols.size());
cell=tableCols.get(cols-1);
System.out.println("列号:"+cols);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return cell.findElement(by);
}
}