gpt4 book ai didi

java - 通过 "Executable Jar"文件运行 selenium 测试时无法从 xls 获取测试数据

转载 作者:行者123 更新时间:2023-12-02 12:38:00 25 4
gpt4 key购买 nike

我通过使用以下代码创建可执行 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();
}
}

为了执行测试用例,我使用 testdata.xls。每当我执行任何测试时,都需要 testdata.xls。

以下是我的项目的层次结构:[请点击图像进行说明]。

enter image description here

我已经从 eclipse -> 项目 -> 导出 -> 作为“可运行的 jar 文件”创建了可执行 jar 文件。

因此,当我执行 jar 文件时,测试用例开始执行,但无法找到 testdata.xls 文件,因此脚本失败终止。

我的问题是:

我需要做哪些更改,以便我的脚本在使用 jar 文件执行测试时从 test data.xls 获取测试数据?

更新:- 编写此代码后添加屏幕截图

public String getCellDataWithRowColAndSheetName(int col, int row, String sheetName) throws BiffException, IOException {
InputStream inputStream = this.getClass().getResourceAsStream("TestData/testdata.xls");
if (inputStream == null) {
throw new IllegalStateException("Unable to read excel spreadsheet from the jar");
}
Workbook workbook = Workbook.getWorkbook(inputStream);
Sheet sheet = workbook.getSheet(sheetName);
Cell cell = sheet.getCell(col, row);
return cell.getContents();
}

enter image description here

最佳答案

这取决于您用于读取 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);

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

关于java - 通过 "Executable Jar"文件运行 selenium 测试时无法从 xls 获取测试数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45076830/

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