In this article, we would discuss about the scope of multiple driver instances and their usability perspective. We would understand the entire concept with the appropriate examples and code samples.

By the term, “Multiple WebDriver Instances”, we mean to say that we create more than one Webdriver instances and launch those using different or same browser specific drivers.

Why do we need multiple WebDriver instances?

We may require such configuration when we have master slave configuration.

–          Master – Slave Setup – In such setups, if we want to execute the test suite on slave setup but a few of the operations are backed up only on Master. Thus, when the user is required to communicate with two different applications in order to accomplish one whole operation, we are tempt to use multiple WebDriver configuration.

1Thus, when multiple web based applications are involved to accomplish a particular test scenario, it is advisable to create multiple WebDriver instances each dedicated to a particular application.

Now let us understand the concept with the help of the code snippet.

Before motioning towards the code, let us take a moment to introduce you with the two applications we have chosen to illustrate the concept.

–          Gmail.com

–          Google.com

I hope the application names are self explanatory and need no introduction.

Code Overview

We have created the following two classes:

–          SwitchDriver: SwitchDriver is a test class that contains the actual test scenario

–          Generic: Generic is a java class that we have used to instantiate the driver objects, launching the browser, opening applications on the instances or any other setting related stuff. We can moreover use the class to create common methods applicable to application one as well as application two and thus, just by passing the driver instance, the system would realize which application to invoke.

SwitchDriver Class

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class SwitchDriver {
    WebDriver driver;
    WebDriver driverSwitch = null;
    Generic obj;
    public static String URL1 = "https://google.com";
    public static String URL2 = "https://gmail.com";

    /**
     * Set up browser settings
     */
    @Before
    public void setUp() {
        // Create Objects
        obj = new Generic();
        // Go to google.com
        if (URL1 != null)
            driver = obj.getDriver(URL1, "firefox");

        // Go to gmail.com
        if (URL2 != null) {
            driverSwitch = obj.getDriver(URL2, "firefox");
            if (driver == null) {// If only gmail.com
                driver = driverSwitch;
                driverSwitch = null;
            }
        }
    }

    /**
     * Test
     */
    @Test
    public void testSwitchingDriver() {
        // Entering text in the first driver instance
        driver.findElement(By.id("gbqfq")).sendKeys(
                "Test Switching between two instances of Driver");
        // Entering text in the second driver instance
        driverSwitch.findElement(By.id("Email")).sendKeys(
                "Tesing switching on Gmail.com");

    }

    /**
     * Tear down the setup after test completes
     */
    @After
    public void tearDown() {
        // Closing both the browser windows
        // Accessing the generic method by passing the driver instance with
        // which the suer wants to communicate
        if (driver != null) {
            obj.quitDriver(driver);
        }
        if (driverSwitch != null) {
            obj.quitDriver(driverSwitch);
        }
    }
}

Generic Class

import java.io.File;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

public class Generic {
    WebDriver driver;

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

    /**
     * @param appUrl The application url under test    
     * @param browser The browser that needs to be launched
     * @return driver the current driver    
     */
    public WebDriver getDriver(String appUrl, String browser) {
        getBrowser(browser);
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.get(appUrl);
        return driver;
    }
    
    /**
     * @param browser The browser that needs to be launched    
     */
    private void getBrowser(String browser) {
        File ieDriver = new File("C:/workspace/lib/IEDriverServer.exe");
        System.setProperty("webdriver.ie.driver", ieDriver.getAbsolutePath());
        DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
        capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
        DesiredCapabilities dc=new DesiredCapabilities();
        dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR,UnexpectedAlertBehaviour.IGNORE);
        

        if (browser.equalsIgnoreCase("Firefox")) {
            driver = new FirefoxDriver(dc);
        } else if (browser.equalsIgnoreCase("IE")) {
            driver = new InternetExplorerDriver();
        } else {
            System.out.println("Browser cannot be launched");
        }
    }
    
    /**
     * @param drv The driver instance on which the action needs to be performed    
     */
    public void quitDriver(WebDriver drv) {
        drv.quit();
    }
}

Conclusion:

I hope the tutorial made a clear impression on when to create multiple WebDriver instances. We also showed the working example in order to explain the concept. There can be various alterations that can be incorporated so as to customize it based on our requirements.

Share this:

One thought on “Switching between multiple WebDriver instances

Leave a Reply

Your email address will not be published. Required fields are marked *