我是硒的关键字驱动方法的新手。我在NullPointerException
ExecuteTest.java
文件夹结构
Object.txt
TestCase.xlsx
错误屏幕截图
添加调试屏幕截图 screenshot1 screenshot2 screenshot3
ReadGuru99Excel.java
import org.apache.poi.ss.usermodel.Sheet;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ReadGuru99ExcelFile {
public Sheet readExcel (String filePath,String fileName, String sheetName)throws IOException{
File file = new File(filePath+"\\"+fileName);
FileInputStream inputStream = new FileInputStream(file);
Workbook guru99Workbook = null;
String fileExtensionName = fileName.substring(fileName.indexOf("."));
if(fileExtensionName.equals(".xlsx")){
guru99Workbook = new XSSFWorkbook(inputStream);
} else if(fileExtensionName.equals(".xls")) {
guru99Workbook = new HSSFWorkbook(inputStream);
}
Sheet guru99Sheet =guru99Workbook.getSheet(sheetName);
return guru99Sheet;
}
}
ReadObject.Java
package operation;
import java.util.Properties;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.File;
public class ReadObject {
Properties p = new Properties();
public Properties getObjectRepository()throws IOException{
InputStream stream = new FileInputStream(new File (System.getProperty("user.dir")+"\\src\\objects\\object.txt"));
p.load(stream);
return p;
}
}
UIOperation.java
package operation;
import org.openqa.selenium.WebDriver;
import java.util.Properties;
import org.openqa.selenium.By;
public class UIOperation {
WebDriver driver ;
public UIOperation(WebDriver driver){
this.driver = driver;
}
public void perform(Properties p, String operation, String objectName, String objectType, String value) throws Exception{
System.out.println("");
switch(operation.toUpperCase()){
case "CLICK":
driver.findElement(this.getObject(p, objectName, objectType)).click();
break;
case "SETTEXT":
driver.findElement(this.getObject(p, objectName, objectType)).sendKeys(value);
break;
case "GOTOURL":
driver.get(p.getProperty(value));
break;
case "GETTEXT":
driver.findElement(this.getObject(p, objectName, objectType)).getText();
break;
default:
break;
}
}
private By getObject(Properties p, String objectName, String objectType) throws Exception{
if(objectType.equalsIgnoreCase("XPATH")){
return By.xpath(p.getProperty(objectName));
}else if(objectType.equalsIgnoreCase("CLASSNAME")){
return By.className(p.getProperty(objectName));
}else if(objectType.equalsIgnoreCase("NAME")){
return By.name(p.getProperty(objectName));
}else if(objectType.equalsIgnoreCase("CSS")){
return By.cssSelector(p.getProperty(objectName));
}else if(objectType.equalsIgnoreCase("LINK")){
return By.linkText(p.getProperty(objectName));
}else if(objectType.equalsIgnoreCase("PARTIALLINK")){
return By.partialLinkText(p.getProperty(objectName));
}else{
throw new Exception("Wrong object type");
}
}
}
ExecuteTest.java
package testcases;
import java.util.Properties;
import operation.ReadObject;
import org.testng.annotations.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.MarionetteDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import operation.UIOperation;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import excelExportAndFileIO.ReadGuru99ExcelFile;
public class ExecuteTest {
@Test
public void testLogin()throws Exception{
/**
* System.setProperty("webdriver.gecko.driver", "E:\\Selenium-2017\\geckodriver-v0.18.0-win64\\geckodriver.exe");
* //Now you can Initialize marionette driver to launch firefox
* DesiredCapabilities capabilities = DesiredCapabilities.firefox();
* capabilities.setCapability("marionette", true);
* WebDriver driver = new RemoteWebdriver(capabilities);
*
* WebDriver webdriver = new FirefoxDriver();
**/
WebDriver driver;
System.setProperty("webdriver.chrome.driver","E:\\Selenium-2017\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
ReadGuru99ExcelFile file = new ReadGuru99ExcelFile();
ReadObject object = new ReadObject();
Properties allObjects = object.getObjectRepository();
UIOperation operation = new UIOperation(driver);
Sheet guru99Sheet = file.readExcel(System.getProperty("user.dir")+"\\test-output","TestCase.xlsx","KeywordFramework");
int rowCount =guru99Sheet.getLastRowNum()-guru99Sheet.getFirstRowNum();
System.out.println("first step clear");
for(int i = 0;i<rowCount+1; i++){
Row row =guru99Sheet.getRow(i);
System.out.println("2nd clear");
if(row.getCell(0).toString().length()==0){
System.out.println("4nd clear");
System.out.println(row.getCell(1).toString()+"-----"+row.getCell(2).toString()+"----"+row.getCell(3).toString()+"---"+row.getCell(4).toString());
System.out.println("3rd clear");
operation.perform(allObjects, row.getCell(1).toString(), row.getCell(2).toString(), row.getCell(3).toString(), row.getCell(4).toString());
} else
System.out.println("New Testcase->" + row.getCell(0).toString()+"Started");
System.out.println("testerassumption");
}
}
}
答案 0 :(得分:0)
首先,请查看关于NullPointerException
以及如何解决此问题的精彩post。
在您的情况下,堆栈跟踪将NullPointerException
指向ExecuteTest.java
中代码的第49行,该代码指向此行代码
operation.perform(allObjects, row.getCell(1).toString(), row.getCell(2).toString(), row.getCell(3).toString(), row.getCell(4).toString());
这里的问题是,当您尝试将getCell()
方法中的单元格内容转换为String时,如果单元格中没有任何内容,那么总会有{{ 1}}。
您可以在每个单元格之前进行NullPointerException
检查,也可以为每个!= null
方法添加" "
,例如
toString()