java 如何让 ChromeDriver 路径不被硬编码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32723558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:40:41  来源:igfitidea点击:

How can I have the ChromeDriver path not be hardcoded?

javaselenium-webdriver

提问by user1210304

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

在我的 selenium 测试中,我对 ChromeDriver 的路径进行了硬编码

        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..

但是,这意味着当我将程序导出为可运行的 jar 文件时,该测试将不起作用,因为它找不到硬编码路径(如果它在另一台计算机上)。有什么办法可以做到吗?

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

a.) - ChromeDriver 是 jar 的一部分并与它一起运行
b.) - 我可以在代码中初始化它,而无需像那样对值进行硬编码

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

大多数情况下,我希望通过运行 jar 来运行所有测试(在 IE、Chrome、FF 中)。

采纳答案by pmartin8

A solution would be to include the .exe in your jar (don't forget to include it in your buildpath). Then, in your application, you get read the content of the .exe from your jar using getResourceAsStream("path_to_your_exe_file").

一个解决方案是将 .exe 包含在您的 jar 中(不要忘记将它包含在您的构建路径中)。然后,在您的应用程序中,您可以使用 .exe 从 jar 中读取 .exe 的内容getResourceAsStream("path_to_your_exe_file")

Then you create a copy of your exe file in a temp directory. See Apache commons IO. Using FileUtils.copy(in, out).

然后在临时目录中创建 exe 文件的副本。请参阅 Apache 公共 IO。使用FileUtils.copy(in, out).

Then you can reference your exe file using the good old System.setProperty("webdriver.chrome.driver",C:\\Users\\kday\\Desktop\\Selenium Stuff\\chromedriver.exe");

然后你可以使用旧的引用你的exe文件 System.setProperty("webdriver.chrome.driver",C:\\Users\\kday\\Desktop\\Selenium Stuff\\chromedriver.exe");

回答by pmartin8

The only way is to carry the file inside your jar. You will need to copy chromedriver.exe inside the main/resources folder of your project and reference it using the class loader ie. ClassLoader.getResource().

唯一的方法是将文件放入 jar 中。您需要将 chromedriver.exe 复制到项目的 main/resources 文件夹中,并使用类加载器 ie 引用它。ClassLoader.getResource().

If you do so, make sure your resources folder is included in your buildpath. To do this in Eclipse right click on project -> properties -> Java build path

如果这样做,请确保您的资源文件夹包含在您的构建路径中。为此,在 Eclipse 中右键单击项目 -> 属性 -> Java 构建路径

URL url = classLoader.getResource("chromedriver.exe");
System.setProperty("webdriver.chrome.driver", url.toString());

I'm not sure what the url.toString() will return if the driver is located in the jar. You'll need to try it out and see for yourself.

如果驱动程序位于 jar 中,我不确定 url.toString() 将返回什么。你需要尝试一下,看看自己。

回答by JeffC

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

您可以在项目下添加一个 Chrome 驱动程序文件夹,然后使用

System.getProperty("user.dir");

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

返回项目路径,然后使用相对路径查找驱动程序。

回答by pmartin8

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

更好的是,您可以将驱动程序导入为 Jar 而不是 .exe

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

罐子在这里。
http://www.java2s.com/Code/Jar/s/Downloadseleniumchromedriver20a4jar.htm

回答by zappee

You can use the webdrivermanagermaven dependency to download and managed binary webdrivers for Selenium:

您可以使用webdrivermanagermaven 依赖项来下载和管理 Selenium 的二进制 webdrivers:

If you use Selenium WebDriver, you will know that in order to use some browsers such as Chrome, Firefox, Opera, PhantomJS, Microsoft Edge, or Internet Explorer, first you need to download a binary file which allows WebDriver to handle browsers. In Java, the absolute path to this binary must be set as JVM properties, as follows:

System.setProperty("webdriver.chrome.driver", "/absolute/path/to/binary/chromedriver"); System.setProperty("webdriver.gecko.driver", "/absolute/path/to/binary/geckodriver"); System.setProperty("webdriver.opera.driver", "/absolute/path/to/binary/operadriver"); System.setProperty("phantomjs.binary.path", "/absolute/path/to/binary/phantomjs"); System.setProperty("webdriver.edge.driver", "C:/absolute/path/to/binary/MicrosoftWebDriver.exe"); System.setProperty("webdriver.ie.driver", "C:/absolute/path/to/binary/IEDriverServer.exe");

This is quite annoying since it forces you to link directly this binary file into your source code. In addition, you have to check manually when new versions of the binaries are released. WebDriverManager comes to the rescue, performing in an automated way all this dirty job for you.

如果您使用 Selenium WebDriver,您就会知道,为了使用某些浏览器,例如 Chrome、Firefox、Opera、PhantomJS、Microsoft Edge 或 Internet Explorer,您首先需要下载一个允许 WebDriver 处理浏览器的二进制文件。在 Java 中,此二进制文件的绝对路径必须设置为 JVM 属性,如下所示:

System.setProperty("webdriver.chrome.driver", "/absolute/path/to/binary/chromedriver"); System.setProperty("webdriver.gecko.driver", "/absolute/path/to/binary/geckodriver"); System.setProperty("webdriver.opera.driver", "/absolute/path/to/binary/operadriver"); System.setProperty("phantomjs.binary.path", "/absolute/path/to/binary/phantomjs"); System.setProperty("webdriver.edge.driver", "C:/absolute/path/to/binary/MicrosoftWebDriver.exe"); System.setProperty("webdriver.ie.driver", "C:/absolute/path/to/binary/IEDriverServer.exe");

这很烦人,因为它迫使您将这个二进制文件直接链接到您的源代码中。此外,当发布新版本的二进制文件时,您必须手动检查。WebDriverManager 来救援,以自动化的方式为您执行所有这些肮脏的工作。

This is the maven dependency what you need to add to your pom.xml file:

这是您需要添加到 pom.xml 文件的 maven 依赖项:

<dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>3.0.0</version>
    <scope>test</scope>
</dependency>