Я пытаюсь получить данные из 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 в конкретной ячейке, которую япытаюсь достать.Любая помощь / предложения будут полезны.