How to fetch DRIVER_EXE_PROPERTY for different browsers dynamically?

1_6KbvW_GQYI9u6Cc_S2AHLQ@2x

   In case of Selenium Automation for Web application, most of the time people needs to set DRIVER_EXE_PROPERTY in the code level prior to driver instantiation. For that we need to download the respective DRIVER_EXE and set the path of the DRIVER_EXE in the code.

  Here my piece of code explain how to fetch DRIVER_EXE_PROPERTY automatically while script execution for different browsers. I have done it for chrome, Firefox and IE in Windows system as well as chrome and Firefox in MAC system. Following are the details:

  Below code is necessary to instantiate chrome driver session. In this code I used  getWebAutomationDriver(browserName) to get the DRIVER_EXE path from the system.

System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, getWebAutomationDriver(browserName));
ChromeDriverService chromeservice = new ChromeDriverService.Builder().build();
driver = new ChromeDriver(chromeservice); 

Following is the explanation for getWebAutomationDriver(browserName) method and there again we called  getActualDriverPath(browserName) as a private method to get the actual DRIVER_EXE path,

public static String getWebAutomationDriver(String browserName) throws InterruptedException {
String driverPath = null;
final String operatingSystem = System.getProperty(“os.name“);
try {
if (getActualDriverPath(browserName) == null) {
if (operatingSystem.contains(“Windows”)) {
if (browserName.equalsIgnoreCase(“chrome”)) {
System.out.println(
“chromedriver not installed on system, go to command-line and enter ‘npm install chromedriver’. \nAlso set PATH variable ‘C:\\Users\\…..\\node_modules\\chromedriver\\lib\\chromedriver’ in your system.\nOnce it done, you may need to restart your system”);
System.exit(0);
}
if (browserName.equalsIgnoreCase(“Firefox”)) {
System.out.println(
“geckodriver not installed on system, go to command-line and enter ‘npm install geckodriver’. \nAlso set PATH variable ‘C:\\Users\\…..\\node_modules\\geckodriver’ in your system.\nOnce it done, you may need to restart your system”);
System.exit(0);
}
if (browserName.equalsIgnoreCase(“IE”) || browserName.equalsIgnoreCase(“Internet Explorer”)
|| browserName.equalsIgnoreCase(“InternetExplorer”)) {
System.out.println(
“iedriver not installed on system, go to command-line and enter ‘npm i iedriver@3.7.0’. \nAlso set PATH variable ‘C:\\Users\\…..\\node_modules\\iedriver\\lib\\iedriver64’ in your system.\nOnce it done, you may need to restart your system”);
System.exit(0);
}
} else {
if (browserName.equalsIgnoreCase(“chrome”)) {
System.out.println(
“chromedriver not installed on MAC, go to terminal and enter ‘brew install chromedriver’. \nAlso set PATH variable in Run Configurations of your project. \nSteps: Right click on your project > Run As > Run Configurations > Environment tab”);
System.exit(0);
}
if (browserName.equalsIgnoreCase(“Firefox”)) {
System.out.println(
“geckodriver not installed on MAC, go to terminal and enter ‘brew install geckodriver’. \nAlso set PATH variable in Run Configurations of your project. \nSteps: Right click on your project > Run As > Run Configurations > Environment tab”);
System.exit(0);
}
}
} else {
driverPath = getActualDriverPath(browserName);
}
} catch (Exception e) {
e.printStackTrace();
}
return driverPath;
}

    Following is the explanation for getActualDriverPath(browserName) to fetch the actual DRIVER_EXE path,

private static String getActualDriverPath(String browserName) {
String actualDriverPath = null;
try {
String command = null;
final String operatingSystem = System.getProperty(“os.name“);
if (operatingSystem.contains(“Windows”)) {
if (browserName.equalsIgnoreCase(“chrome”)) {
command = “where ” + “chromedriver”;
}
if (browserName.equalsIgnoreCase(“Firefox”)) {
command = “where ” + “geckodriver”;
}
if (browserName.equalsIgnoreCase(“IE”) || browserName.equalsIgnoreCase(“Internet Explorer”)
|| browserName.equalsIgnoreCase(“InternetExplorer”)) {
command = “where ” + “IEDriverServer”;
}
} else {
if (browserName.equalsIgnoreCase(“chrome”)) {
command = “which ” + “chromedriver”;
}
if (browserName.equalsIgnoreCase(“Firefox”)) {
command = “which ” + “geckodriver”;
}
}
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = “”;
while ((line = reader.readLine()) != null) {
actualDriverPath = line;
break;
}
p.destroy();
} catch (Exception e) {
e.printStackTrace();
}
return actualDriverPath;
}

   Try to use above methods to get the DRIVER_EXE path automatically into your automation script.

make it perfect !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s