Eclipse TestNG测试用例中NullPointerException的解决方案
我试图通过使用外部excel工作表中的多组登录证明登录Facebook,使用Eclipse TestNG测试用例中NullPointerException的解决方案,eclipse,excel,selenium-webdriver,testng,selenium-chromedriver,Eclipse,Excel,Selenium Webdriver,Testng,Selenium Chromedriver,我试图通过使用外部excel工作表中的多组登录证明登录Facebook,使用TestNG,但在代码之间,我写的东西抛出: FAILED: f java.lang.NullPointerException at DataDriven.loginRetesting.f(loginRetesting.java:31) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.Nat
TestNG
,但在代码之间,我写的东西抛出:
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)
代码:
@测试
public void f()引发异常{
FileInputStream fi=新的FileInputStream(“E:\\workspace99\\SeleniumAutomations\\testdata\\LoginData.xls”);
工作簿w=Workbook.getWorkbook(fi);
表s=w.getSheet(0);
对于(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”))。单击();
睡眠(1000);
if(selenium.isElementPresent(“id=userNavigationLabel”)){
//driver.findElement(By.cssSelector(“span.gb_V.gbii”))。单击();
driver.findElement(By.id(“userNavigationLabel”))。单击();
driver.findElement(By.cssSelector(“input.uiLinkButtonInput”))。单击();
睡眠(1000);
}
否则{
System.out.println(“无效证书”);
driver.findElement(By.id(“email”)).clear();
driver.findElement(By.id(“pass”)).clear();
}
}
}
我找不到问题出在哪里?那么如何解决呢 堆栈跟踪说问题出在代码的第31行,但您的代码片段不允许我们确定第31行在哪里
您正在呼叫“selenium.isElementPresent”。“selenium”是否可以为空?空指针表示变量的预期值为空 请检查文件是否存在于指定路径“E:\workspace99\SeleniumAutomations\testdata\LoginData.xls”处,然后从文件中正确获取值。首先,不要在方法声明中使用“抛出异常”。相反,在方法内部有一个带有“notnull断言”的try块 请记住,您永远不希望在测试方法中抛出典型异常,因为它会过早地将您退出测试生命周期,并跳过@After阶段。@Before注释方法中引发的异常可能会导致异常 因此,您想要做的是失败断言。因此,如果您有时会遇到异常情况,请接受异常(不要抛出它),并使用断言来处理它 例如:
try {
} catch ( NullPointerException e ) {
Assert.assertTrue( "There was an error in blah.", false );
}
此外部文件路径是有效路径
try {
} catch ( NullPointerException e ) {
Assert.assertTrue( "There was an error in blah.", false );
}