2015-12-04 23 views
0

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

这里是我的测试:

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: [email protected] 
    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) 

我可以在我的标准输出日志春天来加载和维修见要求:

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 [or[email protected]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: [email protected] 
    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 [[email protected] testClass = BetaRequestTest, testInstance = [email protected], testMethod = [email protected], testException = org.openqa.selenium.NoSuchElementException: Unable to locate element using css: [email protected] 
    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',它会尝试加载我的网页。不知道为什么视图解析器不工作。另外,出于某种原因,HtmlUnit找不到函数document.querySelectory。

回答

1

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

+0

这正是我最终做的。我使用'IntegrationTest'注释来激活我的上下文和FirefoxDriver。它运行良好,可以在我们的CI服务器上运行。 – ZeroDivide