Author - StudySection Post Views - 37 views
Selenium

Selenium WebDriverManager

WebDriverManager is a class that downloads and sets the browser’s driver binaries automatically like chromedriver.exe, geckodriver.exe, and edgedriver.exe, etc required by Selenium WebDriver API.

Usually, to automate web applications we need to download browser driver executables manually for required browsers. Once driver executables are downloaded into the local system then we need to place them in a specific path and set the path for driver executables that becomes a very time-consuming and boring task to maintain driver executables and when browser version changes. If the path is not defined in the script or provides the wrong path, It will throw an exception.

“The path to the driver executable must be set by the webdriver.chrome.driver system property”.

Here is an example to set the path for driver executables traditionally in Selenium:?

System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
System.setProperty("webdriver.gecko.driver","/path/to/geckodriver");

This problem can be resolved by using WebDriverManager for different browsers. It provides some driver managers for these browsers(Chrome, Firefox, Edge, Opera, and Internet Explorer).

These can be used as follow:
WebDriverManager.chromedriver().setup();
WebDriverManager.firefoxdriver().setup();
WebDriverManager.edgedriver().setup();
WebDriverManager.operadriver().setup();
WebDriverManager.iedriver().setup();

To use WebDriverManager in the selenium script we can download the jar file directly from the following link:
https://jar-download.com/artifact-search/webdrivermanager
and add jar file in the project using the following steps:
Project -> Build path -> Configure Build path -> Libraries -> Add jars

Other ways we can use the maven dependency. In the pom.xml file, we need to add the below dependency to have WebDriverManager in our Project.

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

Below is the example to launch the different browsers using the selenium WebDriverManager:
package testing;
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.openqa.selenium.opera.OperaDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class driverMngr {
WebDriver driver;
public void browserSet(String browser) {
if (browser.equals("chrome")) {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
} else if (browser.equals("firefox")) {
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
} else if (browser.equals("edge")) {
WebDriverManager.edgedriver().setup();
driver = new EdgeDriver();
} else if (browser.equals("opera")) {
WebDriverManager.operadriver().setup();
driver = new OperaDriver();
} else if (browser.equals("ie")) {
WebDriverManager.iedriver().setup();
driver = new InternetExplorerDriver();
} else {
System.out.println("Browser driver not mentioned");
}
driver.manage().window().maximize();
driver.get("https://www.webnersolutions.com/");
System.out.println(driver.getTitle());
driver.quit();
}
public static void main(String arg[]) {
driverMngr call = new driverMngr();
call.browserSet("edge");
call.browserSet("chrome");
}
}

Result with Screenshot:
code

Get certification for your knowledge in the fundamentals of Computer functioning by clearing the Computer Certification Exam conducted by StudySection. After going through this Computer Certification exam, you will be able to evaluate your basic knowledge of computers.

Leave a Reply

Your email address will not be published. Required fields are marked *