Selenium如何处理Table

由于webdriver中没有专门的table类,所以我们需要简单的封装出一个易用易扩展的Table类来帮助简化代码。

以下是我之前用Java语言来实现的一个简单的封装:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

/**
* <b>Name:</b> WebTable</br> <b>Description: </b>This class is used to handling
* web table</br>
*
* @author <i>Yongfei Hu</i>
*/

public class WebTable {

private WebElement webTable;
StartingSelenium ss = new StartingSelenium();

public WebTable(WebElement webTable) {
this.webTable = webTable;
}

public WebElement getWebTable() {
return webTable;
}

/**
* <b>Name: getTableRows</b> <b>Description:</b> This method is to get table
* rows list
*
* @return tableRows
*/
private List<WebElement> getTableRows() {
List<WebElement> tableRows = webTable.findElements(By.tagName("tr"));
return tableRows;
}

/**
* <b>Name: getTableColumns</b> <b>Description:</b> This method is to get
* table columns list
*
* @param currentRow
*
* @return tableColumns
*/
private List<WebElement> getTableColumns(WebElement currentRow) {
List<WebElement> tableColumns = currentRow.findElements(By
.tagName("td"));
return tableColumns;
}

/**
* <b>Name: getRowCount</b> <b>Description:</b> This method is to get the
* table row count
*
* @return rowCount
*/
public int getRowCount() {
int rowCount = getTableRows().size();
Reporter.log("Rows in the table: " + rowCount, true);
return rowCount;
}

/**
* <b>Name: getColumnCount</b> <b>Description:</b> This method is to get the
* table column count
*
* @return columnCount
*/
public int getColumnCount() {
WebElement headerRow = getTableRows().get(0);
List<WebElement> tableColumns = getTableColumns(headerRow);
int columnCount = tableColumns.size();
Reporter.log("Column in the table: " + columnCount, true);
return columnCount;
}

/**
* <b>Name: getCellText</b> <b>Description: </b> This method is to get the
* specific cell data
*
* @param row
* @param column
* @return cellText
*/
public String getCellText(int row, int column) {
WebElement currentRow = getTableRows().get(row);
List<WebElement> tableColumns = getTableColumns(currentRow);
return tableColumns.get(column).getText();
}

/**
* <b>Name: getRowByCellText</b> <b>Description:</b> This method is to get
* the row number by the cell data
*
* @param cellText
*
* @return rowNumber
*/
public int getRowByCellText(String cellText) {
int rowCount = getRowCount();
int colCount = getColumnCount();
int getRow = 0;
boolean flag;

for (int i = 0; i < rowCount; i++) {
flag = false;

for (int j = 0; j < colCount; j++) {
if (getCellText(i, j).equals(cellText)) {
getRow = i;
flag = true;
Reporter.log("The text: " + cellText + " in row number: "
+ getRow, true);
break;
}
}
if (flag) {
break;
}
}
return getRow;
}

/**
* <b>Name: clickLinkInCell</b> <b>Description: </b> This method is to click

* link in the specific cell
*
* @param row
* @param column
* @param index
*
*/
public void clickLinkInCell(int row, int column, int index) {
WebElement currentRow = getTableRows().get(row);
WebElement currentCell = getTableColumns(currentRow).get(column);
currentCell.findElements(By.tagName("a")).get(index).click();
Reporter.log("Clickin a link on the row: " + row, true);
}

/**
* <b>Name: clickLinkInCell</b> <b>Description: </b> This method is to click
* link in the specific cell
*
* @param row
* @param column
* @param index
* @param linkName
*
*/
public void clickLinkInCell(int row, int column, int index, String linkName) {
WebElement currentRow = getTableRows().get(row);
WebElement currentCell = getTableColumns(currentRow).get(column);
WebElement we = currentCell.findElements(By.tagName("a")).get(index);
ss.clickUsingJS(we, linkName);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!