[英]Selenium data-driven testing: Null pointer Exception
嗨,我正在關注有關硒中數據驅動測試的教程,我想從excel文件.xlsx中讀取數據,但它給了我空指針。
下面是代碼:
package com.usman;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class ReadDataFromExcelSheet {
public String[][] getExcelData(String excellocation, String sheetName) {
try {
String dataSets[][] = null;
FileInputStream file = new FileInputStream(new File(excellocation));
// Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(file);
// Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheet(sheetName);
// count number of active rows
int totalRow = sheet.getLastRowNum();
// count number of active columns in row
int totalColumn = sheet.getRow(0).getLastCellNum();
// Create array of rows and column
dataSets = new String[totalRow][totalColumn];
// Iterate through each rows one by one
Iterator<Row> rowIterator = sheet.iterator();
int i = 0;
while (rowIterator.hasNext()) {
System.out.println(i);
Row row = rowIterator.next();
// For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();
int j = 0;
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if (cell.getStringCellValue().contains("User Name")) {
break;
}
// Check the cell type and format accordingly
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
dataSets[i - 1][j++] = cell.getStringCellValue();
System.out.println(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:
dataSets[i - 1][j++] = cell.getStringCellValue();
System.out.println(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN:
dataSets[i - 1][j++] = cell.getStringCellValue();
System.out.println(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
dataSets[i - 1][j++] = cell.getStringCellValue();
System.out.println(cell.getStringCellValue());
break;
}
}
System.out.println("");
i++;
}
file.close();
return dataSets;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void updateResult(String excellocation, String sheetName, String testCaseName, String testStatus) throws IOException {
try {
FileInputStream file = new FileInputStream(new File(excellocation));
// Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(file);
// Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheet(sheetName);
// count number of active tows
int totalRow = sheet.getLastRowNum() + 1;
// count number of active columns in row
for (int i = 1; i < totalRow; i++) {
XSSFRow r = sheet.getRow(i);
String ce = r.getCell(1).getStringCellValue();
if (ce.contains(testCaseName)) {
r.createCell(2).setCellValue(testStatus);
file.close();
System.out.println("resule updated");
FileOutputStream outFile = new FileOutputStream(new File(excellocation));
workbook.write(outFile);
outFile.close();
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public Object[][] getExcelDataBasedOnStartingPoint(String excellocation, String sheetName, String testName) {
try {
String dataSets[][] = null;
FileInputStream file = new FileInputStream(new File(excellocation));
// Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(file);
// Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheet(sheetName);
// count number of active rows
int totalRow = sheet.getLastRowNum();
int totalColumn = 0;
// Iterate through each rows one by one
Iterator<Row> rowIterator = sheet.iterator();
int i = 0;
int count = 1;
while (rowIterator.hasNext() && count == 1 || count == 2) {
// System.out.println(i);
Row row = rowIterator.next();
// For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();
int j = 0;
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if (cell.getStringCellValue().contains(testName + "end")) {
count = 0;
break;
}
// System.out.println(sheetName+"Start");
if (cell.getStringCellValue().contains(testName + "start")) {
// count number of active columns in row
totalColumn = row.getPhysicalNumberOfCells() - 1;
// Create array of rows and column
dataSets = new String[totalRow][totalColumn];
}
// System.out.println(sheetName+"Start");
if (cell.getStringCellValue().contains(testName + "start") || count == 2) {
System.out.println(sheetName + "start");
count = 2;
// Check the cell type and format accordingly
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
dataSets[i - 1][j++] = cell.getStringCellValue();
System.out.println(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:
if (!cell.getStringCellValue().contains(testName + "start")) {
dataSets[i - 1][j++] = cell.getStringCellValue();
System.out.println(cell.getStringCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
dataSets[i - 1][j++] = cell.getStringCellValue();
System.out.println(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
dataSets[i - 1][j++] = cell.getStringCellValue();
System.out.println(cell.getStringCellValue());
break;
}
}
}
System.out.println("");
i++;
}
file.close();
return parseData(dataSets, totalColumn);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* This method is used to remove unwanted null data from array
*
* @param data
* @return
*/
public Object[][] parseData(Object[][] data, int colSize) {
// Creating array list to store data;
ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
// This array list will store one Array index data, every array index
// has three sets of data
ArrayList<String> list1;
System.out.println(data.length);
// running for loop on array size
for (int i = 0; i < data.length; i++) {
// creates a list to store the elements != null
System.out.println(data[i].length);
list1 = new ArrayList<String>();
// this for loop will run on array index, since each array index has
// three sets of data
for (int j = 0; j < data[i].length; j++) {
// this if will check null
if (data[i][j] != null) {
list1.add((String) data[i][j]);
}
}
// once all one array index data is entered in arrayList , then
// putting this object in parent arrayList
if (list1.size() > 0) {
list.add(list1);
}
}
// convert array List Data into 2D Array
Object[][] arr2d = new Object[list.size()][colSize];
// run loop on array list data
for (int i = 0; i < list.size(); i++) {
// every array list index has arryList inside
ArrayList<String> t = list.get(i);
// run loop on inner array List
for (int j = 0; j < t.size(); j++) {
arr2d[i][j] = t.get(j);
}
}
System.out.println(list);
System.out.println(arr2d);
return arr2d;
}
public static void main(String[] args) throws IOException {
String excellocation = "D:\\usman data\\excel-tutorial-with-dataDrivenFramework-master\\excel-tutorial-with-dataDrivenFramework-master\\src\\main\\resources\\testData\\demo.xlsx";
String sheetName = "login";
ReadDataFromExcelSheet excel = new ReadDataFromExcelSheet();
Object[][] data = excel.getExcelDataBasedOnStartingPoint(excellocation, sheetName, "login");
System.out.println(data);
// excel.updateResult(excellocation, sheetName, "Login Test", "FAIL");
// excel.updateResult(excellocation, sheetName, "Registartion Test",
// "PASS");
// excel.updateResult(excellocation, sheetName, "Dashboard Test",
// "PASS");
}
}
我收到以下錯誤:
警告:發生了非法的反射訪問操作警告:org.dom4j.io.SAXContentHandler(文件:/ C:/Users/True%20Meridian/.m2/repository/dom4j/dom4j/1.6.1/dom4j- 1.6.1.jar)方法com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser $ LocatorProxy.getEncoding()警告:請考慮將此報告給org.dom4j.io.SAXContentHandler的維護者警告:使用- -illegal-access =警告以啟用進一步的非法反射訪問操作的警告警告:在以后的發行版中,com.usman.ReadDataFromExcelSheet.getExcelDataBasedOnStartingPoint(ReadDataFromExcelSheet.java:130)上的java.lang.NullPointerException null將拒絕所有非法訪問操作。 com.usman.ReadDataFromExcelSheet.main(ReadDataFromExcelSheet.java:258)
在XSSFSheet sheet = workbook.getSheet(sheetName);
在方法getExcelDataBasedOnStartingPoint
它可能返回null
。 如果是這樣,提供的名稱不是工作簿中任何工作表的工作表名稱。 您應該檢查是否為空。 像這樣:
XSSFSheet sheet = workbook.getSheet(sheetName);
if (sheet == null) throw new IllegalArgumentException("Sheet with sheet name " + sheetName + " does not exist");
話雖如此,這不能完全解決您的問題。 您的主要問題是,工作表“ D:\\ usman data \\ excel-tutorial-with-dataDrivenFramework-master \\ excel-tutorial-with-dataDrivenFramework-master \\ src \\ main \\ resources \\ testData”中不存在工作表“ login” \\ demo.xlsx”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.