java - org.openqa.selenium.remote.service.DriverService$Builder.createArgs()Lcom/google/common/collect/ImmutableList;使用 Selenium 3.5.3 Chrome 76

标签 java selenium selenium-chromedriver guava junit-runner

我正在使用 IntelliJ 编写 Selenium Junit 测试。如果我直接从测试触发,测试运行正常。但是,如果我使用 JunitCore 从 TestRunnerSuite 触发测试,我遇到了以下奇怪的错误,我在谷歌研究后没有找到解决方案。关于 DriverService$builder 的类似问题,但不是我的错误类型。

[main] ERROR sire.responseOrg.TestIncidents - java.lang.AbstractMethodError: org.openqa.selenium.remote.service.DriverService$Builder.createArgs()Lcom/google/common/collect/ImmutableList;
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:332)
    at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
    at sire.responseOrg.WebDrivers.getInstance(WebDrivers.java:15)
    at sire.responseOrg.util.util1.setupChromeDriver(util1.java:51)
    at sire.responseOrg.Test1.setUp(Test1.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at ......Omitted
    at org.junit.runner.JUnitCore.run(JUnitCore.java:127)
    at org.junit.runner.JUnitCore.runClasses(JUnitCore.java:76)
    at sire.responseOrg.TestSuiteRunner.main(TestSuiteRunner.java:24)

我正在使用 Selenium 3.5.3 和 chrome 76。---> 已更新到 Selenium 3.141.59,并具有 main 作用域。
现在出现错误

java.lang.NoClassDefFoundError: org/apache/http/auth/Credentials
    at org.openqa.selenium.remote.HttpCommandExecutor.getDefaultClientFactory(HttpCommandExecutor.java:93)
    at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:72)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.<init>(DriverCommandExecutor.java:63)
    at org.openqa.selenium.chrome.ChromeDriverCommandExecutor.<init>(ChromeDriverCommandExecutor.java:36)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:181)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:168)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
    at sire.responseOrg.WebDrivers.getInstance(WebDrivers.java:15)
    at sire.responseOrg.util.SeleniumUtil.setupChromeDriver(SeleniumUtil.java:62)
    at sire.responseOrg.TestIncidents.setUp(TestIncidents.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:24)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:127)
    at org.junit.runner.JUnitCore.runClasses(JUnitCore.java:76)
    at sire.responseOrg.TestSuiteRunner.main(TestSuiteRunner.java:24)
Caused by: java.lang.ClassNotFoundException: org.apache.http.auth.Credentials
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 33 more

完整的 pom.xml 依赖项

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>myGroupId</groupId>
    <artifactId>myArtifactId</artifactId>
    <version>1.0-SNAPSHOT</version>
    <description>My description</description>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.9</version>
            <scope>main</scope>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-api</artifactId>
            <version>3.141.59</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>3.141.59</version>
            <scope>main</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.6</version>
            <scope>main</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.6</version>
            <scope>main</scope>
        </dependency>
        <dependency>
            <groupId>com.salesforce.seti</groupId>
            <artifactId>selenium-dependencies</artifactId>
            <version>1.0.3</version>
        </dependency>


    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.5.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.1.2</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>test-jar</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
    <packaging>pom</packaging>

</project>

我的项目文件夹结构是

.src
...main
.....java
.......projectname
.........constantsFolder
.........utilFolder
...........util1.java
...........util2.java
.........Test1.java
.........TestRunnerSuite.java
.........WebDrivers.java

如果我从 Test1.java 开始测试,测试会定期运行,但会出现警告

[main] INFO projectname.util.util1 - Set up chrome driver.
Starting ChromeDriver 75.0.3770.90 (a6dcaf7e3ec6f70a194cc25e8149475c6590e025-refs/branch-heads/3770@{#1003}) on port 28755
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
[1566609934.853][WARNING]: This version of ChromeDriver has not been tested with Chrome version 76.
Aug 23, 2019 6:25:34 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
[main] INFO projectname.util.util1 - Navigating to https://mytest.com/

但是,在如下添加 testSuiteRunner 之后。

@RunWith(Suite.class)
@Suite.SuiteClasses({ Test1.class })
public class TestSuiteRunner {
    public static void main(String[] args) {
        Result result = JUnitCore.runClasses(Test1.class);
        // print erros, exit etc omitted
    }
}

现在我得到了奇怪的错误并且无法启动 chromedriver。 我的 webdriver 是单例的

public class WebDrivers {
    private static WebDriver driver = null;

    public static WebDriver getInstance(){
        if (driver == null) {
            driver = new ChromeDriver();
        }
        return driver;
    }
}

这是我第一次从地面开始设置一切。我不确定这是 pom 依赖问题、单例 webdriver 问题还是其他问题。任何人都可以对此发表看法并提供一些线索吗?非常感激。

最佳答案

这个错误信息...

java.lang.AbstractMethodError: org.openqa.selenium.remote.service.DriverService$Builder.createArgs()Lcom/google/common/collect/ImmutableList;

...暗示您使用的二进制文件版本与 guava 依赖性 之间存在一些不兼容。 p>

  • 您正在使用 chrome= 76.0
  • 您正在使用以下内容:

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-chrome-driver</artifactId>
        <version>3.5.3</version>
        <scope>test</scope>
    </dependency>
    
  • 您的 Selenium Client 版本是 3.5.3,比 2 早了。

  • 我们不知道您的JDK 版本

因此 Selenium 客户端 v3.5.3Chrome 浏览器 v76.0

之间存在明显的不匹配

但是根据以下讨论:

这些问题的出现是由于不兼容的 Guava 依赖。

The current guava version used within selenium-java-3.141.59 is guava-25.0-jre


解决方案

确保:

  • JDK 已升级到当前级别 JDK 8u222 .
  • Selenium 已升级到当前级别 Version 3.141.59 .
  • 清理您的项目工作区,通过您的IDE重建您的项目,仅使用所需的依赖项。
  • 如果您的基础 Web Client 版本太旧,则将其卸载并安装最新的 GA 和发布版本的 Web Client
  • 系统重启
  • 非root用户身份执行您的@Test
  • 始终在 tearDown(){} 方法中调用 driver.quit() 以关闭和销毁 WebDriverWeb 客户端 实例优雅。

更新

所以大概是您关于错误的主要问题:

java.lang.AbstractMethodError: org.openqa.selenium.remote.service.DriverService$Builder.createArgs()Lcom/google/common/collect/ImmutableList;

已解决。恭喜。

现在,根据您看到的错误更新问题:

java.lang.NoClassDefFoundError: org/apache/http/auth/Credentials

有两个方面。

关于java - org.openqa.selenium.remote.service.DriverService$Builder.createArgs()Lcom/google/common/collect/ImmutableList;使用 Selenium 3.5.3 Chrome 76,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57634492/

相关文章:

java - OpenAM 与 Maven

java - 在 Xml 中标记特定字符串

java - 使用 Akka Actor 的文件操作

javascript - 使用 Nightwatch.js 测试下载链接

python - 在自动点击下载链接后,使用 Selenium、Firefox、Python 将 EPS 文件下载保存到磁盘

java - 单击具有相同名称的不同 html 类

selenium-chromedriver - 使用 Chrome 驱动程序的 C# Selenium 代理身份验证

c# - Azure DevOps 服务器 : How to prevent 'Access to path is denied' when initalizing a job?

java - JFrame 和 MouseListener 无法正确执行单击的问题

java - 从下拉列表中随机选择?