HttpUnit 使用
HttpUnit是一个集成测试工具,主要关注Web应用的测试,提供的帮助类让测试者可以通过Java类和服务器进行交互,并且将服务器端的响应当作文本或者DOM对象进行处理。HttpUnit还提供了一个模拟Servlet容器,让你可以不需要发布Servlet,就可以对Servlet的内部代码进行测试。本文中作者将详细的介绍如何使用HttpUnit提供的类完成集成测试。
HttpUnit是SourceForge下面的一个开源项目,它是基于JUnit的一个测试框架,主要关注于测试Web应用,解决使用JUnit框架无法对远程Web内容进行测试的弊端。
HttpUnit通过模拟浏览器的行为,处理页面框架(frames),cookies,页面跳转(redirects)等。通过HttpUnit提供的功能,你可以和服务器端进行信息交互,将返回的网页内容作为普通文本、XML Dom对象或者是作为链接、页面框架、图像、表单、表格等的集合进行处理,然后使用JUnit框架进行测试,还可以导向一个新的页面,然后进行新页面的处理,这个功能使你可以处理一组在一个操作链中的页面。
下载网址
http://httpunit.sourceforge.net/
总结示例:
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.PostMethodWebRequest;
import com.meterware.httpunit.TableCell;
import com.meterware.httpunit.TableRow;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebTable;
import org.xml.sax.SAXException;
import java.io.IOException;
public class JsoupLoadWeb {
public static void testHtmlContentForm(String url) throws IOException, SAXException {
System.out.println("获取页面中表单的内容:");
// 建立一个WebConversation实例
WebConversation wc = new WebConversation();
// 获取响应对象1
WebResponse resp = wc.getResponse(url);
// 获取响应对象2
WebRequest getReq = new GetMethodWebRequest(url);
getReq.setParameter("user_name", "test");
getReq.setParameter("password", "111111");
WebResponse resp2 = wc.getResponse(getReq);
//获取响应对象3
WebRequest postReq = new PostMethodWebRequest(url);
postReq.setParameter("user_name", "test");
postReq.setParameter("password", "111111");
WebResponse resp3 = wc.getResponse(postReq);
//获得当前的响应对象
WebResponse myresp = wc.getCurrentPage();
// 用getText方法获取相应的全部内容
String webStr = resp.getText();
// 获得对应的表单对象
WebForm[] froms = resp.getForms();
WebForm form = resp.getForms()[0];
WebForm form2 = resp.getFormWithID("form2");
WebForm form3 = resp.getFormWithName("窗口3");
// 获得表单中所有控件的名字
String[] pNames = form.getParameterNames();
//向指定的URL发出请求
// 设置表单中控件的值
//WebRequest 模仿客户请求,通过它可以向服务器发送信息
WebRequest loginRequest = form.getRequest();
loginRequest.setParameter("userName", " XX");
loginRequest.setParameter("password", " XX");
//获得对应的表格对象
WebTable webTable = resp.getTables()[0];
WebTable webTable2 = resp.getTableWithID("mx");
WebTable webTable3 = resp.getTableStartingWith("");
WebTable webTable4 = resp.getTableStartingWithPrefix("");
WebTable webTable5 = resp.getTableWithSummary("");
//表格对象用法
int colnum = webTable.getColumnCount();
int rownum = webTable.getRowCount();
String[][] datas = webTable.asText(); //将表格对象的内容
//单元格对象用法
TableRow[] rowdatas = webTable.getRows();
TableCell celldata = webTable.getTableCell(1, 0);//2行1列
String cellVal = webTable.getCellAsText(1, 0);//2行1列
celldata.setAttribute("text", "123");//对单元格赋值
celldata.getLinks();
// 页面链接对象
WebLink[] links = resp.getLinks();//获得页面中所有的链接对象
WebLink link1 = resp.getLinkWithID("save");
WebLink link2 = resp.getLinkWithName("保存");
WebLink link3 = resp.getLinkWith("保存");
WebLink link4 = resp.getLinkWithImageText("保存");
link1.click(); // 模拟用户单击事件
}
}
欢迎好评!