我是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]正在运行: C:\ Users \用户Ronit \应用程序数据\本地的\ Temp \ TestNG的月食 - 473949223 \ TestNG的-customsuite.xml [Utils]试图创建C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output \ Default suite \ Default test.xml [Utils]目录C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output \默认套件存在:true FAILED CONFIGURATION:@AfterMethod AfterTest 显示java.lang.NullPointerException 在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) 在org.testng.internal.Invoker.invokeConfigurations(Invoker.java:216) 在org.testng.internal.Invoker.invokeMethod(Invoker.java:712) 在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) 在org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112) 在org.testng.TestRunner.privateRun(TestRunner.java:746) 在org.testng.TestRunner.run(TestRunner.java:600) 在org.testng.SuiteRunner.runTest(SuiteRunner.java:366) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361) 在org.testng.SuiteRunner.privateRun(SuiteRunner.java:319) 在org.testng.SuiteRunner.run(SuiteRunner.java:268) 在org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 在org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1264) 在org.testng.TestNG.runSuitesLocally(TestNG.java:1189) 在org.testng.TestNG.runSuites(TestNG.java:1104) 在org.testng.TestNG.run(TestNG.java:1076) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126) 在org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:152) 在org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:57) SKIPPED CONFIGURATION:@BeforeMethod beforeTest SKIPPED CONFIGURATION:@AfterMethod AfterTest SKIPPED CONFIGURATION:@BeforeMethod beforeTest SKIPPED CONFIGURATION:@AfterMethod AfterTest 失败:登录(" manjeet@wtechspaces.in"," p @ ssw0rd") 显示java.lang.NullPointerException 在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) 在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) 在org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112) 在org.testng.TestRunner.privateRun(TestRunner.java:746) 在org.testng.TestRunner.run(TestRunner.java:600) 在org.testng.SuiteRunner.runTest(SuiteRunner.java:366) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361) 在org.testng.SuiteRunner.privateRun(SuiteRunner.java:319) 在org.testng.SuiteRunner.run(SuiteRunner.java:268) 在org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 在org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1264) 在org.testng.TestNG.runSuitesLocally(TestNG.java:1189) 在org.testng.TestNG.runSuites(TestNG.java:1104) 在org.testng.TestNG.run(TestNG.java:1076) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126) 在org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:152) 在org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:57) 跳过:登录(" ho@baskin.com" ;," welcome123") SKIPPED:登录(" ho@nike.com" ;," welcome123") =============================================== 默认测试 测试运行:3,失败:1,跳过:2 配置失败:1,跳过:4 =============================================== =============================================== 默认套件 总测试运行:3,失败:1,跳过:2 配置失败:1,跳过:4 =============================================== [Utils]尝试创建C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output \ testng-failed.xml [Utils]目录C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output存在:true [Utils]尝试创建C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output \ Default suite \ testng-failed.xml [Utils]目录C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output \默认套件存在:true [TestNG] [FailedReporter传递= 0失败= 0跳过= 0]所花费的时间:36毫秒 [TestNG] org.testng.reporters.XMLReporter@5594a1b5所花费的时间:30毫秒 [Utils]尝试创建C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output \ junitreports \ TEST-TestNGPackage.ParameterizationTest.xml [Utils]目录C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output \ junitreports存在:true [TestNG] org.testng.reporters.JUnitReportReporter@3f3afe78所花费的时间:15毫秒 [TestNG] org.testng.reporters.jq.Main@1cd072a9所花费的时间:228毫秒 [TestNG] org.testng.reporters.EmailableReporter2@34ce8af7所花费的时间:8毫秒 [Utils]尝试创建C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output \ old \ Default suite \ toc.html [Utils]目录C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output \ old \默认套件存在:true [Utils]尝试创建C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output \ old \ Default suite \ Default test.properties [Utils]目录C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output \ old \默认套件存在:true [Utils]试图创建C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output \ old \ Default suite \ index.html [Utils]目录C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output \ old \默认套件存在:true [Utils]试图创建C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output \ old \ Default suite \ main.html [Utils]目录C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output \ old \默认套件存在:true [Utils]尝试创建C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output \ old \ Default suite \ groups.html [Utils]目录C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output \ old \默认套件存在:true [Utils]尝试创建C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output \ old \ Default suite \ classes.html [Utils]目录C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output \ old \默认套件存在:true [Utils]尝试创建C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output \ old \ Default suite \ reporter-output.html [Utils]目录C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output \ old \默认套件存在:true [Utils]试图创建C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output \ old \ Default suite \ methods-not-run.html [Utils]目录C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output \ old \默认套件存在:true [Utils]尝试创建C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output \ old \ Default suite \ testng.xml.html [Utils]目录C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output \ old \默认套件存在:true [Utils]尝试创建C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output \ old \ index.html [Utils]目录C:\ Users \ Ronit \ workspace \ FirstTestNGProject \ test-output \ old exists:true [TestNG] org.testng.reporters.SuiteHTMLReporter@6b71769e所花费的时间:58 ms
请帮助我解决我的错误,提前致谢!
答案 0 :(得分:5)
您发现NullPointerException导致驱动程序在该点之前未初始化。您尝试在beforeTest()方法中初始化WebDriver实例,但它是本地的。要解决此问题,只需在beforeTest()方法中初始化该WebDriver,而不必重新声明为:
driver = new FirefoxDriver();
不是
WebDriver driver = new FirefoxDriver();
第二个NullPointerExpception是由于同样的原因引起的。