我正在使用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。
這正是我最終做的。我使用'IntegrationTest'註釋來激活我的上下文和FirefoxDriver。它運行良好,可以在我們的CI服務器上運行。 – ZeroDivide