繁体   English   中英

如何为不同的类重用现有的webdriver实例

[英]How to reuse the exiting instance of webdriver for different classes

我已经解决了本网站上的存在问题,但无法解决以下问题

工具:Junit,IntelliJ,Java语言

//设置类

public class SetUpTest {
public static WebDriver driver;

    @BeforeClass
public static void setUpTest() throws InterruptedException
{
    driver=new FirefoxDriver();

    driver.get("http://www.ABC.co.uk");
    Thread.sleep(5000);

}


//Test1
public class HomePageTest extends SetUpTest {

    @Test
    public void titleTest()

    {
        assertTrue(driver.getTitle().startsWith("ABC"));

//Test2
 public void merchandisingTest() throws InterruptedException {
    @Test
    public void merchandisingTest()
    {

         driver.navigate().to("http://ABC.co.uk/deals");
 assertThat(driver.getPageSource(),containsString("deals"));

我尝试使用@TestBefore运行测试,但仍然打开了两个浏览器窗口,一个用于Test1,另一个用于Test2。我也使用了@ Suite.SuiteClasses,但问题仍然存在。

你可以试试这个代码

public class BaseTest{
    public WebDriver driver;
    @BeforeSuite
    public void startDriver(){
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(5l, TimeUnit.SECONDS);
        driver.get("http://www.ABC.co.uk");
    }
}

public class HomePageTest extends BaseTest{
    @Test
    public void titleTest(){
        assetTrue(driver.getTitle().startsWith("ABC"));
    }

    @Test
    public void merchanisingTest(){
        driver.navigate().to("http://www.ABC.co.uk/deals");
        assertTrue(driver.getTitle().startsWith("deals"));
    }
}

另外,您正在使用TestNG XML运行此程序,其中@BeforeSuite方法仅被调用一次

暂无
暂无

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

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