Selenium WebDriver – How To Download And Set Up the Browser Drivers

Overview:

One of the challenges we often face with test automation is – setting up the dependency to run the automated test scripts in all the remote virtual machines. For example, downloading & setting up the required version of browser drivers like chromedriver and adding them to the PATH. Doing this setup manually in every machine could be bit annoying.

I would suggest you to go with dockerized selenium grid approach. But in case, you do not want to use docker, You could include the drivers in your framework itself. That is, you add the required executables and push it to the central repo. [I know, some of you might hate this idea! It might increase your project size etc]. But It ensures that – all the dependency required are part of the project. So you would not miss anything in the remote machine to run the test scripts.

But there is some other better approach as well!

Spacelift:

Spacelift is a library from Arquillian Team!

Spacelift provides a set of tools (and tasks) that can be used to encapsulate execution of any command into block that can be executed asynchronously and repeated if needed. You reuse a set of existing tasks and implement your own tasks.

Spacelift comes with some very basic tools like DownloadTool, UnzipTool which can be used for setting up the browser drivers.

Features:

  • Spacelift executes the tasks in asynchronously.
  • Spacelift can block the execution by using ‘await()’ or ‘awaitAtMost(long , TimeUnit)’
  • Spacelift can execute the given task periodically until some condition / timeout is met.

Add below maven dependency to use Spacelift.

<dependency>
   <groupId>org.arquillian.spacelift</groupId>
   <artifactId>arquillian-spacelift</artifactId>
   <version>1.0.0</version>
</dependency>

Setting up Browser Driver:

Lets consider this class to download chrome driver and unzip.

public class BrowserDrivers {

    private final static String CHROMEDRIVER_URL = "https://chromedriver.storage.googleapis.com/2.29/chromedriver_win32.zip";

    public static void setupChrome() {

        Spacelift.task(DownloadTool.class)
            .from(CHROMEDRIVER_URL)
            .to(System.getProperty("user.dir") + "/chrome.zip")
            .then(UnzipTool.class)
            .toDir(System.getProperty("user.dir") + "/chrome/")
            .execute()
            .await();

        System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/chrome/chromedriver");

    }
}

This setup will ensure that your chrome browser driver is setup in the remote machine. Your test can be as shown here.

public class ChromeTest {

    @BeforeSuite
    public void setupDrivers() {
        BrowserDrivers.setupChrome();
    }

    @Test
    public void test1() {
        //your tests
    }

    @Test
    public void test2() {
        //your tests
    }

}

If you like to setup the drivers this way, Then I would suggest you to use the Factory pattern as mentioned in this article.

We could modify the abstract DriverManager class to include an abstract method ‘setupDriver’ method. Each extending Driver manager class like ChromeDriverManager will imlement the download and unzip using spacelift as shown above.

public abstract class DriverManager {

    protected WebDriver driver;

    protected abstract void setupDriver();
    protected abstract void startService();
    protected abstract void stopService();
    protected abstract void createDriver();

    public void quitDriver() {
        if (null != driver) {
            driver.quit();
            driver = null;
        }
    }

    public WebDriver getDriver() {
        if (null == driver) {
            startService();
            createDriver();
        }
        return driver;
    }
}

Happy Testing & Subscribe 🙂

 

 

Share This:

1 thought on “Selenium WebDriver – How To Download And Set Up the Browser Drivers

  1. Hi,
    it is great to see that you are using Spacelift – it’s really powerful tool.
    Arquillian also provides tools for UI testing: Drone [1] & Graphene [2]
    Drone does exactly what you did – automatically downloads webdriver [3], sets everything up and manages the lifecycle. Graphene is then used for navigation, interaction, waiting, bringing abstractions etc… [4]
    A simple example how to use Drone & Graphene is here [5].
    Another example is using functionality provided by Arquillian – using container & deployment [6]
    Take a look at these projects, I believe that you will find them useful.

    [1] https://github.com/arquillian/arquillian-extension-drone/
    [2] https://github.com/arquillian/arquillian-graphene/
    [3] http://arquillian.org/arquillian-extension-drone/#_automatic_download
    [4] http://arquillian.org/arquillian-graphene/
    [5] https://github.com/MatousJobanek/examples/tree/master/drone-graphene/drone-grahene-simple
    [6] https://github.com/MatousJobanek/examples/tree/master/drone-graphene/drone-graphene-deployment

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.