使用 TestNG 在 Selenium WebDriver 中获取空指针异常
Getting Null Pointer Exception in Selenium WebDriver using TestNG
我是 Selenium 的新手,当 运行 下面的脚本出现空指针异常时,一旦调用的站点加载,测试就会停止 运行。我无法理解异常的原因。
代码如下:
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]="ho@abc.com";
data[0][1]="welcome23";
//row2
data[1][0]="ho@baskin.com";
data[1][1]="welcome23";
//row3
data[2][0]="ho@nike.com";
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("manjeet@wtechspaces.in", "p@ssw0rd")
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("ho@baskin.com", "welcome123")
SKIPPED: Login("ho@nike.com", "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 org.testng.reporters.XMLReporter@5594a1b5: 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 org.testng.reporters.JUnitReportReporter@3f3afe78: 15 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@1cd072a9: 228 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@34ce8af7: 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 org.testng.reporters.SuiteHTMLReporter@6b71769e: 58 ms
请帮我解决我做错的地方,在此先感谢!
您发现 NullPointerException 导致驱动程序在此之前未初始化。您尝试在 beforeTest() 方法中初始化 WebDriver 实例,但它是本地的。要解决此问题,只需在 beforeTest() 方法中初始化该 WebDriver 而无需重新声明为:
driver = new FirefoxDriver();
不是
WebDriver driver = new FirefoxDriver();
第二个NullPointerExpception也是同样的原因造成的
我是 Selenium 的新手,当 运行 下面的脚本出现空指针异常时,一旦调用的站点加载,测试就会停止 运行。我无法理解异常的原因。
代码如下:
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]="ho@abc.com";
data[0][1]="welcome23";
//row2
data[1][0]="ho@baskin.com";
data[1][1]="welcome23";
//row3
data[2][0]="ho@nike.com";
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("manjeet@wtechspaces.in", "p@ssw0rd") 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("ho@baskin.com", "welcome123") SKIPPED: Login("ho@nike.com", "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 org.testng.reporters.XMLReporter@5594a1b5: 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 org.testng.reporters.JUnitReportReporter@3f3afe78: 15 ms [TestNG] Time taken by org.testng.reporters.jq.Main@1cd072a9: 228 ms [TestNG] Time taken by org.testng.reporters.EmailableReporter2@34ce8af7: 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 org.testng.reporters.SuiteHTMLReporter@6b71769e: 58 ms
请帮我解决我做错的地方,在此先感谢!
您发现 NullPointerException 导致驱动程序在此之前未初始化。您尝试在 beforeTest() 方法中初始化 WebDriver 实例,但它是本地的。要解决此问题,只需在 beforeTest() 方法中初始化该 WebDriver 而无需重新声明为:
driver = new FirefoxDriver();
不是
WebDriver driver = new FirefoxDriver();
第二个NullPointerExpception也是同样的原因造成的