webdriver manager in selenium

What is WebDriver Manager?

Webdriver manager in Selenium! To begin with, WebDriver Manager helps overcome the limitations of using WebDriver when browser version and WebDriver version are changing in nature. But, no need to worry because WebDriver Manager can easily automate this task by following some of these easy steps:

What is Selenium WebDriver? – Webdriver Manager in Selenium

If you are using Selenium WebDriver then you must be aware that you have to download a binary file which allows WebDriver to handle browser(such as ChromeFirefoxOperaPhantomJSMicrosoft Edge, or Internet Explorer). Basically, in Java you have to set path to this binary and must be set as JVM properties as below:

WebDriver for Chrome, Firefox, Opera, Edge, IE browser

Chrome WebDriver

System.setProperty(“webdriver.chrome.driver”, “/path/to/binary/chromedriver”);

Gecko WebDriver

System.setProperty(“webdriver.gecko.driver”, “/path/to/binary/geckodriver”);

Opera WebDriver

System.setProperty(“webdriver.opera.driver”, “/path/to/binary/operadriver”);

System.setProperty(“phantomjs.binary.path”, “/path/to/binary/phantomjs”);

Edge WebDriver

System.setProperty(“webdriver.edge.driver”, “C:/path/to/binary/msedgedriver.exe”);

IE WebDriver

System.setProperty(“webdriver.ie.driver”, “C:/path/to/binary/IEDriverServer.exe”);

Limitation: This is quite annoying especially when your browser version and WebDriver version are changing in nature.  As a result it increases some manual effort and need monitoring and in some cases it cases test failure and need unnecessary debug. But, no need to worry WebDriverManager can automate this work. WebDriverManager comes in 3 format:

WebDriverManager as Java Dependency:

Add below dependency in POM.xml file:

<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

Or add in gradle project:

dependencies {
testCompile("io.github.bonigarcia:webdrivermanager:3.8.1")
}

WebDriverManager CLI

  • WebDriverManager can be used interactively from the Command Line Interface (CLI), i.e. the shell, to resolve and download binaries for the supported browsers.
  • Particularly, there are two ways of using this feature: 1. Directly from the source code i.e. Using Maven depedency 2. WebDriver Manager Jar

Selenium WebDriver Manager Maven Dependency – Webdriver manager in Selenium

Generally, the command to be used for maven depedency is mvn exec:java -Dexec.args=”browserName”

WebDriverManager Example Using Maven Depedency:

> mvn exec:java -Dexec.args="chrome"
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building WebDriverManager 3.8.1
[INFO] ------------------------------------------------------------------------
INFO] Extracting binary from compressed file chromedriver_win32.zip
[INFO] Resulting binary D:\projects\webdrivermanager\chromedriver.exe
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.919 s
[INFO] Finished at: 2019-04-11T00:36:32+02:00
[INFO] ------------------------------------------------------------------------

WebDriver Manager Jar Or bonigarcia webdrivermanager

  • Using WebDriverManager as a fat-jar (i.e. WebDriverManager with all its dependencies in a single executable JAR file).
  • This JAR file can downloaded from here and also it can be created using the command mvn compile assembly:single from the source code. Once you get the fat-jar, you simply need to use the command java -jar webdrivermanager-3.8.1-fat.jar browserName

WebDriverManager Server

As of version 3.0.0, WebDriverManager can used as a server. In order to start this mode, the shell is used. Once again, two options are allowed:

Directly from the source code and Maven. The command to be used is mvn exec:java -Dexec.args=”server <port>”. If the second argument is not specified then the default port will be used (4041).

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Once you you have added or included dependency, WebDriverManager can do all work for you. As an illustration, write below JUnit Code in ide and run

public class ChromeTest { 
private WebDriver driver; 

@BeforeClass 
public static void setupClass() { 
WebDriverManager.chromedriver().setup(); 
}
 
@Before 
public void setupTest() { 
driver = new ChromeDriver(); 
}
 
@After 
public void teardown() { 
if (driver != null) { 
driver.quit(); 
}
 
} 

@Test 
public void test() { 
// Your test code here 
}
 
}

In above code as has been noted WebDriverManager.chromedriver().setup(); is a magic statement which actually does following work:

  1. It checks the version of the browser installed in your machine (e.g. Chrome, Firefox).
  2. It checks the version of the driver (e.g. chromedrivergeckodriver). If unknown, it uses the latest version of the driver.
  3. Correspondingly, it downloads the WebDriver binary if it is not present on the WebDriverManager cache (~/.m2/repository/webdriver by default).
  4. It exports the proper WebDriver Java environment variables required by Selenium (not done when using WebDriverManager from the CLI or as a Server).

WebDriverManager downloads the driver binaries for the following browsers ChromeFirefoxOperaPhantomJSMicrosoft EdgeInternet Explorer, and Chromium. For that, it provides several drivers managers for these browsers. These drivers managers can be used as follows:

  1. WebDriverManager.chromedriver().setup();
  2. WebDriverManager.firefoxdriver().setup();
  3. WebDriverManager.operadriver().setup();
  4. WebDriverManager.phantomjs().setup();
  5. WebDriverManager.edgedriver().setup();
  6. WebDriverManager.iedriver().setup();
  7. WebDriverManager.chromiumdriver().setup();

The following table contains some examples:

Example Description
WebDriverManager.chromedriver().version(“2.26”).setup(); Force to use version 2.26 of chromedriver
WebDriverManager.firefoxdriver().arch32().setup(); Force to use the 32-bit version of geckodriver
WebDriverManager.operadriver().forceCache().setup(); Force to use the cache version of operadriver
WebDriverManager.phantomjs().useMirror().setup(); Force to use the taobao.org mirror to download phantomjs driver
WebDriverManager.chromedriver().proxy(“server:port”).setup(); Using proxy server:port for the connection

Additional methods are exposed by WebDriverManager, namely:

  • getVersions(): On the positive side, this method allows you to find out the list of of available binary versions for a given browser.
  • getBinaryPath(): On the condition that you have applied this method you will find out the path of the latest resolved binary.
  • getDownloadedVersion(): On account of this method you will find out the version of the latest resolved binary.
  • clearCache(): This methods allows to remove all binaries previously downloaded by WebDriverManager.

WebDriverManager Example:

package webDriver;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class browserTest {

    public static String browserName = "Chrome";
    public WebDriver driver;

    @Test
    public void BrowserTest() {
        if (browserName.contains("Chrome")) {
            WebDriverManager.chromedriver().setup();
            driver = new ChromeDriver();

        } else if (browserName.contains("IE")) {
            WebDriverManager.iedriver().setup();
            ;
            driver = new InternetExplorerDriver();

        } else if (browserName.contains("FireFox")) {
            WebDriverManager.firefoxdriver().setup();
            driver = new FirefoxDriver();
        }
        else if (browserName.contains("EDGE")) {
            WebDriverManager.edgedriver().setup();
            driver = new EdgeDriver();
        }
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(3000, TimeUnit.SECONDS);
        driver.get("https://thoughtcoders.com/");
        

    }

}

To this end, hope above article help you to set up Webdriver manager in Selenium Project. In addition, feel free to reach out to us if you need any assistance. Contact us or call on 9555902032 together with this you may also mail us on query@thoughtcoders.com or join us on Facebook and LinkedIn!

Comments

Arun May 23, 2020

It’s very informative blog before reaching on this blog, wasted around 3 hours for webdriver manager concept. Thank you guys keep up good work.

Write A Comment