您好我正在尝试使用TestNG运行以下代码,但它给出的所有内容都是java.lang.NullPointerException
错误。
Before
方法得到了解决。之后,它显示以下错误。出了什么问题。请帮忙!
错误:
java.lang.NullPointerException
at lokbo.Login.afterMethod(Login.java:50)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:523)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:224)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:731)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:877)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1201)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:776)
at org.testng.TestRunner.run(TestRunner.java:634)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:425)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:420)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:385)
at org.testng.SuiteRunner.run(SuiteRunner.java:334)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1318)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1243)
at org.testng.TestNG.runSuites(TestNG.java:1161)
at org.testng.TestNG.run(TestNG.java:1129)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
代码:
package lokbo;
import java.util.concurrent.TimeUnit;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
public class Login
{
public WebDriver driver;
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "E:\\RF
AUTOMATION\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
String baseurl= "http://dev.idrottonline.se/";
driver.get(baseurl);
driver.manage().timeouts().implicitlyWait(300, TimeUnit.SECONDS);
String expectedTitle = "Idrottonline2 - Svenskidrott";
String actualTitle="";
actualTitle=driver.getTitle();
if(actualTitle.contentEquals(expectedTitle))
//if(expectedTitle.isDisplayed())
{
System.out.println("Test pass");
}
else
{
System.out.println("Test Fail");
}
}
@Test
public void login() {
driver.findElement(By.id("IdrottOnline-LoginBoxTarget")).click();
driver.findElement(By.id("ioui-access-username")).sendKeys("lagorf01");
driver.findElement(By.id("ioui-access-password")).sendKeys("Dumbo753");
driver.findElement(By.xpath(".//*[@id='ioui-access-login']/fieldset/div[5]/div/button")).click();
}
@AfterMethod
public void afterMethod() {
driver.manage().deleteAllCookies();
driver.quit();
}
}