在同一窗口中打开Selenium Webdriver测试

时间:2012-10-11 06:54:15

标签: java browser selenium webdriver

我有几十个Selenium Webdriver测试。我想立刻运行它们。如何运行测试以便每个测试都不会打开新的Webdriver浏览器窗口?

1 个答案:

答案 0 :(得分:3)

您必须在@ BeforeClass / @ AfterClass中启动/拆除您的webdriver,并在所有测试中使用此webdriver。

public class MyTest {

    WebDriver driver;

    @BeforeClass
    public static void setUpClass() {
        driver = new RemoteWebDriver(new URL(hubAddress), capability);
    }

    @AfterClass
    public static void setDownClass() {
         driver.quit();
    }

    @Test 
    public void Test1(){
         driver.get(...);
    }

    @Test 
    public void Test2(){
         driver.get(...):
    }
}

或者在TestSuite中使其保持静态,使用相同的@ BeforeClass / @ AfterClass:

@RunWith(Suite.class)
@SuiteClasses({ Test1.class, Test2.class})
public class MyTestSuite {

    public static WebDriver driver;

    @BeforeClass
    public static void setUpClass() {
        driver = new RemoteWebDriver(new URL(hubAddress), capability);
    }

    @AfterClass
    public static void setDownClass() {
         driver.quit();
    }
}

public class Test1 {

    @Test 
    public void Test1(){
         MyTestSuite.driver.get(...);
    }
}