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
請幫我什麼,我做錯了,在此先感謝!