selenium webdriver 学习总结 - 自定义Table操作类(十一)

前言:

WebDriver中没有关于Table的相关操作类,下面用代码直接展示如何自定义Table操作类
定义两个类,
Point.java
Table.java

Point.java
中定义了表格中获取某个单元格时行列坐标,以此来唯一标识指定的单元格
Point中定义两个属性{rowNo,colNo},分别对应指定单元格的{行,列},索引起始均为0 ,如{0,0}表示第0行,第0列的单元格;

Table.java
该类中方法包含{取指定行元素(private),取所有行元素(private),取指定行中所有Cell,取指定行和列Cell,取指定列中所有Cell}
取指定行元素(private) --> private WebElement getRow(Point point)
该方法用于获取 指定行对应的WebElement实例,point中属性{rowNo,colNo}初始化rowNo即可,将colNo置为null , or anyelse
取所有行元素(private) --> private List<WebElement> getAllRows()
该方法用于获取表格中所有行对应的WebElement实例集合
取指定行中所有Cell --> public List<WebElement> getCellsOfRow(Point point)
该方法用于获取表格中指定行中所有单元格对应的WebElement实例集合,point中属性{rowNo,colNo}初始化rowNo即可
取指定行和列Cell --> public WebElement getCellOfRow(Point point)
该方法用于获取表格中指定行、列的单元格
取指定列中所有Cell --> public List<WebElement> getCellsOfCol(Point point)
该方法用于获取表格中指定所在列的所有单元格的集合,注:当某行不存在该列时该方法会将该单元格元素置为null添加到List数组中
将上一个方法(public List<WebElement> getCellsOfCol(Point point))得到的List数组中为null的元素移除,public List<WebElement> TrimCellsOfCol

代码如下:

package demo.table;
public class Point {
	private Integer rowNo;
	private Integer colNo;
	public Point(){}
	public Point(Integer rowNo, Integer colNo) {
		super();
		this.rowNo = rowNo;
		this.colNo = colNo;
	}
	
	public int getRowNo() {
		return rowNo;
	}
	
	public void setRowNo(Integer rowNo) {
		this.rowNo = rowNo;
	}
	
	public int getColNo() {
		return colNo;
	}
	
	public void setColNo(Integer colNo) {
		this.colNo = colNo;
	}
	
	@Override
	public String toString() {
		return "Point [rowNo=" + rowNo + ", colNo=" + colNo + "]";
	}
}
package demo.table;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import com.jd.acceptance.page.Page;
import com.jd.acceptance.vo.Point;
/**
 * @author xuleilei
 * Table元素处理工具:
 * {取指定行元素(private),取所有行元素(private),取指定行中所有Cell,取指定行和列Cell,取指定列中所有Cell}
 */
