Create a Browser Factory with WebDriverManager

Published by Elias on

WebDriverManager is an excellent tool to automate the management of the browser driver binaries running your automated test scripts locally with Selenium WebDriver and Java.

Simple example

A simple and small example, if you don’t know this tool is:

The main thing here is the code on line 6. The WebDriverManager class has a method with the name of the browser, like a chained method. Following if you use the setup() method this class will download the browser binary according to your operating system and set the binary path.

What is a Browser Factory

Factory is a Design Pattern. It’s used when we need to create an object that shares a common implementation. In this context about browser creation, it will use methods like create(), close(), getDriver().

We have, sometimes, different approaches for a Browser Factory. Below you can see one possible approach:

Basically you have an interface (DriverManager) that has the implementation for each browser (create and quit). They are separated because each browser has its own way to implement certain configurations.

We must create a factory to instantiate the correct browser. On the diagram, you can see an enumeration to support browser creation.

The TestClasses uses the DriverManagerFactory to create the browser instance.

This approach is a good one, of course with a BaseTest and some configuration to easily create multi-browser support, but if you have to create an approach or execute your tests locally we can apply a light way implementation with WebDriverManager.

Browser Factory with WebDriverManager

Since version 3.8.1 you can now use an enumeration to set up and initiate the target browser.

Line 8 set the DriverManagerType enum to Chrome.

Line 9 shows the browser setup, passing the enum (Chrome) as a parameter to getInstance() method.

Line 10 is getting the full class name, from the enum, to create a class.

Line 11 creates a new instance of that class.

Did you figure out that the enum DriverManagerType has the full class name? You now can generate a browser factory with few lines of code.

How to create it

Try to follow, first, what we need to do:

  1. Create a class with a method that will receive the browser name
  2. Convert the browser name to the enum DriverManagerType
  3. Get the full class name from the enumeration and create a class
  4. Set up the browser (binary and path)
  5. Instantiate the browser and return it

Now look at the code below:

Line 4 is a method that receives a browser name as a parameter and returns a WebDriver object.

Line 8 transforms the browser name as a DriverManagerType if the name matches.

Line 9 gets the received browser complete class name from the DriverManagerType and passes it to a generic class object.

Line 10 setup the driver (binary and path) for the received browser.

Line 11 creates a new instance of the browser.
Eg: if the received browser was chrome this line will, implicitly, do: new ChromeDriver();

Line 17 returns the browser instantiated and ready for use on your test class.

The benefits

You can have these benefits:

  • Only one code for browser factory creation
  • Less code to maintain
  • Fast solution for multi-browser

If you need to add additional configurations on the browser, like enable/disable headless mode, change the screen size and so on you should implement like shown on the explanation about browser factory.

Looking for a real example?

At selenium-java-lean-test-achitecture you can see an implementation of the local browser factory.

The application has a properties file. If you clone this project and execute in your machine the take a look at the file on src/main/java/resources/conf folder. There’s a property target as local, which means local execution.

Now open the LocalDriverManager class on src/main/java/driver/local. It’s the same implementation of this post.

The execution is controlled by the DriverFactory class on src/main/java/driver reading the target execution (local or remote) and instantiate the local or remote driver manager.

The BaseWeb class on src/main/java/test, a base test, receive as a parameter the browser or use chrome if no browser is informed, create the WebDriver instance send the browser information to the DriverFactory. Your execution is set as local, so the DriverFactory will instantiate the LocalDriverManager to the browser informed.


I help professional software engineers (backend, frontend, qa) to develop their quality mindset and deliver bug-free software so they become top-level developers and get hired for the best positions in the market.

1 Comment

Luiz · March 24, 2020 at 5:19 PM

Hello Sir!
Please, what would be the best approach to launch the Chrome browser in headless mode in LocalDriverManager?

Leave a Reply

Avatar placeholder

Your email address will not be published.