由于依赖性问题,无法在Geb-Spock Grails测试中使用HtmlUnitDriver

时间:2014-05-19 12:11:54

标签: maven grails spock geb htmlunit-driver

我正在尝试在Grails 2.3.7中的Geb-Spock功能测试中使用HtmlUnitDriver。我使用Maven构建项目。

当我使用此驱动程序运行测试时,我得到:

java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal

我认为我收到此错误,因为我必须排除xml-apis

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-htmlunit-driver</artifactId>
    <version>2.41.0</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
        </exclusion>
    </exclusions>
</dependency>

但是我必须这样做,因为否则当我运行maven并且单元测试开始时我会收到错误:

Fatal error forking Grails JVM: java.lang.reflect.InvocationTargetException
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at org.grails.launcher.GrailsLauncher.launch(GrailsLauncher.java:150)
    at org.grails.maven.plugin.tools.ForkedGrailsRuntime.main(ForkedGrailsRuntime.java:168)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.grails.launcher.GrailsLauncher.launch(GrailsLauncher.java:144)
... 1 more
Caused by: java.lang.LinkageError: loader constraint violation: when resolving overridden method "org.apache.tools.ant.helper.ProjectHelper2$RootHandler.setDocumentLocator(Lorg/xml/sax/Locator;)V" the class loader (instance of org/grails/launcher/RootLoader) of the current class, org/apache/tools/ant/helper/ProjectHelper2$RootHandler, and its superclass loader (instance of <bootloader>), have different Class objects for the type andler.setDocumentLocator(Lorg/xml/sax/Locator;)V used in the signature
at org.apache.tools.ant.helper.ProjectHelper2.parseUnknownElement(ProjectHelper2.java:131)
at org.apache.tools.ant.helper.ProjectHelper2.parseAntlibDescriptor(ProjectHelper2.java:111)
at org.apache.tools.ant.taskdefs.Antlib.createAntlib(Antlib.java:91)
at org.apache.tools.ant.taskdefs.Definer.loadAntlib(Definer.java:440)
at org.apache.tools.ant.taskdefs.Definer.execute(Definer.java:292)
at org.apache.tools.ant.ComponentHelper.checkNamespace(ComponentHelper.java:877)
at org.apache.tools.ant.ComponentHelper.getDefinition(ComponentHelper.java:308)
at org.apache.tools.ant.ComponentHelper.createComponent(ComponentHelper.java:285)
at org.apache.tools.ant.ComponentHelper.createComponent(ComponentHelper.java:264)
at org.apache.tools.ant.UnknownElement.makeObject(UnknownElement.java:417)
at org.apache.tools.ant.UnknownElement.maybeConfigure(UnknownElement.java:163)
at groovy.util.AntBuilder.performTask(AntBuilder.java:314)
at groovy.util.AntBuilder.nodeCompleted(AntBuilder.java:264)
at groovy.util.BuilderSupport.doInvokeMethod(BuilderSupport.java:147)
at groovy.util.AntBuilder.doInvokeMethod(AntBuilder.java:203)
at groovy.util.BuilderSupport.invokeMethod(BuilderSupport.java:64)
at org.codehaus.gant.GantBuilder.invokeMethod(GantBuilder.java:99)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:45)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.gant.GantBinding.initializeGantBinding(GantBinding.groovy:108)
at org.codehaus.gant.GantBinding.<init>(GantBinding.groovy:42)
at org.codehaus.groovy.grails.cli.GrailsScriptRunner.executeScriptWithCaching(GrailsScriptRunner.java:428)
at org.codehaus.groovy.grails.cli.GrailsScriptRunner.callPluginOrGrailsScript(GrailsScriptRunner.java:414)
at org.codehaus.groovy.grails.cli.GrailsScriptRunner.executeCommand(GrailsScriptRunner.java:378)
at org.codehaus.groovy.grails.cli.GrailsScriptRunner.executeCommand(GrailsScriptRunner.java:345)
... 6 more

