【问题标题】:Selenium Webdriver Duplicating ExcelSheetSelenium Webdriver 复制 Excel 表
【发布时间】:2015-08-04 20:31:34
【问题描述】:

我尝试将包含 7 个名为 Excel.xls 工作表的列的 Excel 工作表复制到 ExcelCopy.xls 中,但是在 @After Test 处出现 Java Null Exception 错误,并且对于这种 selenium 编码非常陌生,请帮助我!

 package TestNG;


import java.io.FileInputStream;
import java.io.FileOutputStream;

import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class DuplicateExcelSheet {
    WebDriver driver;
    WebDriverWait wait;

    Workbook w;
    Sheet s;
    FileInputStream fi;
    FileOutputStream fo;
    WritableWorkbook ww;
    WritableSheet ws;
  @Test
  public void f() throws Exception{
    int colCount=s.getColumns();
    System.out.println(colCount);
    ww=Workbook.createWorkbook(fo, w);
    ws=ww.createSheet("Data", 0);

    for (int i = 0; i < colCount; i++) 
    {
    String s1=s.getCell(i, 0).getContents();
     Label l=new Label(i,0,s1);
     ws.addCell(l);
    }
     }

  @BeforeTest
  public void beforeTest() throws Exception{

      fi=new FileInputStream("E:\\selenium\\Excel.xls");
        w=Workbook.getWorkbook(fi);
        s=w.getSheet(0);
          fo=new FileOutputStream("E:\\selenium\\ExcelCopy.xls");
      }

  @AfterTest
  public void afterTest() throws Exception{
       ww.write();
       w.close();
      fi.close();
      fo.close();


  }

}

【问题讨论】:

  • NullPointerException 发生在哪一行?
  • @AfterTest afterTest java.lang.NullPointerException at TestNG.DuplicateExcelSheet.afterTest(DuplicateExcelSheet.java:55) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke( Unknown Source) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 的错误是这样的
  • @rahulbommanaboina 查看我的答案

标签: excel selenium


【解决方案1】:

我假设您想复制完整的 Excel 工作簿。如果,那么请使用FileUtil,这更容易且编码更少。好在你不需要担心目标目录、文件等。如果目标存在,它也会覆盖。

File sourceExcel = new File("D:\\Users\\Saifur\\Desktop\\Delete\\excelFrom\\Selenium.xlsx");
File dstExcel = new File("D:\\Users\\Saifur\\Desktop\\Delete\\excelTo\\Selenium_Copy.xlsx");

try {

    FileUtils.copyFile(sourceExcel, dstExcel);

} catch (IOException e) {

    e.printStackTrace();
}

确保您有以下导入

import org.apache.commons.io.FileUtils;

Maven 仓库here

【讨论】:

  • @rahulbommanaboina 很高兴它做到了。如果这是您一直在寻找的答案,请将答案标记为已接受。见this
【解决方案2】:

看起来您将FileOutputStream fo 声明为类成员,但您没有将其分配到任何地方。因此,当您尝试调用 fo.close()

时,它在第 55 行是 NULL

如果没有使用fo,我建议你把它从这个类中删除。

【讨论】:

  • 我使用 fileOutputStream 修改了程序,仍然得到同样的错误.. !! :(
  • 我建议把所有的对象创建放在@BeforeTest
  • 如果您使用的是 Eclipse,您可以使用调试断点并在测试执行时观察 fo 的状态变化。 Notes on breakpoints
  • 啊!!如果在测试之前在 @ 中声明对象,它会抛出错误,因为我在 @Test 中使用了对象,并且遇到断点不确定如何使用它们或检查,就像 selenium 和编码的初学者一样。
  • 我摆脱了异常,稍微改变了程序,但是现在,excel表已经创建,但是数据被复制了,有人可以检查程序告诉我,为什么数据没有抄袭??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-22
  • 1970-01-01
  • 1970-01-01
  • 2017-08-07
  • 2020-08-22
  • 1970-01-01
相关资源
最近更新 更多