2016-09-29 83 views
0

我是Selenium的新手,在运行下面的脚本时,我收到空指针异常,并在站点调用加载后立即停止运行。我无法理解异常的原因。在使用TestNG的Selenium WebDriver中获取空指针异常

package TestNGPackage; 

import java.util.concurrent.TimeUnit; 

import org.openqa.selenium.support.ui.*; 
import org.openqa.selenium.firefox.*; 
import org.openqa.selenium.*; 
import org.testng.annotations.AfterMethod; 
import org.testng.annotations.BeforeMethod; 
import org.testng.annotations.DataProvider; 
import org.testng.annotations.Test; 
import org.testng.annotations.BeforeTest; 

public class ParameterizationTest { 
WebDriver driver; 
    @Test(dataProvider="getdata") 
    public void Login(String Username, String Password) 
    { 
    //WebDriverWait wait= new WebDriverWait(driver,60); 
    //WebElement username= wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username"))); 
    WebElement username=driver.findElement(By.id("username")); 
     WebElement password= driver.findElement(By.id("password")); 
     username.sendKeys(Username); 
     password.sendKeys(Password); 
     WebElement SignIn= driver.findElement(By.tagName("button")); 
     String text = SignIn.getText(); 
     if (text.equalsIgnoreCase("sign in")) { 
      // sign in btn 
      SignIn.submit(); 
     } 
    } 

    @DataProvider(name="getdata") 
    public Object[][] testgetdata() 
    { 
     Object[][] data = new Object[3][2]; 
     //row1 
     data[0][0]="[email protected]"; 
     data[0][1]="welcome23"; 
     //row2 
     data[1][0]="[email protected]"; 
     data[1][1]="welcome23"; 
     //row3 
     data[2][0]="[email protected]"; 
     data[2][1]="welcome23"; 

     return data; 
    } 

    @BeforeMethod 
    public void beforeTest() { 
     WebDriver driver = new FirefoxDriver(); 

     driver.get("http://52.25.202.63/illweb/"); 
    // driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 

    } 

    @AfterMethod 
    public void AfterTest() { 
     //driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
     WebElement logout = driver.findElement(By.linkText("Logout")); 
     logout.click(); 
     //driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
     driver.close(); 

    } 
} 

我收到以下错误:

其代码如下

[TestNG] Running: C:\Users\Ronit\AppData\Local\Temp\testng-eclipse--473949223\testng-customsuite.xml [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\Default suite\Default test.xml [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output\Default suite exists: true FAILED CONFIGURATION: @AfterMethod AfterTest java.lang.NullPointerException at TestNGPackage.ParameterizationTest.AfterTest(ParameterizationTest.java:60) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:100) at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:515) at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:216) at org.testng.internal.Invoker.invokeMethod(Invoker.java:712) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:811) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1129) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112) at org.testng.TestRunner.privateRun(TestRunner.java:746) at org.testng.TestRunner.run(TestRunner.java:600) at org.testng.SuiteRunner.runTest(SuiteRunner.java:366) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319) at org.testng.SuiteRunner.run(SuiteRunner.java:268) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1264) at org.testng.TestNG.runSuitesLocally(TestNG.java:1189) at org.testng.TestNG.runSuites(TestNG.java:1104) at org.testng.TestNG.run(TestNG.java:1076) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:152) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:57) SKIPPED CONFIGURATION: @BeforeMethod beforeTest SKIPPED CONFIGURATION: @AfterMethod AfterTest SKIPPED CONFIGURATION: @BeforeMethod beforeTest SKIPPED CONFIGURATION: @AfterMethod AfterTest FAILED: Login(" [email protected]", " [email protected]") java.lang.NullPointerException at TestNGPackage.ParameterizationTest.Login(ParameterizationTest.java:20) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:100) at org.testng.internal.Invoker.invokeMethod(Invoker.java:646) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:811) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1129) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112) at org.testng.TestRunner.privateRun(TestRunner.java:746) at org.testng.TestRunner.run(TestRunner.java:600) at org.testng.SuiteRunner.runTest(SuiteRunner.java:366) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319) at org.testng.SuiteRunner.run(SuiteRunner.java:268) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1264) at org.testng.TestNG.runSuitesLocally(TestNG.java:1189) at org.testng.TestNG.runSuites(TestNG.java:1104) at org.testng.TestNG.run(TestNG.java:1076) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:152) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:57) SKIPPED: Login(" [email protected]", "welcome123") SKIPPED: Login(" [email protected]", "welcome123") =============================================== Default test Tests run: 3, Failures: 1, Skips: 2 Configuration Failures: 1, Skips: 4 =============================================== =============================================== Default suite Total tests run: 3, Failures: 1, Skips: 2 Configuration Failures: 1, Skips: 4 =============================================== [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\testng-failed.xml [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output exists: true [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\Default suite\testng-failed.xml [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output\Default suite exists: true [TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 36 ms [TestNG] Time taken by [email protected]: 30 ms [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\junitreports\TEST-TestNGPackage.ParameterizationTest.xml [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output\junitreports exists: true [TestNG] Time taken by [email protected]: 15 ms [TestNG] Time taken by [email protected]: 228 ms [TestNG] Time taken by [email protected]: 8 ms [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite\toc.html [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite exists: true [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite\Default test.properties [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite exists: true [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite\index.html [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite exists: true [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite\main.html [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite exists: true [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite\groups.html [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite exists: true [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite\classes.html [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite exists: true [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite\reporter-output.html [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite exists: true [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite\methods-not-run.html [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite exists: true [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite\testng.xml.html [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite exists: true [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\index.html [Utils] Directory C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old exists: true [TestNG] Time taken by [email protected]: 58 ms

请帮我什么,我做错了,在此先感谢!

回答

5

您会发现一个NullPointerException,导致驱动程序在该点之前未得到初始化。您尝试在beforeTest()方法内初始化WebDriver实例,但它是本地的。为了解决这个问题,就初始化beforeTest()方法内的是没有的webdriver作为重新声明:

driver = new FirefoxDriver(); 

WebDriver driver = new FirefoxDriver(); 

第二NullPointerExpception引起由于同样的理由。