0
votes

I have 3 test classes consisting of multiple test methods that I want to run in parallel. I'm using ThreadLocal for isolating webdriver instances per thread. When I run the tests in sequential manner everything looks fine but problem arises when I run them in parallel. Below is my suite file

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="platform" parallel="classes" thread-count="5">
    <test name="platform">
        <classes>
            <class name="com.sat.platform.mobile.PlatformMobileIdCaptureMonitoringWf11"></class>
            <class name="com.sat.platform.mobile.PlatformMobileIdVerificationMonitoringWf2"></class>
            <class name="com.sat.platform.mobile.PlatformMobileIdandIVMonitoringWf3"></class>
            <class name="com.sat.platform.mobile.PlatformMobileLivenessMonitoringWf6"></class>
            <class name="com.sat.platform.mixed.PlatformMixedIdSimilarityMonitoringWf2and5"></class>
        </classes>
    </test>
</suite>

I'm initializing Webdriver in @BeforeClass in BrowserClient.java as below.

    protected WebDriver driver;
    private static int implicitWaitTime;
    private static int explicitWaitTime;
    private static int fluentWaitTime;
    private static int pollingTime;
    protected static WebDriverWait explicitWait;
    protected static Wait<WebDriver> fluentWait;
    private static String browser;
    protected static Browsers browsers;

    static {
        Properties prop = new Properties();
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        InputStream stream = loader.getResourceAsStream("browser.properties");
        try {
            prop.load(stream);
        } catch (IOException e) {
        }
        implicitWaitTime = Integer.parseInt(prop.getProperty("browser.implicit.wait.timeout"));
        explicitWaitTime = Integer.parseInt(prop.getProperty("browser.explicit.wait.timeout"));
        fluentWaitTime = Integer.parseInt(prop.getProperty("browser.fluent.wait.timeout"));
        pollingTime = Integer.parseInt(prop.getProperty("browser.wait.polling.time"));
        browser = System.getProperty("browser");
    }

    @BeforeClass
    public void initializeEnv() throws MalformedURLException {
        driver = BrowserFactory.createInstance(browser, implicitWaitTime);
        DriverFactory.getInstance().setDriver(driver);
        driver = DriverFactory.getInstance().getDriver();
        explicitWait = new WebDriverWait(driver, explicitWaitTime);
        fluentWait = new FluentWait(driver).withTimeout(Duration.of(fluentWaitTime, SECONDS))
                .pollingEvery(Duration.of(pollingTime, SECONDS))
                .ignoring(NoSuchElementException.class);
    }

the used class here i.e BrowserFactory.java

public static WebDriver createInstance(String browser, int implicitWaitTime) throws MalformedURLException {
        WebDriver driver = null;
        Browsers browserEnum = Browsers.valueOf(browser);
        String testVideo = ImageProcessingUtils.getAbsolutePath("digital_copy.mjpeg", false);

        switch (browserEnum) {
            case chrome:
                ChromeOptions options = new ChromeOptions();
                options.addArguments("--use-fake-ui-for-media-stream", "--use-fake-device-for-media-stream",
                        "--use-file-for-fake-video-capture=" + testVideo, "--start-maximized");
                driver = new ChromeDriver(options);
                break;

            case firefox:
                FirefoxProfile firefoxProfile = new FirefoxProfile();
                firefoxProfile.setPreference("media.navigator.permission.disabled", true);
                firefoxProfile.setPreference("media.navigator.streams.fake", true);
                firefoxProfile .setPreference("browser.private.browsing.autostart", false);
                FirefoxOptions firefoxOptions = new FirefoxOptions();
                firefoxOptions.setProfile(firefoxProfile);
                driver = new FirefoxDriver(firefoxOptions);
                break;
     }
}

DriverFactory.java

public class DriverFactory {

    private static DriverFactory instance = new DriverFactory();
    private static ThreadLocal<WebDriver> driver = new ThreadLocal<>();
    private static List<WebDriver> driversList = new ArrayList();

    private DriverFactory(){

    }

    public static DriverFactory getInstance() {
        return instance;
    }

    public WebDriver getDriver(){
        WebDriver localDriver = driver.get();
        driversList.add(localDriver);
        return localDriver;
    }

    public void setDriver(WebDriver driver){
        this.driver.set(driver);
    }

    public static void removeDriver(){
        for(WebDriver driver : driversList) {
            driver.quit();
        }
    }
}

And my test classes extends BrowserClient.java where i can use the driver directly. One of the common method in all 3 test classes is merchant_gets_oauth_token() as shown below. The problem is when test suite is run, 3 firefox browsers are open in parallel and all of them navigates to the login page but only 1 and sometimes 2 of the tests passes while the 3rd one fails (unable to login).

    @Test
    public void merchant_gets_oauth_token() {
        OAuth2Client client = new OAuth2Client();
        String loginUrl = DslConfigFactory.getEnvConfig("portal.customer.url");
        driver.get(loginUrl);
        CustomerPortalLoginPage loginPage = new CustomerPortalLoginPage(driver);
        log.info("----------merchant logging to customer portal to get oauth token----------");
        loginPage.login(merchantUser.getEmail(), merchantUser.getPassword());
        CustomerPortalHomePage homePage = new CustomerPortalHomePage(driver);
        homePage.clickOnSettings();
        CustomerPortalSettingsPage settingsPage = new CustomerPortalSettingsPage(driver);
        settingsPage.clickOnApiCredentials();
        CustomerPortalApiCredentialsPage apiCredentialsPage = new CustomerPortalApiCredentialsPage(driver);
        clientCredentials = apiCredentialsPage.getOauth2ClientCredentials();
        oauthToken = client.getOauthToken(clientCredentials.get("token"), clientCredentials.get("secret"));
    }

I have been struggling with this problem for a while and had looked up lot of online resources without help. Maybe somebody here is able to do the RCA. Thanks in Advance!!

1

1 Answers

-1
votes
public static void removeDriver(){
        for(WebDriver driver : driversList) {
            driver.quit();
        }
    }

Here, replace driver.quit() with driver.close().
driver.close() method it will close only current driver. driver.close() closes all drivers. 

When first instance executes removeDriver(), it will closes all windows/drivers. Then next instance try to execute removeDriver(),there driver is not available because it is already closed by previous instance of driver.so you get error.