在我的TestNGtesting用例中解决NullPointerException

我试图通过使用TestNG从外部Excel表中使用多组login代码loginFacebook,但在代码之间我写了抛出:

 FAILED: f java.lang.NullPointerException at DataDriven.loginRetesting.f(loginRetesting.java:31) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84) at org.testng.internal.Invoker.invokeMethod(Invoker.java:714) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) at org.testng.TestRunner.privateRun(TestRunner.java:767) at org.testng.TestRunner.run(TestRunner.java:617) at org.testng.SuiteRunner.runTest(SuiteRunner.java:334) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) at org.testng.SuiteRunner.run(SuiteRunner.java:240) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224) at org.testng.TestNG.runSuitesLocally(TestNG.java:1149) at org.testng.TestNG.run(TestNG.java:1057) at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175) 

码:

 @Test public void f() throws Exception{ FileInputStream fi=new FileInputStream("E:\\workspace99\\SeleniumAutomations\\testdata\\LoginData.xls"); Workbook w=Workbook.getWorkbook(fi); Sheet s=w.getSheet(0); for (int i = 1; i < 8; i++) { driver.findElement(By.id("email")).sendKeys(s.getCell(0,i).getContents()); driver.findElement(By.id("pass")).sendKeys(s.getCell(1,i).getContents()); driver.findElement(By.id("u_0_1")).click(); Thread.sleep(1000); if (selenium.isElementPresent("id=userNavigationLabel")) { //driver.findElement(By.cssSelector("span.gb_V.gbii")).click(); driver.findElement(By.id("userNavigationLabel")).click(); driver.findElement(By.cssSelector("input.uiLinkButtonInput")).click(); Thread.sleep(1000); } else{ System.out.println("invalid cridential"); driver.findElement(By.id("email")).clear(); driver.findElement(By.id("pass")).clear(); } } } 

我无法找出问题出在哪里? 所以如何解决它。

首先,不要在方法声明中使用“throws Exception”。 相反,有一个“not null断言”方法内的try块。

请记住,您永远不想在testing方法中抛出典型的exception,因为它会让您太早退出testing生命周期并跳过@After阶段。 在@Before注释的方法中引发的exception可能会导致SkipAssertion。

所以,你想要做的是失败的断言。 所以,如果您有时会发生exception情况,请吞下exception(不要抛出)并使用断言来处理exception。

例如:

 try { } catch ( NullPointerException e ) { Assert.assertTrue( "There was an error in blah.", false ); } 

空指针表示variables的执行值为空。

请检查文件是否存在于指定path“E:\ workspace99 \ SeleniumAutomations \ testdata \ LoginData.xls”中,并从文件中正确获取该值。

堆栈跟踪说,问题是你的代码中的第31行,但你的代码段不允许我们找出第31行。

你正在调用“selenium.isElementPresent”。 “selenium”可以吗?

Interesting Posts