Use same web driver throughout selenium suite

笑着哭i 提交于 2019-12-17 17:03:25

问题


I have a the following selenium test suite inheriting from the same base class, how to I have the tests use the same web driver instance when i run the entire test suite?. I also want to run each tests in isolation aswell. I believe this will cut down the time it takes to run the suite considerably.

This test is run from maven that in turn runs each test class.

@RunWith(Suite.class)
@SuiteClasses({
    AdminPortalTestSuite.class,
    DevPortalTestSuite.class,
    CommonTestSuite.class
})
public class SeleniumTestSuite {

}

Baseclass all tests inherit from

@BeforeClass
public static void setUp() throws Exception {

    if (null == baseUrl || !baseUrl.startsWith("https://")) {
        baseUrl = "https://localhost:8443";
    }

    if (null == browser || browser.startsWith("${")) {
        browser = "firefox";
    }
    //retrieve properties including locale.
    retrieveProperties();
    Thread.sleep(4000);
    setUpDriver();
}

@After
public void tearDownAfterTest() {
    openUrl(LIST_PARTNERS);
    adminPortalLogout();
    openUrl(DASHBOARD);
    developerPortalLogout();
    driver.manage().deleteAllCookies();
}

@AfterClass
public static void tearDown() throws Exception {
    BaseFunctionalTestCase.driver.quit();
}

test example

public class APApplicationFunctionalTestCase extends BaseFunctionalTestCase {

/**
 * Test validation when creating a new application.
 */
@Test
public void testApplicationValidation() {
    Assume.assumeTrue(preHtml5);

    final String partnerName = randomize("partner");
    //create partner
    createPartnerThroughAP(partnerName);

    adminPortalLogin();
    openUrl(ADD_APPLICATION + partnerName);
    waitForId("applicationView.applicationName");
    findById("submit-button").click();
    waitForId("submit-button");

    //check validation
    assertTrue("Failed to validate application name", 
        isTextPresent(resolveAPMessage("partner", "application.messages", 
            "NotEmpty.applicationEditView.applicationView.applicationName")));

    assertTrue("Failed to validate application username", 
        isTextPresent(resolveAPMessage("partner", "application.messages", 
            "NotEmpty.applicationEditView.applicationView.applicationUserName")));

    assertTrue("Failed to validate application password", 
        isTextPresent(resolveAPMessage("partner", "application.messages", 
            "Password.applicationEditView.applicationView.applicationPassword")));

    assertTrue("Failed to validate application password confirmation", 
        isTextPresent(resolveAPMessage("partner", "application.messages", 
            "Length.applicationEditView.applicationPasswordConfirmation")));

}

回答1:


I'm not great with JUnit... looks like you're trying the solution suggested here: Before and After Suite execution hook in jUnit 4.x

which would suggest you should move your @BeforeClass into your SeleniumTestSuite class.




回答2:


This is how I did it. In SeleniumTestSuite, I added a static WebDriver and instantiate it in a setUp() method annotated with @BeforeClass. Then, in the Base class that all of my selenium tests inherit from, I added a getDriver() method, that will try to get the static driver from SeleniumTestSuite. If that driver is null, then a new one gets instantiated and returned. Thus, when the selenium test classes are running via the suite, they will use the driver from SeleniumTestSuite, and when they are running individually, they will use their own driver.

SeleniumTestSuite:

@RunWith(Suite.class)
@SuiteClasses({
    AbcSeleniumTest.class,
    XyzSeleniumTest.class
})
public class SeleniumTestSuite {

    private static WebDriver driver;

    @BeforeClass
    public static void setUp() {
        driver = new FirefoxDriver();
    }

    //driver getter/setter

}

BaseSeleniumTest:

public abstract class BaseSeleniumTest {

    public WebDriver getDriver() {
        WebDriver driver = SeleniumTestSuite.getDriver();
        if(driver != null) {
            return driver;
        }

        return new FirefoxDriver();
    }

}

AbcSeleniumTest:

public class AbcSeleniumTest extends BaseSeleniumTest {

    @Test
    public void testAbc() {
        WebDriver driver = getDriver();

        // test stuff
    }

}


来源:https://stackoverflow.com/questions/8990683/use-same-web-driver-throughout-selenium-suite

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