简体   繁体   English

如何在testng for selenium中使浏览器的打开(在一个浏览器中全部测试)静态

[英]How to make the opening of browser(all test in one browser) static in testng for selenium

I am using selenium and testNG framework for my project. 我在项目中使用了硒和testNG框架。 Now what is happening is each class is opening up a browser and then run its methods, eg, if I have five classes, then five browsers will open simultaneously and then run the tests. 现在发生的事情是每个类都打开一个浏览器,然后运行其方法,例如,如果我有五个类,则五个浏览器将同时打开,然后运行测试。 I want to Open Browser at the start once and run all the methods and then close it. 我想一次启动打开浏览器并运行所有方法,然后将其关闭。

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();
}

Now What I want is the testNG should open browser once and open vtu.ac.in once and then execute the methods aboutVTU and Institutes and give me the result 现在我想要的是testNG应该打开浏览器一次并打开vtu.ac.in,然后执行aboutVTU和Institutes的方法,并给我结果

You already declared the type for driver in your field declarations. 您已经在字段声明中声明了driver的类型。 Redeclaring it in openMain() is your problem. openMain()openMain()声明它是您的问题。 It should look like this. 它应该看起来像这样。

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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM