Opening Selenium Webdriver tests in the same window

亡梦爱人 提交于 2019-12-24 16:24:25

问题


I have dozens of Selenium Webdriver tests. I want to run them all at once. How do I run the test so that each test does not open a new Webdriver browser window?


回答1:


You have to initiate/teardown your webdriver in a @BeforeClass/@AfterClass, and use this webdriver in all your test.

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(...):
    }
}

Or make it static in an TestSuite, with the same @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();
    }
}

and

public class Test1 {

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


来源:https://stackoverflow.com/questions/12833847/opening-selenium-webdriver-tests-in-the-same-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!