【发布时间】:2014-11-24 17:04:34
【问题描述】:
我正在为我的项目使用 selenium 和 testNG 框架。 现在发生的是每个类都打开一个浏览器然后运行它的方法,例如,如果我有五个类,那么五个浏览器将同时打开然后运行测试。 我想在开始时打开浏览器一次并运行所有方法然后关闭它。
public class openSite
{
public static WebDriver driver;
@test
public void openMain()
{
System.setProperty("webdriver.chrome.driver","E:/drive/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://vtu.ac.in/");
}
@test
//Clicking on the first link on the page
public void aboutVTU()
{
driver.findElement(By.id("menu-item-323")).click();
}
@Test
//clicking on the 2nd link in the page
public void Institutes()
{
driver.findElement(By.id("menu-item-325")).click();
}
现在我想要的是testNG应该打开一次浏览器并打开一次vtu.ac.in然后执行关于VTU和Institutes的方法并给我结果
【问题讨论】:
-
删除
driver在openMain()方法中再次实例化。您已经将其声明为static。使用@BeforeClass 方法启动webdriver。其余在@Test。通过这个testng.org/doc/documentation-main.html#annotations -
我试过了,我得到空指针异常
-
从这行 `WebDriver driver = new ChromeDriver();` 中删除
webdriver,这是导致空指针异常的行
标签: java selenium selenium-webdriver automation testng