HttpUnit是一个能模拟WEB浏览器功能的工具,能和junit一同来测试页面。
这个框架简单好用,能测试页面内容,模拟、修改、提交表单,测试超链接等。
下面是一个测试类,举了一些常用的测试例子。这些例子也是httpunit官网上的例子。
package com.yuchujin;
import java.io.IOException;
import junit.framework.Assert;
import org.junit.Test;
import org.xml.sax.SAXException;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.PostMethodWebRequest;
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;
public class WebTest {
/**
* 测试URL是否有返回response。可用来测试URL地址存在和服务是否存在等。
*/
@Test
public void testWebExists(){
//核心类,用来模拟一个浏览器的操作,相当于拥有了一个浏览器
WebConversation web = new WebConversation();
try {
//使用URL字符串地址作为参数,测试服务是否开启。
//geResponse获得返回的response
web.getResponse("http://localhost:8080/HttpUnitProject");
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
/**
* 使用WebRequest作为参数测试服务是否开启。
*/
@Test
public void testWebExists2(){
WebConversation web = new WebConversation();
//使用GET方法创建request对象
WebRequest request = new GetMethodWebRequest("http://localhost:8080/HttpUnitProject");
try {
web.getResponse(request);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 测试response中的内容
*/
@Test
public void testWebContent(){
WebConversation web = new WebConversation();
//使用GET方法创建request对象
WebRequest request = new GetMethodWebRequest("http://localhost:8080/HttpUnitProject/index.jsp");
try {
WebResponse response = web.getResponse(request);
//显示response中的html内容
System.out.println(response.getText());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 测试带参数的request
*/
@Test
public void testWebContentWithParamenter(){
WebConversation web = new WebConversation();
WebRequest request = new GetMethodWebRequest("http://localhost:8080/HttpUnitProject/index.jsp");
//使用reuqest的setParameter方法可以为生成的request添加参数
request.setParameter("username", "yuchujin");
try {
WebResponse response = web.getResponse(request);
System.out.println(response.getText());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 测试页面的跳转
*/
@Test
public void testWebRedirectSuccess(){
WebConversation web = new WebConversation();
//生成POST的request
WebRequest request = new PostMethodWebRequest("http://localhost:8080/HttpUnitProject/validate.jsp");
//使用reuqest的setParameter方法可以为生成的request添加参数
request.setParameter("username", "ycj");
request.setParameter("password", "123");
try {
WebResponse response = web.getResponse(request);
String expectedURL = "http://localhost:8080/HttpUnitProject/success.jsp";
Assert.assertEquals("validate fail",expectedURL, response.getURL().toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 测试页面跳转失败
*/
@Test
public void testWebRedirectFail(){
WebConversation web = new WebConversation();
WebRequest request = new PostMethodWebRequest("http://localhost:8080/HttpUnitProject/validate.jsp");
//使用reuqest的setParameter方法可以为生成的request添加参数
request.setParameter("username", "ycj");
request.setParameter("password", "321");
try {
WebResponse response = web.getResponse(request);
String expectedURL = "http://localhost:8080/HttpUnitProject/fail.jsp";
Assert.assertEquals("validate fail",expectedURL, response.getURL().toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 测试获得页面中的table节点
*/
@Test
public void testWebTables(){
WebConversation web = new WebConversation();
WebRequest request = new GetMethodWebRequest("http://localhost:8080/HttpUnitProject/table.jsp");
WebResponse response = null;
try {
response = web.getResponse(request);
//WebTable代表返回的html中的table标签封装的对象
//response.getTables返回html中所有table dom对象数组
WebTable tables = response.getTables()[0];
Assert.assertEquals(2, tables.getColumnCount());
Assert.assertEquals(3, tables.getRowCount());
//获得表格中某个单元格中的文本
Assert.assertEquals("male", tables.getTableCell(2, 1).getText());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 测试超链接内容
*/
@Test
public void testWebLink(){
WebConversation web = new WebConversation();
WebRequest request = new GetMethodWebRequest("http://localhost:8080/HttpUnitProject/linkFrom.jsp");
try {
WebResponse response = web.getResponse(request);
//模拟html中的超链接,在页面中通过超连接的文本获得超连接的URL
WebLink link = response.getLinkWith("click me to linkTo");
//将超链接生成request
WebRequest linkReq = link.getRequest();
//访问刚超链接页面
response = web.getResponse(linkReq);
Assert.assertEquals("http://localhost:8080/HttpUnitProject/linkTo.jsp",response.getURL().toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 测试form中的内容
*/
@Test
public void testWebForm(){
WebConversation web = new WebConversation();
WebRequest request = new GetMethodWebRequest("http://localhost:8080/HttpUnitProject/form.jsp");
WebResponse response = null;
try {
response = web.getResponse(request);
//获得Html中的form表单,HttpUnit将他封装成WebForm对象
WebForm form = response.getForms()[0];
//WebForm对象提供getParameterValue的方法将根据表单中的name获得对应的value,而不用管该元素的类型。
Assert.assertEquals("yuchujin", form.getParameterValue("name"));
Assert.assertEquals("mela", form.getParameterValue("sex"));
Assert.assertEquals("high", form.getParameterValue("school"));
Assert.assertEquals("on", form.getParameterValue("cj"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 测试WebForm的处理表单和提交能力
*/
@Test
public void testFormSubmit(){
WebConversation web = new WebConversation();
WebRequest request = new PostMethodWebRequest("http://localhost:8080/HttpUnitProject/form.jsp");
WebResponse response = null;
try {
response = web.getResponse(request);
//获得Html中的form表单,HttpUnit将他封装成WebForm对象
WebForm form = response.getForms()[0];
//WebForm对象提供getParameterValue的方法将根据表单中的name获得对应的value,而不用管该元素的类型。
Assert.assertEquals("yuchujin", form.getParameterValue("name"));
Assert.assertEquals("mela", form.getParameterValue("sex"));
Assert.assertEquals("high", form.getParameterValue("school"));
Assert.assertEquals("on", form.getParameterValue("cj"));
//对表单进行处理操作(根据name)
form.setParameter("name", "zhoujianzhen");
// TODO 有的元素不能够remove,比如说radio。限制只发现checkbox能删,能研究一下再修改文章。
form.removeParameter("school");
//提交表单 获得新的response
response = form.submit();
Assert.assertEquals("http://localhost:8080/HttpUnitProject/handleForm.jsp", response.getURL().toString());
System.out.println(response.getText());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
分享到:
相关推荐
赠送jar包:junit-platform-launcher-1.8.0-M1.jar; 赠送原API文档:junit-platform-launcher-1.8.0-M1-javadoc.jar; 赠送源代码:junit-platform-launcher-1.8.0-M1-sources.jar; 赠送Maven依赖信息文件:junit-...
赠送jar包:junit-jupiter-api-5.7.2.jar; 赠送原API文档:junit-jupiter-api-5.7.2-javadoc.jar; 赠送源代码:junit-jupiter-api-5.7.2-sources.jar; 赠送Maven依赖信息文件:junit-jupiter-api-5.7.2.pom; ...
赠送jar包:junit-jupiter-api-5.8.0-M1.jar; 赠送原API文档:junit-jupiter-api-5.8.0-M1-javadoc.jar; 赠送源代码:junit-jupiter-api-5.8.0-M1-sources.jar; 赠送Maven依赖信息文件:junit-jupiter-api-5.8.0-...
赠送jar包:junit-jupiter-api-5.8.2.jar; 赠送原API文档:junit-jupiter-api-5.8.2-javadoc.jar; 赠送源代码:junit-jupiter-api-5.8.2-sources.jar; 赠送Maven依赖信息文件:junit-jupiter-api-5.8.2.pom; ...
赠送jar包:junit-jupiter-engine-5.8.2.jar; 赠送原API文档:junit-jupiter-engine-5.8.2-javadoc.jar; 赠送源代码:junit-jupiter-engine-5.8.2-sources.jar; 赠送Maven依赖信息文件:junit-jupiter-engine-...
赠送jar包:junit-jupiter-params-5.7.2.jar; 赠送原API文档:junit-jupiter-params-5.7.2-javadoc.jar; 赠送源代码:junit-jupiter-params-5.7.2-sources.jar; 赠送Maven依赖信息文件:junit-jupiter-params-...
junit-platform-launcher-1.7.0.jarjunit-platform-launcher-1.7.0.jar
赠送jar包:junit-jupiter-api-5.4.2.jar; 赠送原API文档:junit-jupiter-api-5.4.2-javadoc.jar; 赠送源代码:junit-jupiter-api-5.4.2-sources.jar; 赠送Maven依赖信息文件:junit-jupiter-api-5.4.2.pom; ...
junit-platform-launcher-1.6.2.jarjunit-platform-launcher-1.6.2.jarjunit-platform-launcher-1.6.2.jar
junit-platform-commons-1.7.0.jar
JUnit5相关的 JUnit Jupiter.jar依赖包,主要包含: junit-jupiter-5.5.1.jar,junit-jupiter-api-5.5.1.jar,junit-jupiter-engine-5.5.1.jar,junit-jupiter-params-5.5.1.jar,junit-jupiter-api-5.4.2.jar,...
赠送jar包:junit-platform-engine-1.8.2.jar; 赠送原API文档:junit-platform-engine-1.8.2-javadoc.jar; 赠送源代码:junit-platform-engine-1.8.2-sources.jar; 赠送Maven依赖信息文件:junit-platform-engine...
赠送jar包:junit-platform-launcher-1.8.0-M1.jar; 赠送原API文档:junit-platform-launcher-1.8.0-M1-javadoc.jar; 赠送源代码:junit-platform-launcher-1.8.0-M1-sources.jar; 赠送Maven依赖信息文件:junit-...
赠送jar包:junit-platform-commons-1.8.2.jar; 赠送原API文档:junit-platform-commons-1.8.2-javadoc.jar; 赠送源代码:junit-platform-commons-1.8.2-sources.jar; 赠送Maven依赖信息文件:junit-platform-...
赠送jar包:junit-jupiter-params-5.8.0-M1.jar; 赠送原API文档:junit-jupiter-params-5.8.0-M1-javadoc.jar; 赠送源代码:junit-jupiter-params-5.8.0-M1-sources.jar; 赠送Maven依赖信息文件:junit-jupiter-...
赠送jar包:junit-jupiter-engine-5.4.2.jar; 赠送原API文档:junit-jupiter-engine-5.4.2-javadoc.jar; 赠送源代码:junit-jupiter-engine-5.4.2-sources.jar; 赠送Maven依赖信息文件:junit-jupiter-engine-...
赠送jar包:junit-jupiter-params-5.5.2.jar; 赠送原API文档:junit-jupiter-params-5.5.2-javadoc.jar; 赠送源代码:junit-jupiter-params-5.5.2-sources.jar; 赠送Maven依赖信息文件:junit-jupiter-params-...
赠送jar包:junit-vintage-engine-5.5.2.jar; 赠送原API文档:junit-vintage-engine-5.5.2-javadoc.jar; 赠送源代码:junit-vintage-engine-5.5.2-sources.jar; 赠送Maven依赖信息文件:junit-vintage-engine-...
赠送jar包:junit-jupiter-api-5.5.2.jar; 赠送原API文档:junit-jupiter-api-5.5.2-javadoc.jar; 赠送源代码:junit-jupiter-api-5.5.2-sources.jar; 赠送Maven依赖信息文件:junit-jupiter-api-5.5.2.pom; ...
赠送jar包:junit-platform-launcher-1.6.1.jar; 赠送原API文档:junit-platform-launcher-1.6.1-javadoc.jar; 赠送源代码:junit-platform-launcher-1.6.1-sources.jar; 赠送Maven依赖信息文件:junit-platform-...