我正在尝试使用TestNG作为测试框架进行Selenium测试自动化。为此,我使用了一个Page Object模式来为我正在编写测试的网站的每个页面建模。
我有两个java类
SignInPage.java
public class SignInPage {
private WebDriver driver;
@FindBy(id = "username")
private WebElement usernameTextbox;
@FindBy(id = "password")
private WebElement passwordTextbox;
@FindBy(xpath = "//*[@id=\"left\"]/div[3]/div/form/input[3]")
private WebElement loginButton;
public SignInPage(WebDriver driver) {
this.driver = driver;
driver.get("www.somewebsite.com");
}
public HomePage performLogin(String username, String password){
usernameTextbox.sendKeys(username);
passwordTextbox.sendKeys(password);
loginButton.click();
return PageFactory.initElements(driver, HomePage.class);
}
}
TestLogin.java
public class TestLogin {
public WebDriver driver;
@BeforeClass
public void setup(){
System.setProperty("webdriver.chrome.driver", "path_to_chromedriver.exe");
driver = new ChromeDriver();
}
@Test
public void TestSignIn(){
SignInPage signInPage = new SignInPage(driver);
HomePage homePage = signInPage.performLogin("someusername","somepassword");
}
}
我的问题是每当在TestLogin.java的@Test中调用signInPage.performLogin()时,都会遇到空指针异常。
java.lang.NullPointerException
at com.tipidpc.webpages.SignInPage.performLogin(SignInPage.java:30)
at com.tipidpc.tests.TestLogin.TestSignIn(TestLogin.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:645)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:851)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1177)
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:756)
at org.testng.TestRunner.run(TestRunner.java:610)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:387)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1218)
at org.testng.TestNG.runSuites(TestNG.java:1133)
at org.testng.TestNG.run(TestNG.java:1104)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:127)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
答案 0 :(得分:1)
我解决了这个问题。由于我使用TestNG框架来运行我的测试,因此必须存在正确的导入才能使测试脚本正常运行。
我的所有驱动程序初始化都在@BeforeClass方法中完成,但它使用import org.junit.BeforeClass。解决方案是将其更改为org.testng.annotations.BeforeClass。