我在Eclipse中使用Selenium Web Driver和IUnit。我有从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
,它是数组,并且必须与ReadExcel类相等path_name
。其实我想把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();
}
同样的效果。它没有点击链接
答案 0 :(得分:0)
一种方法是在ReadExcel类中为要访问的变量实现setter()
和getter()
方法。他们显然是公共方法。
修改强>
猜测你试图更新和我的理解,你做了很多错事。假设您正在调用另一个类中的最后一段代码,这是您真正应该做的事情:
此外,我假设你已经修改了ReadExcel
课程,看起来像这样
public class ReadExcel {
ArrayList<String> pathName = new ArrayList<String>();
ArrayList<String> fieldKey = new ArrayList<String>();
ArrayList<String> fieldName = new ArrayList<String>();
ArrayList<String> windowNew = new ArrayList<String>();
ArrayList<String> link = new ArrayList<String>();
public ReadExcel() {
pathName = new ArrayList<String>();
fieldKey = new ArrayList<String>();
fieldName = new ArrayList<String>();
windowNew = new ArrayList<String>();
link = new ArrayList<String>();
}
/**
* Not so sure of this method name. But make sure that this method is called before
* you try to call getXX() methods
*/
public void mai() {
String filename = "E:/data.xls";
if(fileName == null || "".equals(fileName))
return;
HSSFWorkbook workBook = null;
FileInputStream fileInputStream = null;
HSSFSheet sheet;
HSSFRow row;
int rows;
try{
fileInputStream = new FileInputStream(new File(fileName));
workBook = new HSSFWorkbook(fileInputStream);
for(int sheetIndex = 0; sheetIndex < workBook.getNumberOfSheets(); sheetIndex++){
sheet = workBook.getSheetAt(sheetIndex);
rows = sheet.getPhysicalNumberOfRows();
for(int rowIndex = 0; rowIndex < rows; rowIndex++){
/**
* Update with your own logic for retrieval
*/
row = sheet.getRow(rowIndex);
if(row.getPhysicalNumberOfCells() < 6)
continue;
pathName.add(row.getCell(0).getStringCellValue());
fieldKey.add(row.getCell(0).getStringCellValue());
fieldName.add(row.getCell(0).getStringCellValue());
windowNew.add(row.getCell(0).getStringCellValue());
link.add(row.getCell(0).getStringCellValue());
}
}
} catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
} catch (IOException ioException) {
ioException.printStackTrace();
}finally{
if(fileInputStream != null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
workBook = null;
}
}
}
/**
* The getter/setter methods for the variables
*/
public ArrayList<String> getPathName() {
return pathName;
}
public void setPathName(ArrayList<String> pathName) {
this.pathName = pathName;
}
public ArrayList<String> getFieldKey() {
return fieldKey;
}
public void setFieldKey(ArrayList<String> fieldKey) {
this.fieldKey = fieldKey;
}
public ArrayList<String> getFieldName() {
return fieldName;
}
public void setFieldName(ArrayList<String> fieldName) {
this.fieldName = fieldName;
}
public ArrayList<String> getWindowNew() {
return windowNew;
}
public void setWindowNew(ArrayList<String> windowNew) {
this.windowNew = windowNew;
}
public ArrayList<String> getLink() {
return link;
}
public void setLink(ArrayList<String> link) {
this.link = link;
}
}
我希望您在某个地方调用mai()
方法(此名称听起来很奇怪,但)从excel检索数据并将其存储在{{1}中在你试图调用以下代码之前:
ArrayList
您的代码的一些指示:
使用泛型。而不是使用ReadExcel readExcel = new ReadExcel();
ArrayList<String> path_name = readExcel.getPath_name();
for(String pathName: path_name){
driver.findElement(By.xpath(pathName).click();
}
ArrayList pathNameList
在编码时使用Java命名约定看起来不错。其中之一是使用混合大小写字母组合方法名称,以小写字母开头,并以大写字母开始每个后续单词。因此,不要使用ArrayList<String> pathNameList
,而应考虑使用getPath_name()
甚至getPathName()
(,尽管我们大多数人更喜欢第一个)。 Here's a link that can help you with that.
答案 1 :(得分:0)
您可以将变量转换为该类的字段吗?然后在那里添加一个方法,将该字段的值返回给任何感兴趣的人。