我有几十个Selenium Webdriver测试。我想立刻运行它们。如何运行测试以便每个测试都不会打开新的Webdriver浏览器窗口?
答案 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(...);
}
}