在用htmlunit进行WEB测试的过程中,我遇上了个很奇怪的问题,当正常访问一个网页时,就解析的非常完整(
网页代码中含有<script type="text/javascript" src="http://xxxx.xxx.com/xxx.js"/>),但是当我使用
MockWebConnection模拟返回这些网页时,就会爆出
XML" is not defined.
的错误,实在不知道什么原因,望高人指点,先谢谢了。
(还有,我的分不多了,只有30,没办法呃,哈哈)
网页代码中含有<script type="text/javascript" src="http://xxxx.xxx.com/xxx.js"/>),但是当我使用
MockWebConnection模拟返回这些网页时,就会爆出
XML" is not defined.
的错误,实在不知道什么原因,望高人指点,先谢谢了。
(还有,我的分不多了,只有30,没办法呃,哈哈)
<html><head><title></title>
<script type='text/javascript' src='http://localhost/test.js'></script>
</head><body></body></html>
当我直接用webClient.getPage("http://localhost/test.html");调用,就能够正常的解析,没有问题。但是当我用MockWebConnection来模拟,代码如下:final String html = "<html><head>\n"
+ "<script src='http://localhost/test.js'></script>\n"
+ "</head><body onload='test()'>\n" + "</body></html>";
MockWebConnection connection = new MockWebConnection();
connection.setDefaultResponse(html);
try {
final WebClient webClient = new WebClient();
webClient.setWebConnection(connection);
webClient.getPage("http://localhost/test.html");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FailingHttpStatusCodeException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}就会爆出
cmaError: lineNumber=[1] column=[0] lineSource=[<no source>]
name=[ReferenceError] sourceName=[http://localhost/test.js]
message=[ReferenceError: "XML" is not defined.
(http://localhost/test.js#1)]
com.gargoylesoftware.htmlunit.ScriptException: ReferenceError: "XML" is
not defined. (http://localhost/test.js#1)
的异常。但是当我给那段js加上response,代码如下:
final String html2 = "function test() {\n"
+ " alert('hello');\n"
+ "}";
connection.setResponse(new URL("http://localhost/test.js"),
html2);
就又正常了。
所以我的想法就是直接只模拟一个网页入口而已,而后的工作由htmlunit自己解决,就像直接访问浏览器那样。
而js返回的确是 application/javascript类型的页面,两个不同类型的文档必须分开解析,所以只能手动来设置了,郁闷呃