Tuesday, September 10, 2019

Post#9.Open Browser with WebDriverManager


What is WebDriverManager ?

WebDriverManager allows to automate the management of the binary drivers ( e.g. chromedriver, geckodriver, etc.) required by Selenium 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();


Usage:
In order to use WebDriverManager from tests in a Maven project, you need to add the following dependency in your pom.xml (Java 8 or upper required):
<dependency>
              <groupId>io.github.bonigarcia</groupId>
              <artifactId>webdrivermanager</artifactId>
              <version>3.3.0</version>
              <scope>test</scope>
</dependency>

If you are writing script with Java Project, you have to download the webdrivermanager Jar file from maven repository (https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager) and have to add in to your project.
Open Chrome browser:
package pack4;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;

public class OpenChrome {

              public static void main(String[] args) {
                             //setup the chromedriver using WebDriverManager
        WebDriverManager.chromedriver().setup();
        //Create driver object for Chrome
        WebDriver driver = new ChromeDriver();

        //Navigate to a URL
        driver.get("http://google.com");
        //quit the browser
        driver.quit();
              }
}
You are all set to use WebDriverManager in your Automation code. WebDriverManager can be used as below for various browsers:
·       WebDriverManager.chromedriver().setup();
·       WebDriverManager.firefoxdriver().setup();
·       WebDriverManager.operadriver().setup();
·       WebDriverManager.phantomjs().setup();
·       WebDriverManager.edgedriver().setup();
·       WebDriverManager.iedriver().setup();

Different Capabilities of WebDriverManager

WebDriverManager exposes its API by means of the builder pattern. This means that given a WebDriverManager instance, their capabilities can be tuned using different methods.

Start specific version of browser’s driver using WebDriverManager

version (String) : This method helps user to set specific browser driver version
Note : By default, WebDriverManager tries to download the latest version of a given driver binary. A concrete version can be specified using this method.
Example: if you wish to use a specific version instead of latest driver this can be done as below:
WebDriverManager.chromedriver().version(“2.40”).setup();

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 use below methods.
arch32() : Force to use the 32-bit version of a given driver binary.
arch64() : Force to use the 64-bit version of a given driver binary.
Example: 
1.      chromedriver().arch32().setup();
2.      chromedriver().arch64().setup();

Set proxy, username and password using WebDriverManager

WebDriverManager provides the below methods to set the proxy details.
proxy(String) : Use a HTTP proxy for the Internet connection using the following notation: 
my.http.proxy:1234 
or 
username:password@my.http.proxy:1234. 
Example: proxy(“my.http.proxy:1234”) or proxy(username:password@my.http.proxy:1234)
proxyUser(String): Specify a username for HTTP proxy.
proxyPass(String): Specify a password for HTTP proxy.
Example:
proxyUser(“username”)
proxyPass(“password”)

Use Selenium WebDriverManager capabilities together

package pack4;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;

public class OpenChrome {

              public static void main(String[] args) {
                             WebDriverManager.chromedriver()
        .version("2.40")
        .arch32()
        .proxy("my.http.proxy:1234 ")
        .proxyUser("myusername")
        .proxyPass("myPassword")
.setup();
              }
}

No comments:

Post a Comment