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

时间:2015-12-04 01:00:56

标签: 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依赖项。

我刚刚注意到,如果我从&#39; /&#39;更改我的网址到&#39; /index.html'它确实尝试加载我的页面。不确定为什么视图解析器不起作用。此外,由于某种原因,HtmlUnit无法找到函数document.querySelectory。

1 个答案:

答案 0 :(得分:1)

你最好再次使用非无头驱动程序再使用真正的应用程序而不使用MockMvcHtmlUnitDriverBuilder,因为无头驱动程序仅限于某种程度 - HtmlUnit驱动程序不能很好地处理javascript,尤其是如果它很复杂,比如使用jQuery和PhantomJS驱动程序(目前没有维护)[https://github.com/detro/ghostdriver#help-needed]。我总是建议使用真正的浏览器并在需要时以无头模式运行(例如使用Xvfb或Xephyr)。