hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

WebDriverManager- An automated way to handle browser binaries without System.setProperty

Selenium is an open-source framework developed to automate the testing of web applications across different browsers. The first step in browser automation is downloading browser specific binaries which has to be manually done because the browser doesn’t have an automated built-in server to run the automated scripts. Wouldn’t it be cool if there is a way to handle the browser binaries automatically which will make our work easier?


WebDriverManager is an open source Java library that takes care of downloading the browser binaries in an automated manner.


Code snippet with System.setProperty

Let’s see how the browser binaries were downloaded in a traditional approach using System.setProperty


import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class SampleDemo {


public static void main(String args[]) {

WebDriver driver = null;


System.setProperty("webdriver.chrome.driver",

"C:\\Users\\Documents\\Drivers\\chromedriver.exe");

driver = new ChromeDriver();

driver.get("https://www.google.com");

driver.manage().window().maximize();

driver.quit();


}


}


In the above code snippet, System.setProperty has two attributes .The first attribute "webdriver.chrome.driver" specifies the name of the browser specific driver and "C:\\Documents\\Drivers\\chromedriver.exe” is the absolute path which points to the location of the browser specific driver. Here the chromedriver.exe is of version 96 whereas my current chrome browser version is 98.0.4758.82





When we use System.setProperty to instantiate the driver and if path in our local system in which the browser specific driver is located is incompatible with the browser version, we will get the below error.


Starting ChromeDriver 96.0.4664.45 (76e4c1bb2ab4671b8beba3444e61c0f17584b2fc-refs/branch-heads/4664@{#947}) on port 46758

Only local connections are allowed.

Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.

ChromeDriver was started successfully.

Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: session not created: This version of ChromeDriver only supports Chrome version 96

Current browser version is 98.0.4758.81 with binary path C:\Program Files\Google\Chrome\Application\chrome.exe

Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'

System info: host: 'HP-LAPTOP', ip: '198.168.1.14', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '11.0.9'

Driver info: driver.version: ChromeDriver



Let’s see how WebDriverManager can make our work easy in managing the browser binaries. As a first step, we need to add the following dependencies in pom.xml.


Dependency for WebDriverManager

<!https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->

<dependency>

<groupId>io.github.bonigarcia</groupId>

<artifactId>webdrivermanager</artifactId>

<version>5.0.3</version>

</dependency>


Dependency for SLF4J - It is recommended to use a logger library while using WebDriverManager to trace the application and tests.

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-simple</artifactId>

<version>1.7.35</version>

<scope>test</scope>

</dependency>

Code snippet using WebDriverManager

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;


import io.github.bonigarcia.wdm.WebDriverManager;


public class WebDriverManagerexample {

public static void main(String args[]) throws InterruptedException

{

WebDriverManager.chromedriver().setup();

WebDriver driver = new ChromeDriver();

Logger logger = LoggerFactory.getLogger(WebDriverManagerexample.class);

logger.info("WebDriverManager Example");


driver.get("https://www.google.com");

Thread.sleep(5000);

driver.quit();

}

}


Output from the above code snippet


[main] INFO io.github.bonigarcia.wdm.WebDriverManager - Using chromedriver 98.0.4758.80 (resolved driver for Chrome 98)

[main] INFO io.github.bonigarcia.wdm.WebDriverManager - Exporting webdriver.chrome.driver as C:\Users\guest1\.cache\selenium\chromedriver\win32\98.0.4758.80\chromedriver.exe

Starting ChromeDriver 98.0.4758.80 (7f0488e8ba0d8e019187c6325a16c29d9b7f4989-refs/branch-heads/4758@{#972}) on port 2284

Only local connections are allowed.

Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.

ChromeDriver was started successfully.

Feb 08, 2022 11:58:54 PM org.openqa.selenium.remote.ProtocolHandshake createSession

INFO: Detected dialect: W3C

[main] INFO WebDriverManagerexample - WebDriverManager Example


What happens behind WebDriverManager.chromedriver().setup()?


· When the setup() method in WebDriverManager is called, WebDriverManager executes a resolution algorithm to find the browser version using a commands database which contains a collection of shell commands to find the version of the particular browser.

· Once the browser version is found, it tries to find the driver version compatible with the browser version using a versions Database.

· After it has discovered the driver version, the driver is downloaded to the local cache (\.cache\selenium-please refer the above output) by WebDriverManager. This driver will be used whenever WebDriverManager.chromedriver().setup() is called within TTL time of the driver and browser versions stored in the cache.

· The cache has a resolution.properties file in which the relationship between the resolved driver and browser versions are stored. There is a TTL (time-to-live) period of 1 hour for browser versions and 1 day for driver versions. This means when the WebDriverManager.chromedriver().setup() is called, the browser version and driver version is fetched from the local cache if the browser version was updated within one hour and driver version was updated within one day in the local cache.

· This improves the performance as the information in the local cache is reused instead of executing the resolution algorithm every time.

· At last, the driver path is exported using Java System Properties (eg.webdriver.chrome.driver incase of chrome Driver)



By using WebDriverManager in the above code snippet, we can be trouble free from managing the browser binaries whenever the browser version gets updated. With WebDriverManager, we can also specify the specific driver version compatible with the browser version using the below code snippet.


WebDriverManager.chromedriver().driverversion(“94.0.4606.113”).setup();


WebDriverManagers for different browsers


WebDriverManager.chromedriver().setup();

WebDriverManager.firefoxdriver().setup();

WebDriverManager.edgedriver().setup();

WebDriverManager.operadriver.setup();

WebDriverManager.chromiumdriver().setup();

WebDriverManager.edgedriver().setup();



We can also use the create() method of WebDriverManager and instantiate WebDriver objects using WebDriverAPI avoiding the setup() method.


public class WebDriverManagerexample {

public static void main(String args[]) throws InterruptedException

{

WebDriver driver = WebDriverManager.chromedriver().create();

Logger logger = LoggerFactory.getLogger(WebDriverManagerexample.class);

logger.info("WebDriverManager Example");


driver.get("https://www.google.com");

Thread.sleep(5000);

driver.quit();

}

}


Thanks for reading this post!!


References: WebDriverManager (bonigarcia.dev)

216 views0 comments

Recent Posts

See All