我已經寫了一個簡單的Selenium TestNG腳本,但在@AfterTest
方法中得到了一個錯誤java.lang.NullPointerException
。你能幫忙嗎?Selenium TestNG:driver.close()and driver.quit()
public class TestngDemo1
{
public WebDriver driver;
public String url="https://www.guru99.com/";
@Test
public void LaunchURL() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:/Users/AB28488/Desktop/javawork
space/TestNGProject/drivers/chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get(url);
Thread.sleep(2000);
String eTitle="Meet Guru99 - Free Training Tutorials & Video for IT Courses";
String aTitle=driver.getTitle();
Reporter.log(aTitle);
Thread.sleep(3000);
Assert.assertEquals(aTitle, eTitle);
Reporter.log("This will print if titles match!",true);
}
@BeforeMethod
public void BeforeMethod() {
Reporter.log("Before Method");
}
@AfterMethod
public void afterMethod() {
Reporter.log("After method",true);
}
@AfterTest
public void quitDriver() {
driver.quit();
}
}
公共WebDriver驅動程序; &WebDriver driver = new ChromeDriver();爲什麼要創建兩個驅動程序實例? – Pradeep
在你測試方法之前添加下面的代碼driver = new ChromeDriver();而不是實例化內部測試方法 – Pradeep
將您的課程轉換爲testng – zsbappa