Question

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.

Was it helpful?

Solution 2

I solved the issue:

  1. Put the chromedriver.exe in a known path (easy way is to simply drag the file from Windows Explorer into Visual Studio)
  2. Use this relative path syntax: WebDriver = new ChromeDriver(@"../../");

The key is to find the correct path where the driver lives with something like this:

System.IO.Directory.GetCurrentDirectory();

OTHER TIPS

Add the drivers into your solution and in the properties window, define that you want to copy the files to the output directory.

File Properties

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");

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);
        }

This worked for me in java :

System.setProperty(“webdriver.chrome.driver”,
      new File(“./src/test/resources/drivers/chromedriver.exe”).getCanonicalPath());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top