基于Junit的HttpUnit测试

看了taobaoge的junit测试教程,总结下httpunit的使用
httpunit是以junit为基础的一个用来测试html页面内容的测试框架,可以测试的内容如下

1.网站或网页是否存在
2.请求参数的测试和验证
3.响应的测试和验证
4.表格的测试和验证
5.超链接的测试和验证
6.表单的测试和验证

需要将httpunit里面的jars和lib目录下的所有jar包添加到项目中,并且配合junit使用
下面给出关键代码


import static org.junit.Assert.assertEquals;

import java.io.IOException;

import org.junit.Test;
import org.xml.sax.SAXException;

import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.PostMethodWebRequest;
import com.meterware.httpunit.TableCell;
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;

/**
* 关于httpunit类使用的演示
* @author jison
* @since 2015-4-14
*/
public class HttpUnitTest {

/**
* 测试网站或网页是否存在
*/
@Test
public void webExists(){
// 代替浏览器作用的对象
WebConversation webConversation = new WebConversation();
// 请求对象
WebRequest webRequest = new GetMethodWebRequest("http://localhost:8080/HttpUnitDemo/webexists.jsp");
try {
// 响应对象,如果没有抛出异常表明网页存在
WebResponse webResponse = webConversation.getResponse(webRequest);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* 把响应页面的内容拿到
*/
@Test
public void webContent(){
// 代替浏览器作用的对象
WebConversation webConversation = new WebConversation();
// 请求对象
WebRequest webRequest = new GetMethodWebRequest("http://localhost:8080/HttpUnitDemo/webexists.jsp");
try {
// 响应对象,如果没有抛出异常表明网页存在
WebResponse webResponse = webConversation.getResponse(webRequest);
// 打印响应页面的内容
System.out.println(webResponse.getText());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* 请求参数的测试与验证
*/
@Test
public void webReq(){
// 代替浏览器作用的对象
WebConversation webConversation = new WebConversation();
// 请求对象
WebRequest webRequest = new GetMethodWebRequest("http://localhost:8080/HttpUnitDemo/req.jsp");
// 设置请求的参数
webRequest.setParameter("reqKey", "reqValue");
try {
// 响应对象
WebResponse webResponse = webConversation.getResponse(webRequest);
// 打印响应对象的html页面文本
System.out.println(webResponse.getText());

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* 页面跳转测试1
* 与预期的请求参数相同
*/
@Test
public void webRedirect() {
// 代替浏览器作用的对象
WebConversation webConversation = new WebConversation();
// 请求对象
WebRequest webRequest = new GetMethodWebRequest("http://localhost:8080/HttpUnitDemo/redirect.jsp");
// 设置请求的参数
webRequest.setParameter("username", "user");
webRequest.setParameter("password", "pass");
try {
// 响应对象
WebResponse webResponse = webConversation.getResponse(webRequest);
// 打印响应对象的html页面文本
System.out.println(webResponse.getText());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 页面跳转测试2
* 与预期的请求参数不相同
*/
@Test
public void webRedirect2() {
// 代替浏览器作用的对象
WebConversation webConversation = new WebConversation();
// 请求对象
WebRequest webRequest = new GetMethodWebRequest("http://localhost:8080/HttpUnitDemo/redirect.jsp");
// 设置请求的参数
webRequest.setParameter("username", "use");
webRequest.setParameter("password", "pass");
try {
// 响应对象
WebResponse webResponse = webConversation.getResponse(webRequest);
// 打印响应对象的html页面文本
System.out.println(webResponse.getText());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* 测试表格
*/
@Test
public void webTable(){
// 代替浏览器作用的对象
WebConversation webConversation = new WebConversation();
// 请求对象
WebRequest webRequest = new PostMethodWebRequest("http://localhost:8080/HttpUnitDemo/table.jsp");
try {
// 获取响应对象
WebResponse webResponse = webConversation.getResponse(webRequest);
// 获取表格对象
WebTable webTable = webResponse.getTables()[0];
System.out.println("获取第一个表格的第一行第二列的数据:" + webTable.getCellAsText(0, 1));
// 获取表单中第二行第二列的单元格对象
TableCell tableCell = webTable.getTableCell(1, 1);
// 断言单元格的值知否与预期的值相等
assertEquals("21", tableCell.getText());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* 测试超链接
*/
@Test
public void webLink() {
// 代替浏览器作用的对象
WebConversation webConversation = new WebConversation();
// 要测试的页面的请求
WebRequest webRequest = new PostMethodWebRequest("http://localhost:8080/HttpUnitDemo/link.jsp");
try {
WebResponse webResponse = webConversation.getResponse(webRequest);
// 获取文本为“testlink”的超链接
WebLink webLink = webResponse.getLinkWith("testlink");
// 获取超链接对应的请求
WebRequest linkRequest = webLink.getRequest();
// 获取超链接点击后对应的响应
WebResponse linkResponse = webConversation.getResponse(linkRequest);
// 期望的超链接
String expectedURL = "http://localhost:8080/HttpUnitDemo/success.jsp";
// 断言是否与期望的超链接相同
assertEquals(expectedURL, linkResponse.getURL().toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

/**
* 测试表单
*/
@Test
public void webForm(){
// 获取浏览器对象
WebConversation webConversation = new WebConversation();
// 获取请求对象
WebRequest webRequest = new PostMethodWebRequest("http://localhost:8080/HttpUnitDemo/form.jsp");
try {
// 获取响应对象
WebResponse webResponse = webConversation.getResponse(webRequest);
// 获取第一个表单对象
WebForm webForm = webResponse.getForms()[0];
// 断言单行文本域的值
assertEquals("textField", webForm.getParameterValue("textField"));
// 选中的复选按钮的值为on
assertEquals("on", webForm.getParameterValue("checkboxTest"));
// 只有选中的单选按钮取得值才也设置的值一致
assertEquals("radioTest1", webForm.getParameterValue("radioTest1"));
// // 未选中的单选按钮的值与设置的值不一致
// assertEquals("radioTest2", webForm.getParameterValue("radioTest2"));
// 断言下拉列表框
assertEquals("option2", webForm.getParameterValue("selectTest"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值