【发布时间】:2015-06-08 20:47:43
【问题描述】:
我正在使用以下函数从 excel 中读取数据以进行 selenium webdriver 测试。由于数据表有数百行要执行,因此需要下拉菜单来选择特定单元格的值,这使用户更容易在数据表中填写数据。我已使用数据验证列表为 Excel 工作表中的单元格启用下拉菜单。但是如果我尝试执行测试保存它后,我会得到下面给出的错误。如果我从数据表中删除下拉列表选项,它工作正常。 请帮忙,因为我必须在 excel 中为多个单元格值提供下拉菜单,以便轻松输入数据。
package functional_libraries;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.annotations.*;
public class excelNew { @Test
public static String[][] excelRead(String filename, String Sheetname) throws
IOException {
File excel = new File(filename);
FileInputStream fis = new FileInputStream(excel);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet ws = wb.getSheet(Sheetname) ;
int rowNum = ws.getLastRowNum() + 1 ;
int colNum = ws.getRow(0).getLastCellNum() ;
String[][] data = new String[rowNum][colNum] ;
for ( int i = 0 ; i < rowNum ; i++) {
XSSFRow row = ws.getRow(i) ;
for ( short j = 0 ; j < colNum ; j++) {
XSSFCell cell = row.getCell(j) ;
String value = cellToString(cell);
data[i][j] = value ;
}
}
return data ;
}
@Test
public static String cellToString(XSSFCell cell) {
int type;
Object result ;
type = cell.getCellType();
switch (type) {
case 0 : // numeric value in Excel
result = cell.getNumericCellValue() ;
break ;
case 1 : // String Value in Excel
result = cell.getStringCellValue() ;
break ;
case 3 : // Blank Cell
result = "";
break;
default :
throw new RuntimeException("There are no support for this type of cell") ;
}
return result.toString() ;
}
}
[TestNG] 运行: C:\Users\suhail\AppData\Local\Temp\testng-eclipse-1517048838\testng-customsuite.xml
FAILED: TestClaimEntry
java.lang.NullPointerException
at functional_libraries.excelNew.excelRead(excelNew.java:31)
at testSet.TestClaimEntry.TestClaimEntry(TestClaimEntry.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
【问题讨论】:
-
哪一行给出了空指针?
-
它是我脚本中的第 31 行,我在其中调用函数,即 excelNew 来读取数据表。
-
它是我脚本中的第 31 行,我在其中调用函数,即 excelNew 来读取数据表。 @Test public void TestClaimEntry() throws Exception { String [][] steps ;字符串 [][] 数据;字符串 ScriptSteps = "C:\\Testing\\TestClaimEntrySteps.xlsx"; //测试数据文档位置 String DataSet = ""C:\\Testing\\TestData.xlsx"; //line#31 //调用函数从测试脚本文档中读取测试步骤 steps = excelNew.excelRead(ScriptSteps, "TestScript"); //调用函数从Test数据表中读取Test数据 data = excelNew.excelRead(DataSet,"TestData");
标签: excel selenium apache-poi testng