【问题标题】:How to make the opening of browser(all test in one browser) static in testng for selenium如何在硒的testng中使浏览器的打开(一个浏览器中的所有测试)静态
【发布时间】: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的方法并给我结果

【问题讨论】:

  • 删除driveropenMain() 方法中再次实例化。您已经将其声明为static。使用@BeforeClass 方法启动webdriver。其余在@Test。通过这个testng.org/doc/documentation-main.html#annotations
  • 我试过了,我得到空指针异常
  • 从这行 `WebDriver driver = new ChromeDriver();` 中删除 webdriver,这是导致空指针异常的行

标签: java selenium selenium-webdriver automation testng


【解决方案1】:

您已经在字段声明中声明了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();
    }
}

【讨论】:

    猜你喜欢
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    • 2021-01-23
    • 2019-04-12
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    相关资源
    最近更新 更多