2015-07-13 84 views
0

我的Java如何解決在硒的webdriver空指針異常與Java

public class LaunchApplication { 
    private String LAUNCHAPPLICATION_STATUS=null; 
    private String LAUNCHAPPLICATION_MESSAGE=null; 


    @Test 
    @Parameters({"RUN_USING_SG","BROWSER_CODE","URL","TITLE","PLATFORM","VERSION"}) 
    public void LAUNCHAPPLICATION(String RunUsingSG,String BrowserCode,String URL,String Title,String Platform,String Version) throws MalformedURLException 
    { 
     try 
     { 
      System.out.println(System.getProperty("user.dir")); 
      DesiredCapabilities dc=new DesiredCapabilities(); 
      if(BrowserCode.equalsIgnoreCase("IE32")) 
      { 
       System.setProperty("webdriver.ie.driver",System.getProperty("user.dir")+"/AllDrivers/IEDriverServer_32bit_OS.exe"); 
       dc.setBrowserName("internet explorer"); 
       dc.setVersion(Version); 
       Config.driver = new InternetExplorerDriver(); 

      } 
      if(BrowserCode.equalsIgnoreCase("IE64")) 
      { 
       System.setProperty("webdriver.ie.driver",System.getProperty("user.dir")+"/AllDrivers/IEDriverServer_64bit_OS.exe"); 
       dc.setBrowserName("internet explorer"); 
       dc.setVersion(Version); 
       Config.driver = new InternetExplorerDriver(); 
      } 
      if(BrowserCode.equalsIgnoreCase("FF")) 
      { 
       FirefoxProfile firefoxProfile = new FirefoxProfile(); 
       dc.setBrowserName("firefox"); 
       dc.setVersion(Version); 
       Config.driver=new FirefoxDriver(); 
      } 
      if(RunUsingSG.equalsIgnoreCase("Y")) 
      { 
       Config.driver=new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),dc); 

      } 

      if(Platform.equalsIgnoreCase("WINDOWS")) 
      {dc.setPlatform(org.openqa.selenium.Platform.WINDOWS);} 
      Config.driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); 
      Config.driver.get(URL); 
      Config.driver.manage().window().maximize(); 
      //perform final validation 
      if(Config.driver.getTitle().toLowerCase().equalsIgnoreCase(Title)) 
      { 
       LAUNCHAPPLICATION_STATUS="PASS"; 
       LAUNCHAPPLICATION_MESSAGE="EXPECTED BROWSER TITLE:"+Title+", ACTUAL BROWSER TITLE :"+Config.driver.getTitle()+", FOR THE URL:"+URL; 
      } 


     }//end of try 

     catch(Exception generalException) 
     { 
      LAUNCHAPPLICATION_STATUS="FAIL"; 
      LAUNCHAPPLICATION_MESSAGE= "EXPECTED BROWSER TITLE:"+Title+", ACTUAL BROWSER TITLE :"+Config.driver.getTitle()+", FOR THE URL:"+URL+". DETAILS:Exception Occoured:"+generalException.getLocalizedMessage(); 

     } 
    } 

    //@BeforeTest 
    //public void f1(ITestContext ctx) { 

     // if(ctx.getCurrentXmlTest().getParameter("EXECUTE").equals("NO")) 
      // { 
      // throw new SkipException("skiping this test case"); 
      //} 


} 

我通過testng.I執行不知道爲什麼,當獲得空指針異常的錯誤在上面的代碼?當我經過的JUnit執行它工作正常?因此,任何幫助?我想,這可能發生,因爲我已經宣佈LAUNCHAPPLICATION_STATUS = NULL;或任何其他問題。在TestNGContentHandler.endElement()這是一個非常強大的暗示,這個問題是在XML文件中,而不是你LaunchApplication類中發生

org.testng.TestNGException: java.lang.NullPointerException 
at org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:341) 
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:88) 
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204) 
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175) 
Caused by: java.lang.NullPointerException 
at  org.testng.xml.TestNGContentHandler.xmlClasses(TestNGContentHandler.java:342) 
at org.testng.xml.TestNGContentHandler.endElement(TestNGContentHandler.java:693) 
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(Unknown Source) 
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source) 
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source) 
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source) 
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) 
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) 
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) 
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source) 
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source) 
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) 
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(Unknown Source) 
at javax.xml.parsers.SAXParser.parse(Unknown Source) 
at org.testng.xml.XMLParser.parse(XMLParser.java:39) 
at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:17) 
at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:10) 
at org.testng.xml.Parser.parse(Parser.java:168) 
at org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:311) 
... 3 more 
+1

你沒有標註,你得到的異常,也沒有顯示實際的堆棧跟蹤。沒有這些細節很難提供幫助。 – RealSkeptic

+0

@RealSkeptic檢查現在我有現在標記錯誤。 – Ab123

回答

2

的異常。這可能是值得提起針對TestNG的錯誤在這種情況下,提供更清晰的錯誤信息,這取決於這個問題最終被。

嘗試創建一個基本的「Hello World」 @Test,這樣就可以驗證TestNG的(和你的TestNG的配置)運行在所有。如果失敗,我們知道這是一個配置問題。否則,如果它工作,你可以縮小到具體的問題測試。