Source: https://www.toolsqa.com/selenium-webdriver/webdrivermanager/
Using WebDriverManager, we can automatically download the driver’s binary files (.exe files) for Web Automation. This tutorial explains about WebDriverManager’s significance and How to make use of it?
How to set driver binaries using WebDriverManager?
All the browsers (Chrome, Firefox, Opera, IE and Microsoft Edge etc.) implement the WebDriver protocol using an executable file (.exe file). In order to create the Driver object (for any browser), you need to download the appropriate executable binary locally beforehand.
If you are familiar with Web Automation then the below lines would be pretty obvious to you, they locate the appropriate binary file for a particular Browser.
- System.setProperty(“webdriver.chrome.driver”, “//path//to//chromedriver”);
- System.setProperty(“webdriver.gecko.driver”, “//path//to//geckodriver”);
- System.setProperty(“webdriver.opera.driver”, “//path//to//operadriver”);
- System.setProperty(“phantomjs.binary.path”, “//path//to//phantomjs”);
- System.setProperty(“webdriver.edge.driver”, “//path//to//MicrosoftWebDriver.exe”);
- System.setProperty(“webdriver.ie.driver”, “//path//to//IEDriverServer.exe”);
How to set driver binary traditionally in Selenium?
@Test
public void WebDriverManagerTest()
{
//Set the path to Chrome driver
System.setProperty("webdriver.chrome.driver", "E:\\Sandbox\\browserdrivers\\chromedriver.exe" );
//Create driver object for Chrome
WebDriver driver = new ChromeDriver();
//Navigate to a URL
driver.get("http://toolsqa.com");
//quit the browser
driver.quit();
}
What is WebDriverManager in Selenium?
- This is aimed at automating the WebDriver binaries management.
- It downloads the required Driver binary file (if not present locally) into Cache (default location ~/.m2/repository/webdriver).
- This eliminates the problem of locally storing the driver binary files and maintaining different versions of the driver files (for different browsers).
- Unless specified, downloads latest version of the binary (.exe file).
- But how? Simply replace System.setProperty() line in the your automation with the below line:
WebDriverManager.chromedriver().setup();
How to set driver binary automatically using WebDriverManager?
@Test
public void WebDriverManagerTest()
{
//setup the chromedriver using WebDriverManager
WebDriverManager.chromedriver().setup();
//Create driver object for Chrome
WebDriver driver = new ChromeDriver();
//Navigate to a URL
driver.get("http://toolsqa.com");
//quit the browser
driver.quit();
}
What are the different Capabilities of WebDriverManager?
How to start specific version of browser’s driver using WebDriverManager?
For your automation, if you wish to use a specific version instead of latest driver this can be done as below:
WebDriverManager.chromedriver().version(“2.40”).setup();
This uses 2.40 version of Chrome Driver instead 2.41 (which was the latest as per 2nd Oct, 2018, when writing this article).
How to specify platform (32-bit or 64-bit) using WebDriverManager?
By default WebDriverManager uses the proper binary based on the machine on which the test case is executed.But if you wish to use a different binary, then architecture() method is your friend. It takes one of the below arguments:
- github.bonigarcia.wdm.Architecture.X64
- github.bonigarcia.wdm.Architecture.X32
Code for the same will be :
WebDriverManager.chromedriver().architecture(io.github.bonigarcia.wdm.Architecture.X32).setup();
Alternately you can use either arch32() or arch64() to specify the type of binary you want to use.
- chromedriver().arch32().setup();
- chromedriver().arch64().setup();
How to set proxy, username and password using WebDriverManager?
If you are working in a big organization, it is very common that you will be working behind a proxy. If you do not specify the proxy details to WebDriverManager, you might see errors similar to :io.github.bonigarcia.wdm.WebDriverManagerException: java.net.UnknownHostException: chromedriver.storage.googleapis.com.
Let’s see how to sort this out. WebDriverManager provides the below methods to set the proxy details (If your proxy required Authentication, you need specify username & password) :
- proxy(“hostname:portnumber”)
- proxyUser(“username”)
- proxyPass(“password”)
WebDriverManager.chromedriver()
.version("2.40")
.arch32()
.proxy("myproxyhostname:80")
.proxyUser("myusername")
.proxyPass("myawesomePassword")
.setup();