Tuesday 31 March 2020

Tip : Set relative path for selenium chrome web driver

In this post, you will see java snippet inputs for  calling chromedriver.exe file eclipse project relative path.

1) Create folder "drivers" for the project.

2) Upload the chromedriver.exe file as shown in below image. (chromedriver_80 another folder created under drivers folder)

3) Right click on the "chromedriver.exe" and make note of the path
     for example : /Jaspersoft Automation/drivers/chromedriver_80/chromedriver.exe

4) Define a key-value pair for the driver path in configuration.properties file
     chrome_driver_path=\\drivers\\chromedriver_80\\chromedriver.exe

5) While setting System property for driver, give the relative path 
System.setProperty("webdriver.chrome.driver",path+chromeDriverPath);

Run the project using Junit and observe that the driver instantiated from the relative path provided.

Detailed code snippets follows....



configuration.properties 

# Give this relative path of the driver uploaded for "drivers" folder in eclipse project.
chrome_driver_path=\\drivers\\chromedriver_80\\chromedriver.exe

Code snippet in CommonFunctions.java file i.e., instantiating the chrome driver

public void initDriver() {

String chromeDriverPath=PropertyManager.getInstance().getChormeDriverPath();

String path = System.getProperty("user.dir");
System.out.println(path); 

System.setProperty("webdriver.chrome.driver",path+chromeDriverPath);

}

getChromeDriverPath() is the method defined in PropertyManager.java

public class PropertyManager {

     private static PropertyManager instance;
    private static final Object lock = new Object();
    //private static String propertyFilePath=System.getProperty("user.dir") + "\\configuration.properties"; 
    private static String app_mangt_url;
    private static String auth_uname_pwd_autoit_path;
    private static String chrome_driver_path;
    private static String browser_type;
    private static String ie_driver_path;
    private static String auth_uname_pwd_autoit_path_ie;
 
    //Create a Singleton instance. We need only one instance of Property Manager.
    public static PropertyManager getInstance () {
        if (instance == null) {
            synchronized (lock) {
                instance = new PropertyManager();
                instance.loadData();
            }
        }
        return instance;
    }

    //Get all configuration data and assign to related fields.
    private void loadData() {
        //Declare a properties object
        Properties prop = new Properties();

        //Read configuration.properties file
        try {
            //prop.load(new FileInputStream(propertyFilePath));
                               prop.load(this.getClass().getClassLoader().getResourceAsStream("configuration.properties"));
        } catch (IOException e) {
            System.out.println("Configuration properties file cannot be found");
        }

        //Get properties from configuration.properties
        app_mangt_url = prop.getProperty("app_mangt_url");
        auth_uname_pwd_autoit_path=prop.getProperty("auth_uname_pwd_autoit_path");
        chrome_driver_path=prop.getProperty("chrome_driver_path");
        browser_type=prop.getProperty("browser_type");
        ie_driver_path=prop.getProperty("ie_driver_path");
     
        auth_uname_pwd_autoit_path_ie=prop.getProperty("auth_uname_pwd_autoit_path_ie");

    }

    public String getAppMangtURL () {
      return app_mangt_url;
    }
    public String getAuthUnamePwdAutoIT () {
        return auth_uname_pwd_autoit_path;
      }
    public String getChormeDriverPath () {
        return chrome_driver_path;
      }
    public String getbrowserType () {
        return browser_type;
      }
    public String getIEDriverPath () {
        return ie_driver_path;
      }
    public String getAuthUnamePwdAutoITIE () {
        return auth_uname_pwd_autoit_path_ie;
      }

}

Hope this helps you.

Cheers.!

No comments:

Post a Comment