Java Selenium TestNG:driver.close()和driver.quit()
我编写了一个简单的Selenium TestNG脚本,如下所示,但在Java Selenium TestNG:driver.close()和driver.quit(),java,selenium,Java,Selenium,我编写了一个简单的Selenium TestNG脚本,如下所示,但在@postest方法中得到一个错误java.lang.NullPointerException。你能帮忙吗 public class TestngDemo1 { public WebDriver driver; public String url="https://www.guru99.com/"; @Test
@postest
方法中得到一个错误java.lang.NullPointerException
。你能帮忙吗
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();
}
}
因为在测试中,您定义了本地可见的驱动程序
public class TestngDemo1
{
// This is your "global" driver
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");
// This is not the same driver you have defined above as a class field
WebDriver driver= new ChromeDriver();
}
@AfterTest
public void quitDriver()
{
// Here you're trying to call method of an object that does not exist.
driver.quit();
}
}
尝试使用以下方法:
public class TestngDemo1
{
// This is your "global" driver
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");
driver= new ChromeDriver();
}
@AfterTest
public void quitDriver()
{
driver.quit();
}
}
使用以下代码,它将为您工作:
public class TestngDemo1
{
public WebDriver driver;
public String url="https://www.guru99.com/";
@Test
public void LaunchURL() throws InterruptedException
{
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);
}
@BeforeTest
public void initDriver()
{
System.setProperty("webdriver.chrome.driver","C:/Users/AB28488/Desktop/javawork
space/TestNGProject/drivers/chromedriver.exe");
driver= new ChromeDriver();
}
@AfterTest
public void quitDriver()
{
driver.quit();
}
}
为此,我创建了三个类:GetProperties、BaseTest和Tests。GetProperties读取config.properties文件并返回java.util.properties以及键和值(url,毫秒) BaseTest打开和关闭浏览器
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class BaseTest extends GetProperties{
public static FirefoxDriver geckoDriver;
public static ChromeDriver chromeDriver;
@BeforeClass
public static void driversInit() throws Exception {
System.setProperty("webdriver.gecko.driver", "your/path/geckodriver");
geckoDriver = new FirefoxDriver();
System.setProperty("webdriver.chrome.driver", "your/path/chromedriver");
chromeDriver = new ChromeDriver();
}
@AfterClass
public static void driversQuit() throws Exception {
geckoDriver.quit();
chromeDriver.quit();
}
}
测试运行测试
import org.junit.Test;
public class Tests extends BaseTest{
@Test
public void testChromeOpenURL() throws InterruptedException {
chromeDriver.get(url);
Thread.sleep(millis*10);
}
@Test
public void testFirefoxOpenURL() throws InterruptedException {
geckoDriver.get(url);
Thread.sleep(millis*10);
}
}
公共网络驱动程序WebDriver驱动程序=新的ChromeDriver();为什么要创建两个驱动程序实例。?在测试方法之前添加以下代码驱动程序=new ChromeDriver();不要实例化内部测试方法将类转换为TestNG,请阅读,特别是关于(MCVE)的部分,这将帮助您调试自己的程序并自行解决问题。如果您这样做了,但仍然卡住了,您可以回来发布您的MCVE,您尝试了什么,以及执行结果,包括任何错误消息,以便我们可以更好地帮助您。谢谢!你能给我正确的代码吗?我不知道什么是正确的代码,因为我不是TestNG专家。让我们详细说明一下。首先尝试将“WebDriver=new ChromeDriver();”更改为“driver=new ChromeDriver();”