WebDriverManager: New Way to handle driver binaries in Selenium
What is WebDriverManager?
WebDriverManager is an open-source library which is used to automate the handling of browser driver binaries. User can add this library in their selenium project using a build tool like Maven or Gradle depending on your project.
So let’s talk about the advantages or the task which WebDriverManager handles.
- Checks the version of browser installed on your machine.
- Download the driver binary if needed as per the current version of browser to run your selenium script in browser.
- It matches the version of binary and browser and if it doesn’t work anyway then it takes recent browser driver binary (chromedriver.exe or geckodriver.exe)
- It removes the dependency on driver binary on local,
Why we are using this WebDriverManager?
To run selenium script in the browser we need to download driver binary from the official site of Browser. This could be ‘chromedriver.exe’ for Chrome and ‘geckodriver.exe’ and similarly, we have many of the driver binary which needs to be downloaded to run the script in any of the browsers.
But we are not stopping here and our problem is not resolved. But we need to add two lines of code
1- System path for Driver like this. For Chrome Browser we need to put this line of code. Read More
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");//we are writing chromedriver without extension, because as per operating system extension may change
For Firefox we need to add this line.
System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver");
For all other browser, it could be something like this
System.setProperty("webdriver.edge.driver", "/path/to/msedgedriver.exe"); //Edge Browser System.setProperty("webdriver.opera.driver", "/path/to/operadriver"); //For Opera System.setProperty("phantomjs.binary.path", "/path/to/phantomjs"); //For PhantomJs System.setProperty("webdriver.ie.driver", "C:/path/to/IEDriverServer.exe"); // For IE
2- Initializing the browser instance. Which looks like this
WebDriver driver = new ChromeDriver();
// For Chrome
But we are not sure that next morning this script is going to work on Chrome/Firefox/Edge/Other Browser or not if browse version is updated.
But why this is going to happen. This is always going to happen due to the mismatch of driver binary and the browser version. Then we could be a possible way to move ahead.
So the answer will be download and set the system path and run the script, Might be it will work.
This is the reason, Boni Garcia came up with this WebDriverManager library which automates this complete process of downloading binary and matching it with the version of the browser.
How to add WebDriverManager Dependency In Selenium Project?
There are three ways to add WebDriverManager in project.
- By Adding Maven Dependency In POM: If you are using maven as build tool in that case we need to add this dependency in pom.xml
<dependency> <groupId>io.github.bonigarcia</groupId> <artifactId>webdrivermanager</artifactId> <version>4.2.2</version> <scope>test</scope> </dependency>
- By Adding WebDriverManager dependency in build.gradle file like this
dependencies {
testImplementation("io.github.bonigarcia:webdrivermanager:4.2.2")
}
Once we perform these two steps, we are good to create our first script.
How to initialize the WebDriverManager instance in script?
Use this code in your eclipse and it will launch the chrome browser without setting up System properties for chrome
package webdriverExample;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class WebDriverManagerExample {
public static void main(String[] args) throws InterruptedException {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("https://abodeqa.com");
Thread.sleep(5000);
driver.quit();
}
}
Explanation:
1- In above script, WebDriverManager has been initialized for Chrome Browser through line of codeWebDriverManager.chromedriver().setup().
2- So above line will setup the driver binary for chrome. Similarly if we need to initialize other browser then here is the list of initialization for individual browserWebDriverManager.firefoxdriver().setup();
WebDriverManager.edgedriver().setup();
WebDriverManager.operadriver().setup();
WebDriverManager.phantomjs().setup();
WebDriverManager.iedriver().setup();
WebDriverManager.chromiumdriver().setup();
3- After adding WebDriverManager initialization, our code will be same as we use to write previously using System property.
Along with this WebDriverManager also provide one generic driver manager which is parameterized using Selenium Driver Class. Have a look on this code
package webdriverExample;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
import io.github.bonigarcia.wdm.config.DriverManagerType;
public class WebDriverManagerExample {
public static void main(String[] args) throws InterruptedException {
Class<ChromeDriver> driverclass = ChromeDriver.class;
WebDriverManager.getInstance(driverclass).setup();
WebDriver driver = new ChromeDriver();
driver.get("https://abodeqa.com");
Thread.sleep(5000);
driver.quit();
}
}
So In above code, we can see that first we have created Selenium Driver Class of Chrome browser by writing this line of code
Class<ChromeDriver> driverclass = ChromeDriver.class;
After this driver class instance is being passed to WebDriverManager’s getinstance method as parameter like this
WebDriverManager.getInstance(driverclass).setup();
But above initialization can also be written like this using enumeration DriverManagerType, So above two line of code can be written in a single line WebDriverManager.getInstance(DriverManagerType.CHROME).setup();
What if we want to keep specific version of driver binary or browser version?
This library has provided a way to use a specific version of the driver as well as browser
So let’s say you want to run some older version of chromedriver or any other browser driver, in that case, you need to write the code like this
WebDriverManager.chromedriver().driverVersion(“86.0.4240.22”).setup();
So in place of version 86.0.4240.22, you can put any of the version of chromedriver version which you can see on this URL
https://chromedriver.chromium.org/downloads
Similarly, in place of using driver version, we can use browser version to select any specific version of the browser, So code will look like this
WebDriverManager.chromedriver().browserVersion(“87.0.4280”).setup();
So if you want to learn something more about WebDriverManager in that I would suggest you, use this link and read this in detail.
Laptop Affiliate Link: Lenovo Ideapad S540
Configuration : Processor: i5 10th gen, RAM: 8GB HardDriver : 1tb hdd + 256 gb SSD , Operating System: Windows 10 Home Office: 2019 & NVIDIA: 2GB
This is a fantastic blog from which people can learn a lot. It is very informative and is explained in…
Thanks for sharing such knowledgeable Content on Automation testing, to know how to enhance the performance of ERP's like Oracle…
Thanks.. this list is much needed.
This one is also good to use. Thanks for sharing this.. Might be we can also add it in list…
How about youtube-dl?