3

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")));

}
john
  • 709
  • 3
  • 13
  • 25

2 Answers2

3

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
    }

}
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.

Community
  • 1
  • 1
lukeis
  • 1,983
  • 2
  • 19
  • 19
  • 2
    I used to do it that way. Now I think you should create a basic Singleton pattern class that has a getDriver() method that initializes the driver if it equals null but otherwise returns the singleton to the caller. – djangofan Mar 08 '13 at 18:35