我是 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\AppData\Local\Temp\testng-eclipse--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\Default 套件存在:true 配置失败:@AfterMethod AfterTest java.lang.NullPointerException at TestNGPackage.ParameterizationTest.AfterTest( ParameterizationTest.java:60) 在 sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 在 java.lang.reflect.Method org.testng.internal.MethodInvocationHelper 的 .invoke(未知来源)。org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:515) 的 invokeMethod(MethodInvocationHelper.java:100) org.testng.internal.Invoker.invokeConfigurations(Invoker.java:216) 的 org.testng.internal.Invoker .invokeMethod(Invoker.java:712) 在 org.testng.internal.invoker.invokeTestMethods(Invoker.java:1129) 在 org.testng.internal.Invoker.invokeTestMethod(Invoker.java:811)。 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) 在 org.testng.SuiteRunner.runTest(SuiteRunner.java:366) 在 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) 在 org.testng.TestNG.run(TestNG.java:1076) 在 org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126) 在 org.testng.remote.RemoteTestNG.initAndRun( RemoteTestNG.java:152) 在 org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:57) 跳过配置:@BeforeMethod beforeTest 跳过配置:@AfterMethod AfterTest 跳过配置:@BeforeMethod beforeTest 跳过配置:@AfterMethod AfterTest 失败:Login("manjeet@wtechspaces.in", "p@ssw0rd") java.lang.NullPointerException at TestNGPackage.ParameterizationTest.Login(ParameterizationTest.java:20 ) 在 sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 在 java.lang.reflect.Method.invoke(Unknown Source) ) 在 org.testng.internal.Invoker.invokeTestMethod(Invoker.java: org.testng.internal.Invoker.invokeMethod(Invoker.java:646) 811)在组织。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 org.testng.TestRunner.run(TestRunner.java:600) 的 .testng.TestRunner.privateRun(TestRunner.java:746) org.testng.SuiteRunner 的 org.testng.SuiteRunner.runTest(SuiteRunner.java:366)。 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) org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) org.testng.TestNG.runSuitesSequentially(TestNG.java:1264) 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) 跳过:登录("ho@baskin. com", "welcome123") 跳过:登录("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 pass=0 failed=0 skipped=0] 花费的时间:36 ms [TestNG] org.testng.reporters.XMLReporter@5594a1b5 花费的时间:30 ms [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\Default 套件存在:true [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old \Default suite\Default test.properties [Utils] 目录 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] 目录 C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default 套件存在:true [Utils]尝试创建 C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite\main.html [Utils] 目录 C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default 套件存在: true [Utils] 尝试创建 C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite\groups.html [Utils] 目录 C:\Users\Ronit\workspace\FirstTestNGProject\test-output\ old\Default 套件存在:true [Utils] 正在尝试创建 C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default suite\classes.html [Utils] 目录 C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default 套件存在:true [Utils] 正在尝试创建 C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default 套件\reporter-output。 html [Utils] 目录 C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\Default 套件存在:true [Utils] Attempting to create C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old\ Default suite\methods-not-run.html [Utils] 目录 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] 目录 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] 目录 C:\Users\Ronit\workspace\FirstTestNGProject\test-output\old 存在:true [TestNG] org.testng 花费的时间.reporters.SuiteHTMLReporter@6b71769e:58 毫秒
请帮助我解决我做错了什么,在此先感谢!