请帮助我使用Selenium和Java代码从同一个excel文件的多个表中读取数据的代码。请在下面找到我尝试的代码。它总是从最后一页读取数据。但我需要从表格0到表格n读取数据。
public class HybridExecuteTest {
WebDriver webdriver = null;
@Test(dataProvider="hybridData")
public void testLogin(String testcaseName,String keyword,String objectName,String objectType,String value)
throws Exception {
if(testcaseName!=null&&testcaseName.length()!=0){
webdriver=new FirefoxDriver();
webdriver.manage().window().maximize();
}
ReadObject object = new ReadObject();
Properties allObjects = object.getObjectRepository();
UIOperation operation = new UIOperation(webdriver);
//Call perform function to perform operation on UI
operation.perform(allObjects,keyword,objectName,objectType,value);
}
@SuppressWarnings("null")
@DataProvider(name="hybridData")
public Object[][] get() throws IOException
{
Object[][] object = null;
String filePath = System.getProperty("user.dir")+"\\";
System.out.println("File Path is:" + filePath);
String fileName = "TestCaseRep.xls";
//Create a object of File class to open xlsx file
File file = new File(filePath+"\\"+fileName);
//Create an object of FileInputStream class to read excel file
FileInputStream inputStream = new FileInputStream(file);
int totalSheet = 0;
Sheet Sheet1=null;
Workbook Workbookone = null;
//Find the file extension by spliting file name in substing and getting only extension name
String fileExtensionName = fileName.substring(fileName.indexOf("."));
//Check condition if the file is xlsx file
if(fileExtensionName.equals(".xlsx")){
//If it is xlsx file then create object of XSSFWorkbook class
Workbookone = new XSSFWorkbook(inputStream);
}
//Check condition if the file is xls file
else if(fileExtensionName.equals(".xls")){
//If it is xls file then create object of XSSFWorkbook class
Workbookone = new HSSFWorkbook(inputStream);
}
//Read sheet inside the workbook by its name
totalSheet = Workbookone.getNumberOfSheets();
if(totalSheet > 0) {
System.out.println("Total Sheet Found:" + totalSheet);
for(int k=0;k<totalSheet ;k++) {
System.out.println("Sheet Name:" + Workbookone.getSheetName(k));
Sheet1 = Workbookone.getSheetAt(k);
System.out.println("Sheet Found:" + Sheet1);
//Find number of rows in excel file
int rowCount = Sheet1.getLastRowNum()-Sheet1.getFirstRowNum();
object = new Object[rowCount][5];
for (int i = 0; i < rowCount; i++) {
//Loop over all the rows
Row row = Sheet1.getRow(i+1);
//Create a loop to print cell values in a row
for (int j = 0; j < row.getLastCellNum(); j++) {
//Print excel data in console
object[i][j] = row.getCell(j).toString();
System.out.println(object[i][j]);
}
}
System.out.println("");
}
}
return object;
}
}
答案 0 :(得分:1)
问题是你正在重新初始化for循环中的object
变量,这就是你得到最后一张纸的价值的原因。
因为它是一个二维数组,您只能存储两个值的行和列。
如果你想要存储工作表,你必须去三维数组。但我建议你去列表。所以我将返回类型从Object[][]
修改为ArrayList<Object[][]>
,以便将工作表数据添加到List
public ArrayList<Object[][]> get() throws IOException {
ArrayList<Object[][]> sheetDatas=new ArrayList<>();
Object[][] object = null;
String filePath = System.getProperty("user.dir") + "\\";
System.out.println("File Path is:" + filePath);
String fileName = "TestCaseRep.xls";
//Create a object of File class to open xlsx file
File file = new File(filePath + "\\" + fileName);
//Create an object of FileInputStream class to read excel file
FileInputStream inputStream = new FileInputStream(file);
int totalSheet = 0;
Sheet Sheet1 = null;
Workbook Workbookone = null;
//Find the file extension by spliting file name in substing and getting only extension name
String fileExtensionName = fileName.substring(fileName.indexOf("."));
//Check condition if the file is xlsx file
if (fileExtensionName.equals(".xlsx")) {
//If it is xlsx file then create object of XSSFWorkbook class
Workbookone = new XSSFWorkbook(inputStream);
} //Check condition if the file is xls file
else if (fileExtensionName.equals(".xls")) {
//If it is xls file then create object of XSSFWorkbook class
Workbookone = new HSSFWorkbook(inputStream);
}
//Read sheet inside the workbook by its name
totalSheet = Workbookone.getNumberOfSheets();
if (totalSheet > 0) {
System.out.println("Total Sheet Found:" + totalSheet);
for (int k = 0; k < totalSheet; k++) {
System.out.println("Sheet Name:" + Workbookone.getSheetName(k));
Sheet1 = Workbookone.getSheetAt(k);
System.out.println("Sheet Found:" + Sheet1);
//Find number of rows in excel file
int rowCount = Sheet1.getLastRowNum() - Sheet1.getFirstRowNum();
/**
*
* You are reinitializing the object here so old datas will be lost
*/
object = new Object[rowCount][5];
for (int i = 0; i < rowCount; i++) {
//Loop over all the rows
Row row = Sheet1.getRow(i + 1);
//Create a loop to print cell values in a row
for (int j = 0; j < row.getLastCellNum(); j++) {
//Print excel data in console
object[i][j] = row.getCell(j).toString();
System.out.println(object[i][j]);
}
}
System.out.println("");
sheetDatas.add(object);
}
}
return sheetDatas;
}
}
例如,如果您想从表2中获取值 你可以像list.get(1)
一样打电话