我正在尝试测试Gmail的登录功能。但它显示异常错误“java.lang.NullPointerException”。代码如下:
package gmail;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class Gmail {
WebDriver driver;
@Before
public void setup() {
System.setProperty("webdriver.chrome.driver","E://chromedriver.exe");
driver=new ChromeDriver();
driver.get("https://accounts.google.com/");
driver.manage().timeouts().implicitlyWait(300, TimeUnit.SECONDS);
}
@After
public void quit() {
driver.manage().deleteAllCookies();
driver.quit();
}
@Test()
public void login() {
WebElement txtUserName=driver.findElement(By.name("Email"));
txtUserName.sendKeys("abc@gmail.com");
WebElement txtPassword=driver.findElement(By.name("Passwd"));
txtPassword.sendKeys("abcd123");
WebElement btnLogin=driver.findElement(By.name ("signIn"));
btnLogin.submit();
}
}
错误:
FAILED: login
java.lang.NullPointerException
at gmail.Gmail.login(Gmail.java:33)
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:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:335)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:330)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
=============================================== 默认测试
答案 0 :(得分:3)
您的测试注释不属于TestNG。
替换
@Before with @BeforeTest
@After with @AfterTest
@Test should point TestNG library
答案 1 :(得分:1)
在此代码片段中@Test注释属于TestNg框架和@Before,@ After注释属于Junit框架。 Eclipse将显示run作为TestNG测试(因为Test annotation是从TestNG库导入的)如果你将它作为TestNG运行它不会执行@Before和@After(因为它们属于Junit框架)因此驱动程序变量是没有初始化,所以我们得到NullPointer异常
要么我们应该导入Junit测试注释并运行Junit测试或更改@After& @Before到@AfterTest& @BeforeTest并作为TestNG测试运行
答案 2 :(得分:1)
"scripts": {
"prestart": "git pull && npm install",
"start": "node ./bin/www"
}
答案 3 :(得分:0)
始终检查您在代码中使用的import
语句,确保它始终是所需库中的语句。可能存在同一方法退出来自不同库的多个类的情况。
您在import
使用JUnit
语句代替TestNg
。
<强>解决方案: - 强>
@AfterTest
代替@After
注释。@BeforeTest
代替@Before
注释。应该有帮助!