It works using an absolute path like:
WebDriver = new ChromeDriver(@"C:\Users\<my user>\Documents\<my project>\WebDrivers\Chrome\");
But since I run my tests on a TFS build server too, I need it to be a relative path.
Add the drivers into your solution and in the properties window, define that you want to copy the files to the output directory.
In the picture above, the drivers are in the resources directory:
/my solution
/resources
/chromedriver.exe
/IEDriverServer.exe
After a build, they will be copied to:
/bin
/debug
/resources
/chromedriver.exe
/IEDriverServer.exe
When you are creating your driver, you can define the path to the driver now relative in the bin directory.
return new ChromeDriver("resources");
I solved the issue:
WebDriver = new ChromeDriver(@"../../");
The key is to find the correct path where the driver lives with something like this:
System.IO.Directory.GetCurrentDirectory();
This worked for me in java :
System.setProperty(“webdriver.chrome.driver”,
new File(“./src/test/resources/drivers/chromedriver.exe”).getCanonicalPath());
you may have enum for your all drivers:
public enum Drivers
{
Chrome,
Firefox,
Safari,
Edge,
IE
}
public static IWebDriver GetDriver(Drivers driver)
{
var outPutDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
// below is my location where I copied all drivers like chromedriver.exe
var relativePath = @"..\..\bin\Debug\BrowserDriver";
var chromeDriverPath = Path.GetFullPath(Path.Combine(outPutDirectory,relativePath));
// return this driver , just debug this code and check the "outPutDirectory" path
return new ChromeDriver(chromeDriverPath);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.