How can I instantiate a new ChromeDriver using a relative path in C#?

我们两清 提交于 2020-12-05 12:46:07

问题


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.


回答1:


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



回答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();




回答3:


This worked for me in java :

System.setProperty(“webdriver.chrome.driver”,
      new File(“./src/test/resources/drivers/chromedriver.exe”).getCanonicalPath());



回答4:


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


来源:https://stackoverflow.com/questions/22648279/how-can-i-instantiate-a-new-chromedriver-using-a-relative-path-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!