Use the same web driver in selenium set

I have the following selenium test suite inheriting from the same base class, how do I need the tests to use the same instance of the web driver when I run the entire test suite ?. I also want to run each test separately. I believe that this will reduce the time required to run the package significantly.

This test runs from maven, which 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")));

}
+3
source share
2 answers

JUnit... , : junit 4.x

@BeforeClass SeleniumTestSuite.

+1

. SeleniumTestSuite WebDriver setUp(), @BeforeClass. , Base, , getDriver(), SeleniumTestSuite. NULL, . , , SeleniumTestSuite, , .

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
    }

}
+3

Source: https://habr.com/ru/post/1620333/


All Articles