java - 在 @beforeMethod 中执行 TestNG 类时出现 java.lang.NullPointerException
问题描述
我将 chrome.exe 放在同一项目路径中,但无法使用此路径执行它: src\test\resources\Drivers\chromedriver.exe 所以我将路径设置为“D:\AutomationTask\automationTask\src\test\ resources\Drivers\chromedriver.exe”,昨天运行良好。现在我正在尝试运行该项目,但我在 @Befre 类中收到 java.lang.NullPointerException。
这是我的代码:
package testPackage;
import org.testng.annotations.Test;
import objectModels.AuthenticationPage;
import objectModels.RegistrationPage;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
public class LoginTest {
WebDriver driver;
AuthenticationPage authentication;
RegistrationPage registration;
@Test(priority = 7)
public void loginWithCorrectEmailAndPassword() {
authentication.EnterUserEmailForLogin("ayasalama@outlook.com");
authentication.EnterUserPasswordForLogin("123456");
}
@Test(priority = 0)
public void LoginWithIncorrectEmail() {
authentication.EnterUserEmailForLogin("ayasalama@outlook.eg");
authentication.EnterUserPasswordForLogin("123456");
}
@Test(priority = 1)
public void LoginWithIncorrectPassword() {
authentication.EnterUserEmailForLogin("ayasalama@outlook.com");
authentication.EnterUserPasswordForLogin("12345");
}
@Test(priority = 2)
public void LoginWithEmptyEmailAndPassword() {
authentication.EnterUserEmailForLogin("");
authentication.EnterUserPasswordForLogin("");
}
@Test(priority = 3)
public void LoginWithEmptyEmail() {
authentication.EnterUserEmailForLogin("");
authentication.EnterUserPasswordForLogin("123456");
}
@Test(priority = 4)
public void LoginWithEmptyPassword() {
authentication.EnterUserEmailForLogin("ayasalama@outlook.com");
authentication.EnterUserPasswordForLogin("");
}
@Test(priority = 5)
public void LoginWithInvalidEmailFormat() {
authentication.EnterUserEmailForLogin("ayasalama@gmail");
authentication.EnterUserPasswordForLogin("123456");
}
@Test(priority = 6)
public void LoginWithInvalidPasswordLength() {
authentication.EnterUserEmailForLogin("ayasalama@outlook.com");
authentication.EnterUserPasswordForLogin("123");
}
@BeforeMethod
public void beforeMethod() {
authentication.NavigateToUrl();
}
@BeforeClass
public void beforeClass() {
System.setProperty("webdriver.chrome.driver",
"D:\\AutomationTask\\automationTask\\src\\test\\resources\\Drivers\\chromedriver.exe");
driver = new ChromeDriver();
authentication = new AuthenticationPage(driver);
registration = new RegistrationPage(driver);
}
@AfterClass
public void afterClass() {
driver.close();
}
}
这是testNG跟踪:
java.lang.NullPointerException
at objectModels.AuthenticationPage.<init>(AuthenticationPage.java:26)
at testPackage.LoginTest.beforeClass(LoginTest.java:77)
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:133)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:62)
at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:340)
at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:294)
at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:176)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:122)
at java.util.ArrayList.forEach(Unknown Source)
at org.testng.TestRunner.privateRun(TestRunner.java:770)
at org.testng.TestRunner.run(TestRunner.java:591)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:402)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:396)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)
at org.testng.SuiteRunner.run(SuiteRunner.java:304)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1180)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1102)
at org.testng.TestNG.runSuites(TestNG.java:1032)
at org.testng.TestNG.run(TestNG.java:1000)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
解决方案
推荐阅读
- flutter - jitsi 在构建时遇到颤振崩溃
- cookies - 未发送跨域请求中的 httpOnly cookie
- file - Drupal 上传然后移动不会更新文件列表中的路径或文件名
- python - 循环从保存在多个文件夹中的 CSV 创建多个 JSON 文件
- javascript - 如何将获取响应转换为数组缓冲区?
- python - Python:在 VSCode 中使用 Jupyter 内核
- regex - 在 Go 中使用正则表达式替换不区分大小写的字符串和数组(作为字符串)
- html - 使用 div 的 Colspan、rowspan 和布局
- teradata - Teradata 触发器:两个简单的语法问题
- python-3.x - Python3/Cheetah3:“过滤器”类型的对象没有 len()