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

时间:2019-08-24 01:35:19

标签: 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和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

完整的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是Singleton

public class WebDrivers {
    private static WebDriver driver = null;

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

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

3 个答案:

答案 0 :(得分:5)

此错误消息...

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

...表示您使用的二进制版本之间特别存在 guava 依赖。 >

  • 您正在使用 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 Client v3.5.3 Chrome浏览器v76.0

之间显然不匹配

但是根据以下讨论:

由于不兼容的 Guava 依赖性,这些问题浮出水面。

  

selenium-java-3.141.59 中使用的当前 guava 版本是 guava-25.0-jre


解决方案

确保:

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

更新

大概是您对错误的主要疑问:

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

已解决。恭喜你

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

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

有两个方面。

答案 1 :(得分:2)

从POM中删除范围。  测试 或主要 不需要运行测试

答案 2 :(得分:0)

在最新的 testng 7.3 中使用了这个 gauva jar。解决了这个错误也不要单独配置任何 testNG。如果我们在 pom.xml

中添加配置,请删除它