这是我使用 HTMLUnit 的方法。不需要硒。
它作为常规的 JUnit 测试运行(可以很容易地由 CI 服务器自动运行),如果测试失败,它会打印出 DOH 日志。
public class JavascriptTest {
private static final int MAX_RUNNING_TIME = 10 * 1000;
//The test runner
public static final String PATHNAME = "src/main/webapp/library/mystuff/dojo/util/tests/runTests.html";
//Runs all of the Dojo Objective Harness (D.O.H.) javascript tests.
//The tests are currently grouped into test modules, and the parent module is "util.tests.module" (in module.js)
//As you can see in the URL pathname, we pass that module name to the testRunner and it runs all the javascript tests.
@Test
public void runAllJavascriptTests() throws Exception {
final WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_8);
final HtmlPage page = webClient.getPage("file://" + new File(PATHNAME).getAbsolutePath());
waitForTestsToRun(webClient, page);
String log = page.getElementById("logBody").asText();
assertTrue(log, page.asText().contains("WOOHOO!!")); //D.O.H. will display WOOHOO!! if all tests are successful.
}
private void waitForTestsToRun(WebClient webClient, HtmlPage page) {
webClient.waitForBackgroundJavaScript(500);
int runningTime = 0;
while(testsAreRunning(page) && runningTime < MAX_RUNNING_TIME){
webClient.waitForBackgroundJavaScript(500);
runningTime += 500;
}
}
private boolean testsAreRunning(HtmlPage page) {
//Check if the "Tests Running" div is visible.
return "".equals(page.getElementById("playingMsg").getAttribute("style"));
}
}
下面是runTests.html 的内容。它基本上只是重定向到 DOJO 测试运行器,参数特定于我们要测试的目录中的测试。
这只是一种构造事物的好方法,您也可以在 JUnit 测试的 PATHNAME 字段中指定此 URL。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Dojox Unit Test Runner</title>
<!--The "testModule" param tells the runner which test module to run-->
<!--The "paths" param adds our dojo module paths, otherwise it would just look in the default dojo modules for code to test.-->
<meta http-equiv="REFRESH" content="0;url=../../../../dojo-release-1.7.2-src/util/doh/runner.html?testModule=util.tests.module&paths=util,../../mystuff/dojo/util;mystuff,../../mystuff/dojo"></HEAD>
<BODY>
Redirecting to D.O.H runner.
</BODY>
</HTML>