要求
我正在尝试在基于功能测试阶段的自定义测试阶段运行我的 JavaScript 测试。基本上它需要:
- 启动嵌入式Tomcat
- 打开控制器
- 检查执行测试的结果
我做了什么
首先,我根据这篇文章创建了我的自定义测试阶段。所以我的 _Events.groovy 看起来像
includeTargets << new File("${basedir}/scripts/_RunJavaScriptUiTests.groovy")
eventConfigureTomcat = { tomcat ->
tomcat.connector.setAttribute("compression", "on")
tomcat.connector.setAttribute("compressableMimeType", "text/html,text/xml,text/plain,application/javascript")
tomcat.connector.port = serverPort
}
eventAllTestsStart = {
phasesToRun << "uijs"
}
uijsTests = ["uijs"]
uijsTestPhasePreparation = {
functionalTestPhasePreparation()
}
uijsTestPhaseCleanUp = {
functionalTestPhaseCleanUp()
}
eventTestPhaseEnd = { phase ->
if( phase == "uijs" ) {
runJavaScriptUiTests()
}
}
接下来,我决定使用PhantomJS打开我的页面并分析执行的测试。所以我在RunJavaScriptUiTests.groovy脚本中使用了它
target(runJavaScriptUiTests:"Running Siesta tests") {
event("StatusUpdate", ["Siesta test phase..."])
//this is the script that evaluates the result of the tests
File script = new File("web-app/js/siesta/siesta-phantomjs-runner.js")
String home = System.getenv("PHANTOMJS_HOME")
if(!home) {
throw new RuntimeException("PHANTOMJS_HOME must be set.")
}
String executable = "${home}bin${File.separator}phantomjs"
String port = System.getProperty("server.port","8080")
String url = "http://localhost:$port/insoft-ext-ui/siesta" //url of my tests
println "Running Phantomjs ${executable} ${script.absolutePath} "
try {
ant.exec(executable: executable, outputproperty: "cmdOut", failonerror: 'true', errorproperty: "cmdErr") {
arg(value: script.absolutePath)
arg(value: url)
}
}catch(e) {
println "ERROR: $e"
throw e
}
try {
String output = "${ant.project.properties.cmdOut}"
println output
}catch(e) {
event("StatusError",["Exception $e"])
}
}
我可以看到functionalTestPhasePreparation运行,因为这会正确启动我的应用程序。我还可以看到 phantomjs 命令在打印时是正确的:
Running: /desenv/phantomjs-1.9.2/bin/phantomjs /desenv/java/projetos/insoft-ext-ui/web-app/js/siesta/siesta-phantomjs-runner.js http://localhost:8080/insoft-ext-ui/siesta
但这给了我groovy.lang.MissingPropertyException
groovy.lang.MissingPropertyException: No such property: org.codehaus.grails.INCLUDED_JS_LIBRARIES for class: org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:273)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:53)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.grails.web.filters.JavascriptLibraryFilters$_closure1_closure2_closure3.doCall(JavascriptLibraryFilters.groovy:27)
at org.codehaus.groovy.grails.web.filters.JavascriptLibraryFilters$_closure1_closure2_closure3.doCall(JavascriptLibraryFilters.groovy)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
有关完整的 Stacktrace,请参见此处。
有趣的是,如果我这样做
grails test run-app
phantomjs /desenv/java/projetos/insoft-ext-ui/web-app/js/siesta/siesta-phantomjs-runner.js http://localhost:8080/insoft-ext-ui/siesta
该脚本有效,我没有任何异常。
问题
为什么会抛出 MissingPropertyException?我查看了JavascriptLibraryFilters并没有找到原因。
编辑
关于 Tomcat
我正在使用 Grails 附带的嵌入式 Tomcat,但在 _Events.groovy 中启用压缩:
eventConfigureTomcat = { tomcat ->
tomcat.connector.setAttribute("compression", "on")
tomcat.connector.setAttribute("compressableMimeType", "text/html,text/xml,text/plain,application/javascript")
tomcat.connector.port = serverPort
}