所以我认为这是一种依赖性问题。 但我不知道如何处理它。 我尝试分析依赖关系树,但我不确定如何检查谁想要这个org/xml/sax/Locatororg/w3c/dom/ElementTraversal

我尝试从HtmlUnitDriver中排除不仅仅是xml-apis,还包括xalan和xerces,但它没有帮助。

pom.xml看起来像这样:

...
<properties>
    <grails.version>2.3.7</grails.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <h2.version>1.3.170</h2.version>
    <gebVersion>0.7.2</gebVersion>
    <seleniumVersion>2.41.0</seleniumVersion>
    <spockVersion>0.7</spockVersion>
    <gebSpockVersion>0.9.0-RC-1</gebSpockVersion>
</properties>

<dependencies>
    <dependency>
        <groupId>org.grails</groupId>
        <artifactId>grails-plugin-async</artifactId>
        <version>${grails.version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.grails.plugins</groupId>
                <artifactId>spock</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.grails</groupId>
        <artifactId>grails-plugin-rest</artifactId>
        <version>${grails.version}</version>
    </dependency>

    <dependency>
        <groupId>org.grails</groupId>
        <artifactId>grails-plugin-services</artifactId>
        <version>${grails.version}</version>
    </dependency>
    <dependency>
        <groupId>org.grails</groupId>
        <artifactId>grails-plugin-i18n</artifactId>
        <version>${grails.version}</version>
    </dependency>

    <dependency>
        <groupId>org.grails</groupId>
        <artifactId>grails-plugin-databinding</artifactId>
        <version>${grails.version}</version>
    </dependency>

    <dependency>
        <groupId>org.grails</groupId>
        <artifactId>grails-plugin-filters</artifactId>
        <version>${grails.version}</version>
    </dependency>

    <dependency>
        <groupId>org.grails</groupId>
        <artifactId>grails-plugin-gsp</artifactId>
        <version>${grails.version}</version>
    </dependency>

    <dependency>
        <groupId>org.grails</groupId>
        <artifactId>grails-plugin-log4j</artifactId>
        <version>${grails.version}</version>
    </dependency>

    <dependency>
        <groupId>org.grails</groupId>
        <artifactId>grails-plugin-servlets</artifactId>
        <version>${grails.version}</version>
    </dependency>

    <dependency>
        <groupId>org.grails</groupId>
        <artifactId>grails-plugin-url-mappings</artifactId>
        <version>${grails.version}</version>
    </dependency>

    <dependency>
        <groupId>org.grails</groupId>
        <artifactId>grails-resources</artifactId>
        <version>${grails.version}</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.grails</groupId>
        <artifactId>grails-test</artifactId>
        <version>${grails.version}</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.grails.plugins</groupId>
                <artifactId>spock</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.grails</groupId>
        <artifactId>grails-plugin-testing</artifactId>
        <version>${grails.version}</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.grails.plugins</groupId>
                <artifactId>spock</artifactId>
            </exclusion>
        </exclusions>
    </dependency>


    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>${h2.version}</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.grails.plugins</groupId>
        <artifactId>cache</artifactId>
        <version>1.1.1</version>
        <scope>compile</scope>
        <type>zip</type>
    </dependency>

    <dependency>
        <groupId>org.grails.plugins</groupId>
        <artifactId>hibernate</artifactId>
        <version>3.6.10.9</version>
        <scope>runtime</scope>
        <type>zip</type>
    </dependency>

    <dependency>
        <groupId>org.grails.plugins</groupId>
        <artifactId>database-migration</artifactId>
        <version>1.3.8</version>
        <scope>runtime</scope>
        <type>zip</type>
    </dependency>

    <dependency>
        <groupId>org.grails.plugins</groupId>
        <artifactId>jquery</artifactId>
        <version>1.11.0.2</version>
        <scope>runtime</scope>
        <type>zip</type>
    </dependency>

    <dependency>
        <groupId>org.grails.plugins</groupId>
        <artifactId>jquery-ui</artifactId>
        <version>1.10.3</version>
        <scope>runtime</scope>
        <type>zip</type>
    </dependency>

    <dependency>
        <groupId>org.grails.plugins</groupId>
        <artifactId>resources</artifactId>
        <version>1.2.7</version>
        <scope>runtime</scope>
        <type>zip</type>
    </dependency>

    <dependency>
        <groupId>org.grails.plugins</groupId>
        <artifactId>tomcat</artifactId>
        <version>7.0.52.1</version>
        <scope>provided</scope>
        <type>zip</type>
    </dependency>

    <dependency>
        <groupId>org.grails.plugins</groupId>
        <artifactId>spock</artifactId>
        <version>${spockVersion}</version>
        <scope>test</scope>
        <type>zip</type>
    </dependency>

    <dependency>
        <groupId>org.grails.plugins</groupId>
        <artifactId>geb</artifactId>
        <version>${gebVersion}</version>
        <scope>test</scope>
        <type>zip</type>
    </dependency>

    <dependency>
        <groupId>org.gebish</groupId>
        <artifactId>geb-spock</artifactId>
        <version>${gebSpockVersion}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.spockframework</groupId>
        <artifactId>spock-grails-support</artifactId>
        <version>0.7-groovy-1.8</version>
    </dependency>

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-htmlunit-driver</artifactId>
        <version>${seleniumVersion}</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>xalan</groupId>
                <artifactId>xalan</artifactId>
            </exclusion>
            <exclusion>
                <groupId>xml-apis</groupId>
                <artifactId>xml-apis</artifactId>
            </exclusion>
            <exclusion>
                <groupId>xerces</groupId>
                <artifactId>xercesImpl</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-chrome-driver</artifactId>
        <version>${seleniumVersion}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-firefox-driver</artifactId>
        <version>${seleniumVersion}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-support</artifactId>
        <version>${seleniumVersion}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.solr</groupId>
        <artifactId>solr-solrj</artifactId>
        <version>4.3.0</version>
        <exclusions>
            <exclusion>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.2.5</version>
    </dependency>
    <dependency>
        <groupId>commons-httpclient</groupId>
        <artifactId>commons-httpclient</artifactId>
        <version>3.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.2.5</version>
    </dependency>
    <dependency>
        <groupId>org.tuckey</groupId>
        <artifactId>urlrewritefilter</artifactId>
        <version>4.0.3</version>
    </dependency>

    <dependency>
        <groupId>co.freeside</groupId>
        <artifactId>betamax</artifactId>
        <version>1.1.2</version>
        <scope>test</scope>
    </dependency>

对于如何解决这个问题,我将不胜感激。

修改

我尝试过 erdi 建议的版本,所以:

selenium-htmlunit-driver  2.26.0
                     geb  0.9.0-RC-1
               geb-spock  0.9.0-RC-1
              (and spock  0.7)

我收到错误:

2014-05-23 14:37:06,008 [main] ERROR javascript.StrictErrorReporter  
- runtimeError: message=[The data necessary to complete this operation 
is not yet available.] 
sourceName=[http://localhost:8080/search-web-app/static/plugins/jquery-1.11.0.2/js/jquery/jquery-1.11.0.min.js] line=[2] lineSource=[null] lineOffset=[0]

FirefoxChrome驱动程序正常工作(测试通过)。 我希望HtmlUnit驱动程序工作的原因是我们希望能够在Jenkins服务器上运行测试。据我所知,HtmlUnit是允许它的那个,因为它不会打开浏览器(如果我理解错误,请纠正我。)

1 个答案:

答案 0 :(得分:1)

我能用Grails运行的HtmlUnitDriver的最新版本是2.26.0。无论如何,你应该真的避免使用HtmlUnitDriver,因为它不适用于更复杂的网页。您在测试中驾驶真正的浏览器几乎总是更好。

请注意,最新的Geb版本是0.9.2,你不应该为geb-spock和Grails Geb插件使用不同的版本(0.9.0-RC-1和0.7.2)。