Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I have the ChromeDriver path not be hardcoded?

In my selenium tests, I have the path to ChromeDriver hardcoded with

        System.setProperty("webdriver.chrome.driver", "C:\\Users\\kday\\Desktop\\Selenium Stuff\\chromedriver.exe");
        WebDriver driver = new ChromeDriver(); 

However, this means that when I export the program as a runnable jar file, that the test will not work since it cannot find the hardcoded path (if it is on another computer). Is there any way to make it so that either..

a.) - ChromeDriver is part of the jar and runs with it
b.) - I can initialize it in the code without hardcoding the value in like that

Mostly, I want all the tests to run (in IE, Chrome, FF) by just running the jar.

like image 827
J. Doe Avatar asked Sep 22 '15 17:09

J. Doe


2 Answers

Better yet, you can import the driver as a Jar instead of a .exe

The jar is here.
http://www.java2s.com/Code/Jar/s/Downloadseleniumchromedriver20a4jar.htm

like image 158
pmartin8 Avatar answered Oct 25 '22 15:10

pmartin8


You can add a Chrome driver folder under the project then use

System.getProperty("user.dir");

to return the project path and then use a relative path to find the driver.

like image 32
JeffC Avatar answered Oct 25 '22 15:10

JeffC