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.
A simple and small example, if you don’t know this tool is:
public class ChromeTest { | |
public void openGoogleChrome() { | |
// setup the browsr binary and set the proper path | |
WebDriverManager.chromedriver().setup(); | |
// create the Google Chrome browser instance | |
WebDriver driver = new ChromeDriver(); | |
driver.get("http://eliasnogueira.com"); | |
} | |
} |
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.
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
.
Since version 3.8.1 you can now use an enumeration to set up and initiate the target browser.
import static io.github.bonigarcia.wdm.DriverManagerType.CHROME; | |
import org.openqa.selenium.WebDriver; | |
import io.github.bonigarcia.wdm.WebDriverManager; | |
// … | |
DriverManagerType chrome = DriverManagerType.CHROME; | |
WebDriverManager.getInstance(chrome).setup(); | |
Class<?> chromeClass = Class.forName(chrome.browserClass()); | |
driver = (WebDriver) chromeClass.newInstance(); |
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.
Try to follow, first, what we need to do:
DriverManagerType
Now look at the code below:
public class LocalDriverManager { | |
// to make this work just pass the browser name, that must match from the DriverManagerType class | |
public WebDriver createInstance(String browser) { | |
WebDriver driver = null; | |
try { | |
DriverManagerType driverManagerType = DriverManagerType.valueOf(browser.toUpperCase()); | |
Class<?> driverClass = Class.forName(driverManagerType.browserClass()); | |
WebDriverManager.getInstance(driverManagerType).setup(); | |
driver = (WebDriver) driverClass.newInstance(); | |
} catch (IllegalAccessException | ClassNotFoundException e) { | |
// exception or log for class not found | |
} catch (InstantiationException e) { | |
// exception of log for instantiation problem | |
} | |
return driver; | |
} | |
} |
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.
You can have these benefits:
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.
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 general.properties 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.
1 Comment
Hello Sir!
Please, what would be the best approach to launch the Chrome browser in headless mode in LocalDriverManager?