public class Table {
	private By by;
	private WebElement table;
	public Table(By by) {
		super();
		this.by = by;
		this.table = Page.getElement(this.by);
	}
/**
 * 取指定行元素
 * @param point
 * point对象中指定行(rowNo)属性
 * @return
 * 指定行所对应的WebElement对象
 */
	private WebElement getRow(Point point){
		WebElement target = null;
		List<WebElement> allRows = this.getAllRows();
		try{
			target = allRows.get(point.getRowNo());
		}catch(IndexOutOfBoundsException e){
			throw new MyException(">>>rowNo out of range : rowNo should be [rowNo >= 0 || rowNo < "+allRows.size()+"]");
		}
		return target;
	}    
	
/**
 * 取所有行元素
 * @return
 * List<WebElement>返回表格中所有行元素
 */
	private List<WebElement> getAllRows(){
		List<WebElement> allRows = null;
		allRows = this.table.findElements(By.tagName("tr"));
		return allRows;
	}
/**
 * 取指定行中所有Cell
 * @param point
 * point对象中指定行(rowNo)属性
 * @return
 * 返回指定行中所有单元格元素
 */
	public List<WebElement> getCellsOfRow(Point point){
		List<WebElement> cells = null;
		WebElement row = this.getRow(point);
		cells = row.findElements(By.tagName("th"));
		if(cells.size()>0){
			return cells;
		}
		cells = row.findElements(By.tagName("td"));
		if(cells.size()>0){
			return cells;
		}
		return null;
	}
/**
 * 取指定行和列Cell
 * @param point
 * point对象中指定行(rowNo)和列(colNo)
 * @return
 * 返回指定行和列对象的WebElement对象
 */
	public WebElement getCellOfRow(Point point){
 		List<WebElement> cells = null;  
		WebElement Row = this.getRow(point);
		WebElement target = null;  
		int cell = point.getColNo();
		//分开处理列里面"<th>""<td>"两种标签
		cells = Row.findElements(By.tagName("th"));
		if(cells.size() == 0){
			cells = Row.findElements(By.tagName("td"));
		}
		
		if(cells.size() == 0){
			return null;
		}
		
		try{
			target = cells.get(cell);  
		}catch(IndexOutOfBoundsException e){
			throw new MyException(">>>colNo out of range : colNo should be [colNo >= 0 || colNo < "+cells.size()+"]");
		}
		
		return target; 
}
/**
 * 取指定列中所有Cell
 * @param point
 * point对象中指定列(colNo)
 * @return
 *返回指定列中所有单元格元素,该方法会将不存在的列所对应的单元格元素置为null,添加到List结果中 
 */
	public List<WebElement> getCellsOfCol(Point point){
		int colNo = point.getColNo();
		if(colNo >= this.maxLength()){
			throw new MyException(">>>colNo should be less than the max number("+this.maxLength()+") of any row's column amount in this table");
	}
	List<WebElement> cells = new ArrayList<WebElement>();
	List<WebElement> tmpCells = null;
	Iterator<WebElement> allRows = this.getAllRows().iterator();
	WebElement row = null;
	while(allRows.hasNext()){
		row = allRows.next();
		try{
			tmpCells = row.findElements(By.tagName("th"));
			if(tmpCells.size() == 0){
				tmpCells = row.findElements(By.tagName("td"));
			}
			
			if(tmpCells.size()>0){
				if(colNo < tmpCells.size()){
					cells.add(tmpCells.get(colNo));
				}else{
					cells.add(null);
				}
			}else    //当tr中不存在td  th 时,添加null
				cells.add(null);
			}catch(IndexOutOfBoundsException e){
     			throw new MyException(">>>colNo out of range : \n"
+ "colNo should be [colNo >= 0 || colNo < "+tmpCells.size()+"]");
			}
		}
		return cells;
	}
/**
 * 获取表格中行的最大宽度,即行的最大单元格个数
 * @return
 */
	private int maxLength(){
		int max = 0;
		int arrLength = this.getAllRows().size();
		Iterator<WebElement> allRows = this.getAllRows().iterator();
		int maxArr[] = new int[arrLength];
		WebElement row = null;
		List<WebElement> tmpCells = null;
		int i = 0;
		while(allRows.hasNext()){
			row = allRows.next();
			if(i == 0){
				tmpCells = row.findElements(By.tagName("th"));
				if(tmpCells.size() == 0){
					tmpCells = row.findElements(By.tagName("td"));
				}
			}else{
				tmpCells = row.findElements(By.tagName("td"));
			}
		maxArr[i] = tmpCells.size();
		i++;
		}
		Arrays.sort(maxArr);
		max = maxArr[arrLength-1];
		return max;
	}
/**
 * 将指定列中所有单元格
 * @param cells
 * @return
 */
	@SuppressWarnings("null")
	public List<WebElement> TrimCellsOfCol(List<WebElement> cells){
		List<WebElement> tmp = null;
		Iterator<WebElement> cellIter = cells.iterator();
		while(cellIter.hasNext()){
			WebElement e = cellIter.next();
			if(e != null){
				tmp.add(e);
			}
		}
		return tmp;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值