我在项目中使用了硒和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,然后执行aboutVTU和Institutes的方法并给我结果

最佳答案

您已经在字段声明中声明了driver的类型。在openMain()中重新声明它是您的问题。它应该看起来像这样。

import static org.testng.Assert.assertNotNull;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class OpenSite {
    private WebDriver driver;

    @BeforeClass(alwaysRun=true)
    public void openMain()
    {
      System.setProperty("webdriver.chrome.driver","/usr/local/bin/chromedriver");
      driver = new ChromeDriver();
      driver.get("http://vtu.ac.in/");
    }

    @Test
    //Clicking on the first link on the page
    public void aboutVTU()
    {
        assertNotNull(driver);
        driver.findElement(By.id("menu-item-323")).click();
    }

    @Test(dependsOnMethods="aboutVTU")
    //clicking on the 2nd link in the page
    public void Institutes()
    {
        assertNotNull(driver);
        driver.findElement(By.id("menu-item-325")).click();
    }
}

10-01 08:08
查看更多