How to manage chromedriver for multiple OS in same directory in Selenium Test Automation Framework?

There are multiple ways to manage chromedriver for multiple OS in same directory in Selenium Test Automation Framework.

The simplest of them is to find the Operating system name using:

String OS = System.getProperty("os.name");
 
Now, we can initialize a variable browser and  pass it's value using our testng.xml file i.e: Firefox, Chrome or IE (in which browser we need to execute our testscript)

String browserName = browser;
 
Now we must initialize the path to chromedrivers in our properties / config file:
ChromedriverPath = .drivers/chromedriver.exe;
ChromedriverPathMac= .drivers/chromedriver;
 
// fetch driver paths from properties file
String ChromedriverPath = prop.getProperty("ChromedriverPath");
String ChromedriverPathMac = prop.getProperty("ChromedriverPathMac");
 
Now, we can simply use switch or can use if / else blocks to pick the correct Chromedriver based on OS even if drivers are present in same directory as MacOS chromedriver does not have any extension where as Windows Chromedriver is named as chromedriver.exe

if (browserName.equals("Chrome") && OS.contains("Window") ) {
System.setProperty("webdriver.chrome.driver", ChromedriverPath);
driver = new ChromeDriver();
else if(browserName.equals("Chrome") && OS.contains("Mac") ) {
System.setProperty("webdriver.chrome.driver", ChromedriverPathMac);
driver = new ChromeDriver();
}

Happy Testing :)
How to manage chromedriver for multiple OS in same directory in Selenium

Post a Comment

0 Comments