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

use*_*691 3 java selenium guava selenium-chromedriver 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)
Run Code Online (Sandbox Code Playgroud)

我正在使用Selenium 3.5.3和chrome76。--->已更新为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
Run Code Online (Sandbox Code Playgroud)

完整的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>
Run Code Online (Sandbox Code Playgroud)

我的项目文件夹结构是

.src
...main
.....java
.......projectname
.........constantsFolder
.........utilFolder
...........util1.java
...........util2.java
.........Test1.java
.........TestRunnerSuite.java
.........WebDrivers.java
Run Code Online (Sandbox Code Playgroud)

如果我从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/
Run Code Online (Sandbox Code Playgroud)

但是,在添加如下的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
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,我收到了奇怪的错误,无法启动chromedriver。我拥有的Webdriver是Singleton

public class WebDrivers {
    private static WebDriver driver = null;

    public static WebDriver getInstance(){
        if (driver == null) {
            driver = new ChromeDriver();
        }
        return driver;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我第一次尝试从地面进行一切设置。我不确定是pom依赖问题,单例webdriver问题还是其他原因。谁能对此发表看法并提供一些线索?非常感激。

Deb*_*anB 5

此错误消息...

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

...暗示您正在使用的二进制版本之间特别不兼容guava 依赖项

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

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-chrome-driver</artifactId>
        <version>3.5.3</version>
        <scope>test</scope>
    </dependency>
    
    Run Code Online (Sandbox Code Playgroud)
  • 您的Selenium Client版本为3.5.3,该版本早于2年。

  • 您不知道您的JDK版本

因此,Selenium Client v3.5.3Chrome浏览器v76.0之间存在明显的不匹配

但是,根据以下讨论:

这些问题归因于不相容的番石榴依赖。

内使用的当前番石榴版本selenium-java-3.141.59guava-25.0-jre


确保这件事:

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

更新资料

因此,大概是您对该错误的主要问题:

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

解决。恭喜你

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

java.lang.NoClassDefFoundError: org/apache/http/auth/Credentials
Run Code Online (Sandbox Code Playgroud)

有两个方面。