请检查代码建议更正:
组件框架;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.Duration;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class DataDriveFramework {
public void readExcel(String filePath, String fileName, String sheetName) throws IOException {
File file = new File(filePath+"\\"+fileName);
FileInputStream fis = new FileInputStream(file);
Workbook loginWorkbook=null;
String fileExtension=fileName.substring(fileName.indexOf("."));
if(fileExtension.equals(".xlsx"))
{
loginWorkbook=new XSSFWorkbook(fis);
}
else if(fileExtension.equals("xls"))
{
loginWorkbook=new HSSFWorkbook(fis);
}
Sheet loginSheet=loginWorkbook.getSheet(sheetName);
int rowCount=loginSheet.getLastRowNum()-loginSheet.getFirstRowNum();
for(int i=1;1<rowCount+1;i++)
{
Row row=loginSheet.getRow(i);
String username=row.getCell(0).getStringCellValue();
String password=row.getCell(0).getStringCellValue();
test(username,password);
}
}
public void test(String username, String password)
{
WebDriver driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
String baseURL="https://accounts.google.com";
driver.get(baseURL);
driver.findElement(By.id("identifierId")).sendKeys(username);
driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div/c-wiz/div/div[2]/div/div[2]/div/div[1]/div/div/button/span")).click();
driver.findElement(By.xpath("//*[@id=\"password\"]/div[1]/div/div[1]/input")).sendKeys(password);
driver.findElement(By.xpath("//*[@id=\"passwordNext\"]/div/button/span")).click();
driver.quit();
}
public static void main(String[] args)throws IOException {
DataDriveFramework readFile=new DataDriveFramework();
String filePath="C:\\Users\\Shefali\\eclipse-workspace\\DataDriveFramework\\TestExcelSheet\\";
readFile.readExcel(filePath, "DataDriven.xls", "Sheet1");
}
}
代码是从excel工作表中获取数据并自动登录。但我得到一个错误--
"Exception in thread "main" java.lang.NullPointerException: Cannot invoke "org.apache.poi.ss.usermodel.Workbook.getSheet(String)" because "loginWorkbook" is "null"
at com.framework.DataDriveFramework.readExcel(DataDriveFramework.java:31)
at com.framework.DataDriveFrame.main(DataDriveFrame.java:60)"
1条答案
按热度按时间z0qdvdin1#
您在
readExcel()
方法的以下行中遗漏了一个点'.'
:它应该是: