java - Geb 和 Spring-Boot 元素未找到错误

标签 java spring spring-mvc spring-boot geb

我正在使用 Geb 对我的 Spring-Boot 应用程序运行完整的堆栈测试。我正在使用 Spring 提供的 MockMvcHtmlUnitDriver 在测试中加载我的上下文。

这是我的测试:

    package com.foo.test

    import com.foo.config.FooConfiguration
    import geb.Page
    import geb.spock.GebReportingSpec
    import org.springframework.beans.factory.annotation.Autowired
    import org.springframework.test.context.ContextConfiguration
    import org.springframework.test.context.web.WebAppConfiguration
    import org.springframework.test.web.servlet.htmlunit.webdriver.MockMvcHtmlUnitDriverBuilder
    import org.springframework.web.context.WebApplicationContext

    @ContextConfiguration(classes = FooConfiguration)
    @WebAppConfiguration
    class BetaRequestTest extends GebReportingSpec{

        @Autowired
        WebApplicationContext webApplicationContext;

        def setup() {
            browser.driver = MockMvcHtmlUnitDriverBuilder
                    .webAppContextSetup(webApplicationContext).build()
        }

        def destroy(){
            browser.driver?.close()
        }

        def "should render the page"(){
            setup:
                to BetaRequestPage
                at BetaRequestPage
        }

    }

    class BetaRequestPage extends Page {
        static url = "/"
        static at = {assert heading == "Welcome to Foo"}
        static content = {
            heading { $("h1", 0)}
        }
    }

由于没有此类元素错误,测试总是失败:

   org.openqa.selenium.NoSuchElementException: Unable to locate element using css: com.gargoylesoftware.htmlunit.TextPage@6fee2139
    For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
    Build info: version: '2.45.0', revision: '5017cb8e7ca8e37638dc3091b2440b90a1d8686f', time: '2015-02-27 09:10:26'
    System info: host: 'DESKTOP-8N2CCGT', ip: '192.168.1.100', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_65'
    Driver info: driver.version: HtmlUnitDriver
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementsByCssSelector(HtmlUnitDriver.java:1008)
        at org.openqa.selenium.By$ByCssSelector.findElements(By.java:436)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElements(HtmlUnitDriver.java:1657)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElements(HtmlUnitDriver.java:555)
        at geb.navigator.SearchContextBasedBasicLocator.find(SearchContextBasedBasicLocator.groovy:49)

我可以在标准输出日志中看到 Spring 正在加载并服务请求:

  19:33:34.045 [Test worker] DEBUG c.g.htmlunit.WebClient - Get page for window named '', using WebRequest[<url="http://localhost/", GET, EncodingType[name=application/x-www-form-urlencoded], [], {Accept=*/*, Accept-Encoding=gzip, deflate}, null>]
    19:33:34.046 [Test worker] DEBUG c.g.htmlunit.WebClient - Load response for GET http://localhost/
    19:33:34.059 [Test worker] DEBUG o.s.t.w.s.TestDispatcherServlet - DispatcherServlet with name '' processing GET request for [/]
    19:33:34.062 [Test worker] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /
    19:33:34.062 [Test worker] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Did not find handler method for [/]
    19:33:34.063 [Test worker] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping - Mapping [/] to HandlerExecutionChain with handler [org.springframework.web.servlet.mvc.ParameterizableViewController@4982a084] and 1 interceptor
    19:33:34.063 [Test worker] DEBUG o.s.t.w.s.TestDispatcherServlet - Last-Modified value for [/] is: -1
    19:33:34.064 [Test worker] DEBUG o.s.w.s.v.ContentNegotiatingViewResolver - Requested media types are [*/*] based on Accept header types and producible media types [*/*])
    19:33:34.064 [Test worker] DEBUG o.s.w.s.view.BeanNameViewResolver - No matching bean found for view name 'forward:index.html'
    19:33:34.064 [Test worker] DEBUG o.s.w.s.v.ContentNegotiatingViewResolver - Returning [org.springframework.web.servlet.view.InternalResourceView: unnamed; URL [index.html]] based on requested media type '*/*'
    19:33:34.065 [Test worker] DEBUG o.s.t.w.s.TestDispatcherServlet - Rendering view [org.springframework.web.servlet.view.InternalResourceView: unnamed; URL [index.html]] in DispatcherServlet with name ''
    19:33:34.065 [Test worker] DEBUG o.s.w.s.view.InternalResourceView - Forwarding to resource [index.html] in InternalResourceView 'null'
    19:33:34.065 [Test worker] DEBUG o.s.mock.web.MockRequestDispatcher - MockRequestDispatcher: forwarding to [index.html]
    19:33:34.065 [Test worker] DEBUG o.s.t.w.s.TestDispatcherServlet - Successfully completed request
    19:33:34.066 [Test worker] DEBUG o.s.t.w.s.TestDispatcherServlet - DispatcherServlet with name '' processing GET request for [/]
    19:33:34.066 [Test worker] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path index.html
    19:33:34.068 [Test worker] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Did not find handler method for [index.html]
    19:33:34.068 [Test worker] WARN  o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/] in DispatcherServlet with name ''
    19:33:34.068 [Test worker] DEBUG o.s.t.w.s.TestDispatcherServlet - Successfully completed request
    19:33:34.070 [Test worker] DEBUG c.g.htmlunit.html.HtmlElement - Firing Event unload (Current Target: HTMLElement for HtmlHtml[<html>]);
    19:33:34.071 [Test worker] DEBUG c.g.htmlunit.WebWindowImpl - setEnclosedPage: com.gargoylesoftware.htmlunit.TextPage@6fee2139
    19:33:34.071 [Test worker] DEBUG c.g.htmlunit.WebWindowImpl - destroyChildren
    19:33:34.164 [Test worker] DEBUG o.s.t.c.s.AbstractDirtiesContextTestExecutionListener - After test method: context [DefaultTestContext@131136f6 testClass = BetaRequestTest, testInstance = com.foo.test.BetaRequestTest@191c9106, testMethod = $spock_feature_2_0@BetaRequestTest, testException = org.openqa.selenium.NoSuchElementException: Unable to locate element using css: com.gargoylesoftware.htmlunit.TextPage@6fee2139
    For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html

我做错了什么?当我运行应用程序时,Html 服务正确。 index.html 文件位于 WebApp 的 jar 中,我的测试对 WebApp jar 具有 TestCompile 依赖性。

我刚刚注意到,如果我将网址从“/”更改为“/index.html”,它会尝试加载我的页面。不确定为什么 View 解析器不起作用。另外,由于某种原因,HtmlUnit 找不到函数 document.querySelectory。

最佳答案

您最好在实际应用程序中直接使用非 headless 驱动程序,而不是使用 MockMvcHtmlUnitDriverBuilder 因为 headless 驱动程序在一定程度上受到限制 - HtmlUnit 驱动程序也不处理 javascript好吧,特别是如果它很复杂,比如使用 jQuery 和 PhantomJS 驱动程序时(当前未维护)[ https://github.com/detro/ghostdriver#help-needed] 。我总是建议使用真正的浏览器,并在需要时以 headless 模式运行它(例如使用 Xvfb 或 Xephyr)。

关于java - Geb 和 Spring-Boot 元素未找到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34079160/

相关文章:

java - 我有这个错误 java.lang.NoSuchMethodError

java - 带有两个 Spring 配置上下文 XML 文件的 bean 引用

java - Spring AOP - @Around Error at::0 在切入点中正式未绑定(bind)

java - 应用程序扩展 Neo4jConfiguration 时无法使用 InterceptorRegistry

Java 使用时区

java - 加载其他内容时加载对话框不会绘制

java - Java 中应有 <identifier>

java - 如何检查鼠标是否在处理 3 中的 Canvas 上?

java - Spring MVC : Fallback for unknown language code in uri parameter value

java - 如何将 ArrayList 从 Jsp 传递到 Spring(注释基础) Controller