Changing driver (i.e. from Chrome driver to firefox driver) from properties file

Create a property file (property) In the property file enter

Webdrivername = org.openqa.selenium.chrome.ChromeDriver

Next time if need to run script in say IE, Firefox, Opera just change the content in property file.

Below is the Java code which can be used...

public WebDriver driver  = browserDriver();
public static WebDriver drv  = null;



public static WebDriver browserDriver() {

File file1 = new File(/*The path for property the driver of that browser */);

if(drv==null){
try{

switch (sdriver) {

case "chrome":
File FileChrome  = new File(file1+"/chromedriver.exe");
System.setProperty("webdriver.chrome.driver", FileChrome.getAbsolutePath());
browserName("org.openqa.selenium.chrome.ChromeDriver");
break;

case "ie":
File fileIE = new File(file1+"/IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", fileIE.getAbsolutePath());
browserName("org.openqa.selenium.ie.InternetExplorerDriver");
   break;
 
case "ff":
browserName("org.openqa.selenium.firefox.FirefoxDriver");
break;
case "opera":
File FileOpera = new File(file1+"/operadriver.exe");
System.setProperty("webdriver.opera.driver", FileOpera.getAbsolutePath());
browserName("org.openqa.selenium.opera.OperaDriver");
break;

default:
browserName("org.openqa.selenium.safari.SafariDriver");
break;
}
}catch(ClassNotFoundException e){
e.printStackTrace();
ApplicationLogger.logData("Driver is not found");
System.out.println("Driver is not found");
}catch(NoSuchMethodException e){
e.printStackTrace();
ApplicationLogger.logData("Driver is not found");
System.out.println("Driver is not found");
}catch(SecurityException e){
e.printStackTrace();
ApplicationLogger.logData("Driver is not found");
System.out.println("Driver is not found");
}catch(InstantiationException e){
e.printStackTrace();
ApplicationLogger.logData("Driver is not found");
System.out.println("Driver is not found");
}catch(IllegalAccessException e){
e.printStackTrace();
ApplicationLogger.logData("Driver is not found");
System.out.println("Driver is not found");
}catch(InvocationTargetException e){
e.printStackTrace();
ApplicationLogger.logData("Driver is not found");
System.out.println("Driver is not found");
}
}
return d;
}



public static WebDriver browserName(String x) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException{
@SuppressWarnings("unchecked")
Class claz = (Class) Class.forName(x);
Constructor cons = claz.getConstructor();
d = (WebDriver) cons.newInstance(new Object[]{});
drv=d;

return d;
}

Comments

Anonymous said…
very good thank you