java - Maven 存储库中的 ChromeDriver 未在测试脚本中执行

标签 java maven selenium selenium-webdriver selenium-chromedriver

在我的 pom 脚本中,我有:

<dependency>
   <groupId>org.seleniumhq.selenium</groupId>
   <artifactId>selenium-java</artifactId>
   <version>2.53.1</version>
</dependency>

MVNrepository 上的存储库描述列出了几个编译依赖项,包括 Selenium-Chrome-DriverSelenium-Firefox-Driver 等。我可以成功运行 Firefox-Driver 脚本,但是当我尝试运行相同的脚本时使用对象的脚本

ChromeDriver 驱动程序 = new ChromeDriver();

我收到以下错误:

java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html

无需下载 Firefox 驱动程序即可使用(这就是 pom 脚本的要点); Chrome 驱动程序有什么不同吗?下图显示它是从 Maven 脚本编译的。

Maven - ChromeDriver

测试脚本:

import org.junit.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import static org.junit.Assert.assertTrue;

public class MyFirstTest {

    @Test
    public void googleTest() {
        ChromeDriver driver = new ChromeDriver();
        driver.get("https://www.google.com/");
        assertTrue(driver.getTitle().equals("Google"));
        driver.quit();
    }
}

最佳答案

是的,它与 Firefox 不同,差异在 a page referenced by exception 上有解释。 :

The ChromeDriver consists of three separate pieces. There is the browser itself ("chrome"), the language bindings provided by the Selenium project ("the driver") and an executable downloaded from the Chromium project which acts as a bridge between "chrome" and the "driver". This executable is called "chromedriver", but we'll try and refer to it as the "server" in this page to reduce confusion.

因此,为了实例化 ChromeDriver,您需要

  1. this page 下载适合您平台的 ChromeDriver 二进制文件,
  2. 在实例化驱动程序时指定其路径(或者在 PATH 中定义它)。例如:

    System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
    ChromeDriver driver = new ChromeDriver();
    

    讨论了有关如何指定 ChromeDriver 二进制文件的所有各种选项 here

  3. Chrome 本身应该有一个定义的位置,以便 ChromeDriver 找到它。解释了如何做到这一点 here

关于java - Maven 存储库中的 ChromeDriver 未在测试脚本中执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39377593/

相关文章:

java - N 层系统中的异常

java - 我可以确定是否在复合组件中定义了 JSF2 构面吗?

java - 单元测试Springboot MockMvc返回403 Forbidden

java - 防止对象类创建

java - Apache 客户端请求配置生成器错误

java - 使用 spotify maven 插件构建 Docker 镜像时更改了小二进制文件

java - 从 Linux 终端启动 selenium webdriver 测试

java - 一种封装java应用程序的方法

javascript - 如何在 Visual Studio Code 中调试单个 Nightwatch JS 测试

java - 我在使用 selenium Jar 文件 : 时在 eclipse 中收到此错误