bootstraps未在功能测试模式下加载

时间:2014-11-30 19:55:16

标签: twitter-bootstrap playframework assets functional-testing

我遇到同样的问题(assets are not loaded in functional test mode),但仍无法正常工作......

我使用activator test

开始测试

这是我对从activator play-java(-v 2.3.x)生成的默认java项目的自定义:

build.stb

name := """play-test-app"""

version := "1.1-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(PlayJava)

scalaVersion := "2.11.1"

libraryDependencies ++= Seq(
  javaJdbc,
  javaEbean,
  javaWs,
  cache,
  "org.jumpmind.symmetric.jdbc" % "postgresql" % "9.2-1002-jdbc4",
  "org.webjars" % "bootstrap" % "3.3.1",
  "org.webjars" % "jquery" % "2.1.1"
)

unmanagedResourceDirectories in Test <+=  baseDirectory ( _ /"target/web/public/test" ) 

路由

GET         /assets/*file             controllers.Assets.at(path="/public", file)

main.scala.html

...

<link rel='stylesheet' href='@routes.Assets.at("lib/bootstrap/css/bootstrap.min.css")'>
<script data-main="@routes.Assets.at("javascripts/main.js")" type="text/javascript" src="@routes.Assets.at("lib/bootstrap/js/bootstrap.min.js")"></script>
<script data-main="@routes.Assets.at("javascripts/main.js")" type="text/javascript" src="@routes.Assets.at("lib/jquery/jquery.min.js")"></script>
</head>
   <body>
       Already added
...

测试

/**
 * add your integration test here
 * in this example we just check if the welcome page is being shown
 */
@Test
public void test() {
    running(testServer(3333, fakeApplication(inMemoryDatabase())), HTMLUNIT, new Callback<TestBrowser>() {
        public void invoke(TestBrowser browser) {
            browser.goTo("http://localhost:3333");
            assertThat(browser.pageSource()).contains("Already added");
        }
    });
}

来自控制台的结果:

[error] Driver info: driver.version: HtmlUnitDriver, took 3.257 sec
[error]     at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:484)
[error]     at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:463)
[error]     at org.fluentlenium.core.Fluent.goTo(Fluent.java:336)
[error]     at IntegrationTest$1.invoke(IntegrationTest.java:22)
[error]     at IntegrationTest$1.invoke(IntegrationTest.java:20)
[error]     at play.test.Helpers.running(Helpers.java:555)
[error]     at play.test.Helpers.running(Helpers.java:541)
[error]     at IntegrationTest.test(IntegrationTest.java:20)
[error]     ...
[error] Caused by: com.gargoylesoftware.htmlunit.ScriptException: Error: Bootstrap's JavaScript requires jQuery (http://localhost:3333/assets/lib/bootstrap/js/bootstrap.min.js#6)
[error]     at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:684)
[error]     at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:602)
[error]     at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:507)
[error]     at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:570)
[error]     at com.gargoylesoftware.htmlunit.html.HtmlPage.loadExternalJavaScriptFile(HtmlPage.java:1062)
[error]     at com.gargoylesoftware.htmlunit.html.HtmlScript.executeScriptIfNeeded(HtmlScript.java:409)
[error]     at com.gargoylesoftware.htmlunit.html.HtmlScript$3.execute(HtmlScript.java:266)
[error]     at com.gargoylesoftware.htmlunit.html.HtmlScript.onAllChildrenAddedToPage(HtmlScript.java:286)
[error]     at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:683)
[error]     at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
[error]     at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:642)
[error]     at org.cyberneko.html.HTMLTagBalancer.callEndElement(HTMLTagBalancer.java:1170)
[error]     at org.cyberneko.html.HTMLTagBalancer.endElement(HTMLTagBalancer.java:1072)
[error]     at org.cyberneko.html.filters.DefaultFilter.endElement(DefaultFilter.java:206)
[error]     at org.cyberneko.html.filters.NamespaceBinder.endElement(NamespaceBinder.java:330)
[error]     at org.cyberneko.html.HTMLScanner$ContentScanner.scanEndElement(HTMLScanner.java:3116)
[error]     at org.cyberneko.html.HTMLScanner$ContentScanner.scan(HTMLScanner.java:2083)
[error]     at org.cyberneko.html.HTMLScanner.scanDocument(HTMLScanner.java:918)
[error]     at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:499)
[error]     at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:452)
[error]     at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
[error]     at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.parse(HTMLParser.java:899)
[error]     at com.gargoylesoftware.htmlunit.html.HTMLParser.parse(HTMLParser.java:242)
[error]     at com.gargoylesoftware.htmlunit.html.HTMLParser.parseHtml(HTMLParser.java:188)
[error]     at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(DefaultPageCreator.java:268)
[error]     at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:156)
[error]     at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:437)
[error]     at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:311)
[error]     at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:376)
[error]     at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:474)
[error]     ... 41 more
[error] Caused by: net.sourceforge.htmlunit.corejs.javascript.JavaScriptException: Error: Bootstrap's JavaScript requires jQuery (http://localhost:3333/assets/lib/bootstrap/js/bootstrap.min.js#6)
[error]     at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1006)
[error]     at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:798)
[error]     at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:105)
[error]     at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:405)
[error]     at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:309)
[error]     at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3031)
[error]     at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.exec(InterpretedFunction.java:115)
[error]     at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$3.doRun(JavaScriptEngine.java:561)
[error]     at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:669)
[error]     ... 70 more
[error] Failed: Total 3, Failed 1, Errors 0, Passed 2
[error] Failed tests:
[error]         IntegrationTest
[error] (test:test) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 6 s, completed 2014-11-30 20:43:15

1 个答案:

答案 0 :(得分:0)

让我改变导入脚本的顺序; jquery先行,后面是bootstrap:

<script data-main="@routes.Assets.at("javascripts/main.js")" type="text/javascript" src="@routes.Assets.at("lib/jquery/jquery.min.js")"></script>
<script data-main="@routes.Assets.at("javascripts/main.js")" type="text/javascript" src="@routes.Assets.at("lib/bootstrap/js/bootstrap.min.js")"></script>