Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法从xls获取测试数据,同时通过“运行selenium测试”;可执行Jar";文件_Java_Excel_Testng_Executable Jar - Fatal编程技术网

Java 无法从xls获取测试数据,同时通过“运行selenium测试”;可执行Jar";文件

Java 无法从xls获取测试数据,同时通过“运行selenium测试”;可执行Jar";文件,java,excel,testng,executable-jar,Java,Excel,Testng,Executable Jar,我通过使用以下代码创建可执行jar文件来运行selenium脚本 public class ExecutableRar { public static void main(String[] args) { TestListenerAdapter tla = new TestListenerAdapter(); TestNG testng = new TestNG(); List<String> testFilesList = new ArrayList<

我通过使用以下代码创建可执行jar文件来运行selenium脚本

public class ExecutableRar {

public static void main(String[] args) {
    TestListenerAdapter tla = new TestListenerAdapter();
    TestNG testng = new TestNG();
    List<String> testFilesList = new ArrayList<String>();
    testFilesList.add("C:\\path to testng.xml file\\testng.xml");
    testng.setTestSuites(testFilesList);
    testng.setUseDefaultListeners(false);
    testng.addListener(tla); 
    testng.run();
   }
}

这取决于您用于读取Excel电子表格的内容

例如,如果您使用的是JXL,那么读取excel电子表格的代码如下所示

InputStream inputStream = this.getClass().getResourceAsStream("TestData/testdata.xls");
if (inputStream == null) {
    throw new IllegalStateException("Unable to read excel spreadsheet from the jar");
}
jxl.Workbook workbook =  jxl.Workbook.getWorkbook(inputStream);

您要做的唯一更改是将代码从读取文件更改为从类路径读取输入流,然后将其输入excel读取机制。

您能告诉我如何为excel文件设置类路径吗?您不需要为excel设置类路径。它是自动可用的。您可以通过查看目标/文件夹来再次检查这一点。你应该在那里的某个地方找到你的电子表格。我不确定你指的是什么正确的文件路径。另外,生成的异常是什么?我得到的是IllegalSateException,但随后我清理并重建了eclipse,现在我没有得到异常,但没有从testdata文件中获取任何内容。testdata/testdata.xls这是我所指的路径
InputStream inputStream = this.getClass().getResourceAsStream("TestData/testdata.xls");
if (inputStream == null) {
    throw new IllegalStateException("Unable to read excel spreadsheet from the jar");
}
jxl.Workbook workbook =  jxl.Workbook.getWorkbook(inputStream);