Tuesday 9 August 2022

Automation of web driver | Usage of WebDriverManager or alternative for System.setProperty("webdriver.chrome.driver", "D\chromedriver.exe");

  • WebDriverManager is an open-source Java library
  • It carries out the management (i.e., download, setup, and maintenance) of the drivers required by Selenium WebDriver(e.g., chromedriver, geckodriver, msedgedriver, etc.) in a fully automated manner.
  • The primary use of WebDriverManager is the automation of driver.
  • With this we can get rid of System property to fetch the driver placed in physical drive or from the project path. 
More details : 
https://bonigarcia.dev/webdrivermanager/

GitHub @ https://github.com/bonigarcia/webdrivermanager

Example: 
This is how we set the path for chrome driver once the driver is downloaded and placed in system hard drive . 
System.setProperty("webdriver.chrome.driver", "D\chromedriver.exe");

Now, using DriverMnager we can get rid of physical drive path, that is replace the above line of code with 
WebDriverManager.chromedriver().setup();

This requires the following dependency to be added in pom.xml file. 

<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>4.3.0</version>
</dependency>

In the Before hook of cucumber, normally we create the ChromeDriver object, prior creating the chrome driver object, we need to call the chrome driver setup() as shown in below example.

@Before(order = 0) // Cucumber Before Hook with order 0
public void before(Scenario scenario) throws IOException {

	Log.info("Initiating the chrome driver from Cucumber Before hook with order=0");		
	WebDriverManager.chromedriver().setup();
	driver = new ChromeDriver();
	driver.manage().window().maximize();
}

No comments:

Post a Comment