Sunday, June 28, 2020

WebDriverManager -To get rid off specifying the driver path(chrome/gecko/ie)in Selenium?

Problem


1.If there is new version available w.r.to browser then we must change it manually every time either chrome driver/gecko/IE/Opera or any drivers.
Below is the example where we explicitly specified path of drivers from folder.









Here if there is new chrome driver then we must download then update that version under driver folder, same will be applicable for gecko driver/IE drivers as well.
But there is way to avoid this by using WebDriverManager.

WebDriver Manager is Abstract class which has multiple methods to avoid specifying the driver paths.
WebDriverManager supports chromedriver ,firefoxdriver ,ie driver, edgedriver, operadriver… etc.
We have 2 ways to use WebDriverManager
1.      WebDriverManager.chromedriver().setup() :-This will always tries to fetch the latest Chrome Driver version and download it to local maven repo . m2/repository/webdriver folder

Here    WebDriverManager : is Abstract Class
            Chromedriver() : is method for Chrome Browser, similarly we have firefoxdriver iedriver for respective Firefox and IE Browsers
            Setup() : is method which will download the driver version into

WebDriverManager.firefoxdriver().setup();

WebDriverManager.iedriver().setup();


Code Below:

package automationTest;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;

import io.github.bonigarcia.wdm.WebDriverManager;

public class LoginLogout1 {
public static void main(String[] args) throws InterruptedException {
//System.setProperty("webdriver.chrome.driver","D:\\UPC\\Selenium\\Repository\\Project1\\drivers\\chromedriver1.exe");
        WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("http://www.newtours.demoaut.com/");
System.out.println(driver.getTitle());
driver.findElement(By.xpath("//input[@name='userName']")).clear();
driver.findElement(By.xpath("//input[@name='userName']")).sendKeys("k.manjunath1818@gmail.com");
driver.findElement(By.xpath("//input[@name='password']")).clear();
driver.findElement(By.xpath("//input[@name='password']")).sendKeys("selenium@123");
driver.findElement(By.xpath("//input[@name='login']")).click();
Thread.sleep(10000);
driver.findElement(By.xpath("//tr//td[@class='mouseOut']//a[contains(text(),'SIGN-OFF')]")).click();
/*
* Assert.assertEquals(
* driver.findElement(By.xpath("//span[contains(text(),'India')]")) .getText(),
* "India");
*/
driver.close();
driver.quit();
}
}

2.WebDriverManager.chromedriver().version("83").setup() :-This will tries to fetch the specified Chrome Driver version only.

Version() :- Will take String arguments for version number

https://chromedriver.chromium.org/downloads --> For Chrome Driver Versions
https://github.com/mozilla/geckodriver/releases  à For Gecko Driver Versions

When we are using any of these methods, I would suggest to always cross check the Browser Version/Selenium Version/Driver version because you may face compatibility issues and end with with Session not Created Exception.

For Latest Chrome Browser u can use below: -
Chrome Driver Version: - 83
Selenium Version: - 4
Browser Version: - Latest

 

code:

package automationTest;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;

import io.github.bonigarcia.wdm.WebDriverManager;

public class LoginLogout1 {
public static void main(String[] args) throws InterruptedException {
//System.setProperty("webdriver.chrome.driver","D:\\UPC\\Selenium\\Repository\\Project1\\drivers\\chromedriver1.exe");
        WebDriverManager.chromedriver().version("83").setup();
WebDriver driver = new ChromeDriver();
driver.get("http://www.newtours.demoaut.com/");
System.out.println(driver.getTitle());
driver.findElement(By.xpath("//input[@name='userName']")).clear();
driver.findElement(By.xpath("//input[@name='userName']")).sendKeys("k.manjunath1818@gmail.com");
driver.findElement(By.xpath("//input[@name='password']")).clear();
driver.findElement(By.xpath("//input[@name='password']")).sendKeys("selenium@123");
driver.findElement(By.xpath("//input[@name='login']")).click();
Thread.sleep(10000);
driver.findElement(By.xpath("//tr//td[@class='mouseOut']//a[contains(text(),'SIGN-OFF')]")).click();
/*
* Assert.assertEquals(
* driver.findElement(By.xpath("//span[contains(text(),'India')]")) .getText(),
* "India");
*/
driver.close();
driver.quit();
}
}


If your using Maven just add below Dependency: -

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

If your using Gradle just add below Dependency: -
// https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager
compile group: 'io.github.bonigarcia', name: 'webdrivermanager', version: '3.4.0'
If your using Normal Java Project then download Jar from here and add to the Project: -

operatingSystem(OperatingSystem) :- By Default WebDriver will try to download based on the user operating System but if we want to explicitly specify the Operating system then this can be used. (WINLINUXMAC)
WebDriverManager.edgedriver().operatingSystem(OperatingSystem.MAC).setup();

browserVersion(String):- If we want to explicitly specify the browser version for which we need the driver version then we can specify that using this method

  

WebDriverManager.firefoxdriver().browserVersion("75").setup();


 localRepositoryUser(String) / localRepositoryPassword(String) :- If we set username/Password for Local repository then we can speciy the username/password with this so that when WebDriverManager can access local repo even if its secured


Points to Remember: -

1.      Make sure to cross check the Browser/Selenium with version number mentioned in Version(“”) method to compatibility Issues

2.      WebDriverManager has 1 day ttl(time to live) so once it downloaded the specified version or latest version till 24 hrs it will look for local repo before launching browser if it doesn’t find then again it will downloads (this can be configured using
3.      With 4.x version of WebDriverManager few methods are renamed refer the below link for complete Documentation


Selenium_Grid_With_Docker_Compose(yml file)

 docker-compose.yml version: "3" services:   hub:     image: selenium/hub:3.141.59     ports:       - "4444:4444"   chro...