问题背景:测试工具要实现运行被测试设备里面的web服务中的某个url请求(HTML),运行里面js的相关事件,抓取返回值,这里关键是怎么通过工具实现请求被测试设备的url(web服务),这里我通过上网学习,写了几种思路的方法.

方法一:  直接使用Java方法调用系统浏览器,然后请求URL

下面是我实现这种思路的部分代码

public static void runBroswer(String url,int flag) {
try {
Desktop desktop = Desktop.getDesktop();
if (desktop.isDesktopSupported() && desktop.isSupported(Desktop.Action.BROWSE)) {
URI uri = new URI(url);
desktop.browse(uri); //使用系统默认的浏览器执行这个url
Thread.sleep(2000);
//Runtime.getRuntime().exec("taskkill /F /IM Iexplore.exe");
Runtime.getRuntime().exec("taskkill /IM firefox.exe"); //因为我系统默认的是火狐,然后关闭火狐浏览器
}
}

这种方法,实现的效果在实际中,就是要调用浏览器,桌面会弹出浏览器界面,然后请求url;

方法二: httpclient httpurlconnection

后面我在想,能不能直接使用Java模拟浏览器直接实现url请求,开始是使用这两个httpclient 和 HttpUrlConnection来实现这个效果,下面是部分代码:

String strUrl = "http://localhost:8088/testWeb"; //这是我临时建的web项目,去访问它的index.jsp界面
URL url = new URL(strUrl);
HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
InputStreamReader input = new InputStreamReader(httpcon.getInputStream(), "utf-8");
BufferedReader br = new BufferedReader(input);
StringBuffer sb = new StringBuffer();
String line = "";
while((line = br.readLine()) != null){ //读取每行数据
sb.append(line);
sb.append("\r\n");
}
String sbString = sb.toString();
System.out.println("result... "+sbString);

通过运行这部分代码发现.它只是完全返回HTML代码,并没有执行HTML里面的js onload()事件,也就是没有运行,js;当然我这部分代码是使用HttpUrlConnection类,也可以使用Httpclient类,第三方工具类;

方法三: 使用HtmlUtil 工具类

因为第二种方法我感觉他是没有运行js里面的事件,只是获取HTML代码,后面继续网上学习,发现了HtmlUtil这个工具类.

HtmlUnit是一款基于Java的没有图形界面的浏览器程序。它模仿HTML document并且提供API让开发人员像是在一个正常的浏览器上操作一样,获取网页内容,填充表单,点击超链接等等。

它非常好的支持JavaScript并且仍在不断改进,同时能够解析非常复杂的AJAX库,通过不同的配置来模拟Chrome、Firefox和IE浏览器。

下面是我实现的部分代码:

public static void main(String[] args) {
WebClient wc = new WebClient(BrowserVersion.getDefault());
wc.setJavaScriptEnabled(true); //启用JS解释器,默认为true
wc.setJavaScriptTimeout(100000);//设置JS执行的超时时间
wc.setCssEnabled(false); //禁用css支持
wc.setThrowExceptionOnScriptError(false); //js运行错误时,是否抛出异常
wc.setTimeout(10000); //设置连接超时时间 ,这里是10S。如果为0,则无限期等待
//wc.setWebConnection(
// new WebConnectionWrapper(wc) {
// public WebResponse getResponse(WebRequest request) throws IOException {
// WebResponse response = super.getResponse(request);
// if (request.getUrl().toExternalForm().contains("test.js")) {
// String content = response.getContentAsString("GBK");
// WebResponseData data = new WebResponseData(content.getBytes("UTF-8"),
// response.getStatusCode(), response.getStatusMessage(), response.getResponseHeaders());
// response = new WebResponse(data, request, response.getLoadTime());
// }
// return response;
// }
// }
//);
try {
//HtmlPage page = wc.getPage("http://192.168.0.1/Del_Bridge_Wan.html");
HtmlPage page = wc.getPage("http://localhost:8088/testWeb");
FileWriter fileWriter = new FileWriter("D:\\text.html");
System.out.println("over...");
String str = "";
//获取页面的XML代码
str = page.asXml();
fileWriter.write( str );
//关闭webclient
//wc.close();
wc.closeAllWindows();
fileWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}

通过运行这部分代码后发现,(注释部分解决编码问题,可以通过重写WebConnectionWrapper类的getResponse方法来修改返回值),运行了HTML里面的onload事件,达到了不调用浏览器也能访问url,并能运行js事件,而不是直接返回该HTML的源代码.最后打开生成的test.html就可以发现是我之前创建的testWeb服务index.jsp里面写的返回值;