Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
115 views
Welcome To Ask or Share your Answers For Others

1 Answer

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
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...