2017-07-14 76 views
0

我是新的关键字驱动的硒方法。我越来越NullPointerException运行下面ExecuteTest.java关键字驱动框架 - 获取错误java.lang.NullPointerException

文件夹结构

  1. Folder structure

而Object.txt

  1. Object.txt

TestCase.xlsx

  1. TestCase.xlsx

错误截图

  1. Screenshot1
  2. Screenshot2

    添加调试截图 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

嗨@ @ santhoshkumar.I尝试调试并附上了调试结果的截图,未弄清楚哪一行抛出异常?请参考它。谢谢!寻求帮助 –

+1

请勿为纯文本提供*屏幕截图*。直接在帖子中包含文字。 –

回答

0

首先,请看到这个精彩的postNullPointerException以及如何解决它。

在你的情况下,堆栈跟踪点NullPointerException到在ExecuteTest.java的代码,它指向这行代码的行49存在的

operation.perform(allObjects, row.getCell(1).toString(), row.getCell(2).toString(), row.getCell(3).toString(), row.getCell(4).toString()); 

这里的问题是,当你试图将getCell()方法中单元格的内容转换为字符串,如果单元格中没有任何内容,则总是会有NullPointerException

您可以放置​​一个!= null检查每一个细胞之前或添加" "为每toString()方法,像

operation.perform(allObjects, (row.getCell(1)+"").toString(), (row.getCell(2)+"").toString(), (row.getCell(3)+"").toString(), (row.getCell(4)+"").toString()); 
+0

谢谢! @ demouser123通过为每个toString()添加“”我现在没有得到任何空指针异常。它的工作原理!感谢分享帖子链接,它帮助我理解空指针异常。 –