我正在尝试使用硒测试脚本从excel检索数据。我有一个excelData文件,在其中有我的代码可以读取和设置excel数据,还有另一个类文件,可以在其中从excel中获取数据。当我运行测试脚本时,它没有返回任何数据。
我的Excel代码:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
public class ExcelUtils {
private static XSSFSheet ExcelWSheet;
private static XSSFWorkbook ExcelWBook;
private static XSSFCell Cell;
private static XSSFRow row;
private static MissingCellPolicy xRow;
//This method is to set the File path and to open the Excel file, Pass Excel Path and Sheetname as Arguments to this method
public static void setExcelFile(String Path,String SheetName) throws Exception {
try {
// Open the Excel file
FileInputStream ExcelFile = new FileInputStream(Path);
// Access the required test data sheet
ExcelWBook = new XSSFWorkbook(ExcelFile);
ExcelWSheet = ExcelWBook.getSheet(SheetName);
} catch (Exception e){
throw (e);
}
}
//This method is to read the test data from the Excel cell, in this we are passing parameters as Row num and Col num
public static String getCellData(int RowNum, int ColNum) throws Exception{
try{
Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
String CellData = Cell.getStringCellValue();
return CellData;
}catch (Exception e){
return"";
}
}
//This method is to write in the Excel cell, Row num and Col num are the parameters
public static void setCellData(String Result, int RowNum, int ColNum) throws Exception {
try{
row = ExcelWSheet.getRow(RowNum);
Cell = row.getCell(ColNum, MissingCellPolicy.RETURN_BLANK_AS_NULL);
if (Cell == null) {
Cell = row.createCell(ColNum);
Cell.setCellValue(Result);
} else {
Cell.setCellValue(Result);
}
// Constant variables Test Data path and Test Data file name
FileOutputStream fileOut = new FileOutputStream(Constants.Path_TestData + Constants.File_TestData);
ExcelWBook.write(fileOut);
fileOut.flush();
fileOut.close();
}catch(Exception e){
throw (e);
}
}
}
我的测试脚本:
import org.openqa.selenium.By;
import org.testng.annotations.Test;
import config.config;
import utility.ExcelUtils;
public class ExcelData extends config {
@Test(priority=0)
public void SignIn() throws Exception {
driver.get("https://www.google.com/");
String SearchData = ExcelUtils.getCellData(1, 1);
driver.findElement(By.name("q")).sendKeys(SearchData);
}
}
这是常量文件:
public class Constants {
public static final String Chrome_Driver = "~/Eclipserelatedfiles/chromedriver_linux64/chromedriver";
public static final String Path_TestData = "~/Documents/eclipse-workspace/Automation/testdata/";
public static final String File_TestData = "TestData.xlsx";
}
Excel文件:
Data
-----------
seleniumhq
我没有出现问题,因为我要提取的特定单元格中的excel中存在数据。任何帮助/建议都会有所帮助。
答案 0 :(得分:1)
我已经使用下面的控制台日志测试了您的代码,并且该代码可以正常工作。
public static void main(String[] args) throws Exception{
ExcelUtils.setExcelFile("D:/Book1.xlsx","sheet1");
String SearchData = ExcelUtils.getCellData(1, 1);
System.out.println("Data in cell B2 = " + SearchData);
}