gpt4 book ai didi

java - 1 次测试运行后无法从文件中读取,org.apache.poi.EmptyFileException : The supplied file was empty (zero bytes long)

转载 作者:行者123 更新时间:2023-11-30 07:45:37 26 4
gpt4 key购买 nike

我附上代码。基本上发生的情况是第一次测试运行正常,但是在第二次测试运行中文件以某种方式损坏。

File src=new File("C:\\Users\\Sajid\\Desktop\\SeleniumData.xlsx");   
FileInputStream fis = new FileInputStream(src);
workbook = new XSSFWorkbook(fis);
sheet= workbook.getSheetAt(0);

for(int i=1; i<=sheet.getLastRowNum(); i++){
// Import data for Email.
cell = sheet.getRow(i).getCell(0);
cell.setCellType(Cell.CELL_TYPE_STRING);
new WebDriverWait(driver, 50).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"email\"]")));
driver.findElement(By.xpath("//*[@id=\"email\"]")).sendKeys(cell.getStringCellValue());
Thread.sleep(500);
// Import data for password.
cell = sheet.getRow(i).getCell(1);
cell.setCellType(Cell.CELL_TYPE_STRING);
WebElement password = driver.findElement(By.xpath("//*[@id=\"password\"]"));
Actions act1 = new Actions(driver);
act1.moveToElement(password).perform();
password.clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys(cell.getStringCellValue());
Thread.sleep(500);
// To click on Login button
driver.findElement(By.className("submit")).click();
Thread.sleep(4000);
FileOutputStream fos=new FileOutputStream(src);
// Message to be written in the excel sheet
WebElement ele4 = driver.findElement(By.className("mdi-settings"));
act1.moveToElement(ele4).perform();
driver.findElement(By.className("mdi-settings")).click();
Thread.sleep(1000);
WebElement ele5 = driver.findElement(By.linkText("Location Details"));
act1.moveToElement(ele5).perform();
driver.findElement(By.linkText("Location Details")).click();
Thread.sleep(3000);
cell = sheet.getRow(i).getCell(2);
cell.setCellType(Cell.CELL_TYPE_STRING);
driver.findElement(By.xpath("//*[@id =\"name\"]")).clear();
driver.findElement(By.xpath("//*[@id =\"name\"]")).sendKeys(cell.getStringCellValue());
fos.close();
fis.close();
driver.findElement(By.cssSelector("b.hidden-xs")).click();
Thread.sleep(1000);
driver.findElement(By.cssSelector("a.sign-out")).click();
Thread.sleep(1000);

我在第二次迭代时遇到的异常是:

org.apache.poi.EmptyFileException: The supplied file was empty (zero bytes long)

最佳答案

问题出在 FileOutputStream fos = new FileOutputStream(src); 中,它创建了没有内容的新文件。因为 src 是输入文件,所以它现在是空的。

要么不使用它(我找不到任何用途),写入另一个文件,要么使用 FileOutputStream fos = new FileOutputStream(src, true); 如果你想将数据附加到文件。

关于java - 1 次测试运行后无法从文件中读取,org.apache.poi.EmptyFileException : The supplied file was empty (zero bytes long),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51614446/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com