我在带有 IUnit 的 Eclipse 中使用 Selenium Web 驱动程序。我有从 excel 文件中读取数据的代码。每列都显示为数组。这是代码:
class ReadExcel {
ArrayList path_name = new ArrayList();
ArrayList field_key = new ArrayList();
ArrayList field_name = new ArrayList();
ArrayList window_new = new ArrayList();
ArrayList link = new ArrayList();
lov_name = new ArrayList();
public void mai() {
int i = 0;
String path_namel = "", field_keyl = "", field_namel = "", window_newl = "", linkl = "", lov_namel = "";
String filename = "E:/data.xls";
if (filename != null && !filename.equals("")) {
FileInputStream fs = new FileInputStream(filename);
HSSFWorkbook wb = new HSSFWorkbook(fs);
for (int k = 0; k < wb.getNumberOfSheets(); k++) {
int j = i + 1;
HSSFSheet sheet = wb.getSheetAt(k);
int rows = sheet.getPhysicalNumberOfRows();
for (int r = 1; r < rows; r++) {
HSSFRow row = sheet.getRow(r);
int cells = row.getPhysicalNumberOfCells();
HSSFCell cell1 = row.getCell(0);
path_namel = cell1.getStringCellValue();
HSSFCell cell2 = row.getCell(1);
field_keyl = cell2.getStringCellValue();
HSSFCell cell3 = row.getCell(2);
field_namel = cell3.getStringCellValue();
HSSFCell cell4 = row.getCell(3);
window_newl = cell4.getStringCellValue();
HSSFCell cell5 = row.getCell(4);
linkl = cell5.getStringCellValue();
HSSFCell cell6 = row.getCell(5);
lov_namel = cell6.getStringCellValue();
path_name.add(path_namel);
field_key.add(field_keyl);
field_name.add(linkl);
window_new.add(window_newl);
link.add(linkl);
lov_name.add(lov_namel);
}
i++;
}
}
}
}
在我的硒测试中,我有这样的循环:
for (int i=0; i<path_name.length; i++){
driver.findElement(By.xpath(path_name[i])).click();
}
在这里,我使用数组变量path_name
,它必须path_name
与 ReadExcel 类相等。实际上,我想将 excel 中的这些值用作数组。我应该如何从 ReadExcel 调用变量?
编辑 我尝试使用 getter 和 setter 方法。
int q;
String g;
public String getG() {
return g;}
public void setG(String g) {
this.g = g;}
public int getQ() {
return q;}
public void setQ(int q) {
this.q = q;}
q=path_name.size();
g=path_name.get(i).toString();
我的测试我以这种方式调用变量
ReadExcel h = new ReadExcel();
String k= h.getG();
ReadExcel p = new ReadExcel();
int n= p.getQ();
for (int j=0; j<n; j++){
driver.findElement(By.xpath(k)).click();}
编辑器中没有错误,但循环不起作用。它应该点击链接(k),但没有效果。我也试试这个(在第一个答案中建议)
ReadExcel readExcel = new ReadExcel();
ArrayList<String> path_name = readExcel.getPath_name();
for(String pathName: path_name){
driver.findElement(By.xpath(pathName)).click();
}
同样的效果。它不会点击链接