英文:
NegativeArraySizeException, DataProvider, Excel
问题
I'd like to implement the data from Excel file to different tests depending on the scenario correct data/invalid one, however when I want to get the cell value I get the "NegativeArraySizeException".
The first row has just a title so I don't want to read it, that's why I have the parameters [rows-1].
Could you please indicate what is my mistake?
Thank you
我想根据不同的场景实现从Excel文件中获取数据以进行不同的测试,正确的数据/无效的数据,但是当我想要获取单元格的值时,我得到了"NegativeArraySizeException"异常。
第一行只有一个标题,所以我不想读取它,这就是为什么我使用了参数 [rows-1]。
请问您能指出我的错误在哪里吗?
谢谢
英文:
I'd like to implement the data from Excel file to different tests depending on the scenario correct data/invalid one, however when I want to get the cell value I get the "NegativeArraySizeException".
The first row has just a title so I don't want to read it, that's why I have the parameters [rows-1].
Could you please indicate what is my mistake?
Thank you
public class SignInTest extends Driver {
@BeforeMethod
public void setUp() {
Driver.initConfiguration();
}
public Object[][] getData(String excelPath, String sheetName) {
int rows = excel.getRowCount(sheetName);
int cols = excel.getColumnCount(sheetName);
Object[][] data = new Object[rows - 1][1];
excel = new ExcelReader(excelPath);
for (int rowNum = 1; rowNum < rows; rowNum++) {
for (int colNum = 0; colNum < cols; colNum++) {
data[rowNum - 1][colNum] = excel.getCellData(sheetName, colNum, rowNum);
}
}
return data;
}
@DataProvider(name = "credentials")
public Object[][] getCredentials() {
Object[][] data = getData(excelPath, sheetName);
return data;
}
@Test(dataProviderClass = DataProviders.class, dataProvider = "credentials")
public void loginWithCorrectCredentials(String email, String password) {
HomePageActions hp = new HomePageActions();
SignInActions sign = new SignInActions();
DataProviders dp = new DataProviders();
dp.getData(excelPath, "correctData");
System.out.println("email " + email);
System.out.println("password " + password);
}
答案1
得分: 1
这个函数 "excel.getRowCount(sheetName)"
在这一行:
int rows = excel.getRowCount(sheetName);
返回了0(或可能是null),因此当你执行rows-1时,得到的数值小于零。我希望这一点是显而易见的。所以问题变成了为什么?
在故障排除时要注意的事项:
- getColumnCount是否也返回了零?如果是,这可能指向工作表引用可能出错。
- sheetName是否真正正确地传递给了函数?
- 你能否在工作表的特定位置插入一个显式值?也就是说,那个引用是否有效?添加一行测试代码,看看会发生什么。
- 如果你将数组硬编码设置为:
Object[][] data = new Object100;
我的直觉告诉我你在对工作表引用方面有问题,但如果没有更多了解你的工作表引用,就无法确定。
希望这些提示能指引你朝正确的方向前进。祝你好运!
英文:
This function "excel.getRowCount(sheetName)"
On this line:
int rows = excel.getRowCount(sheetName);
Is returning 0 (or possibly null), thus when you do rows-1, you get a number less than zero. I should hope that much is obvious. So the question becomes WHY?
Things to look for in troubleshooting:
- Is the getColumnCount also returning zero? If so, this points to a possible
error in the worksheet reference. - Is the sheetName actually correctly being passed into the function?
- Can you insert an explicit value into a specific place on the worksheet? Meaning is that reference working? Throw in a test line and see what happens.
- What happens if you hard set the array to say:
Object[][] data = new Object100;
My gut is telling me you have an issue with the reference to the worksheet, but without knowing more about your worksheet referencing, it's impossible to know for sure.
I hope some of this points you in the right direction and gets you going. Good luck!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论