测试1:测试网页是否存在:
要测试一個网页是否存在,只要简单的通过WebConversation的getResponse()方法即可,例如:
WebConversation webConversation = new WebConversation();
webConversation.getResponse("http://localhost:8080/httpUnit/");
如果找不到网页,則會引发HttpNotFoundException,由于不是断言错误,所以这会在JUnit中产生一個Error。
<!--EndFragment-->
测试2:Get、Post:
public class AuthActionTest extends BaseTest {
@Autowired
private UserSpaceRedisDAO userRedisDAO;
@Test
public void testLogin() throws IOException {
String account="sgswebtest";
String pwd="1111";
WebConversation webConversation = new WebConversation();
WebRequest request =new GetMethodWebRequest("http://localhost:8080/auth!doLogin.action");
request.setParameter("loginAccount",account);
request.setParameter("loginPwd",pwd);
WebResponse response =webConversation.getResource(request);
System.out.println("-----------------------"+response.getResponseMessage());
Assert.notNull(response);
Assert.isTrue(response.getResponseCode()==200);
//登录成功返回userId
String responseText=response.getText();
Assert.notNull(responseText);
Long userId=Long.valueOf(responseText.replaceAll("\"", ""));
//根据账号查询userID,然后和返回的userId做比较
super.jedis.select(0);
Long newUserId=this.userRedisDAO.getUserIdByAccontName(account);
Assert.notNull(newUserId);
Assert.isTrue(newUserId.longValue()==userId.longValue(),"登录后返回的userId和账号ID不对应");
}
}
<!--EndFragment-->
测试3:取得表格信息:
您可以从WebResponse中取得相关的HTML信息,假设网页中有如下这样的一个表格:
书籍名称
|
设计模式(Design Pattern)
|
软件版本
|
无
|
书籍版次
|
第二版
|
修改时间
|
2004/12/26
|
下面的程序演示如何取得表格相关信息进行测试:
WebConversation webConversation = new WebConversation();
WebResponse response = webConversation.getResponse("http://localhost:8080/httpUnit/tableTest.jsp");
WebTable webTable = response.getTables()[0];
assertEquals(2, webTable.getColumnCount());
TableCell cell = webTable.getTableCell(2, 0);
assertEquals("书籍版次", cell.getText());
测试4:跟随超链接:
网页中有很多的超链接,我们可以使用HttpUnit来模拟超链接的点击,例如网页中如果有个超链接如下:
<a href="httpUnitABC.jsp">HttpUnit ABC</a>
则可以使用下面的程序来吵到链接,然后模拟一个click动作来跟随这个超链接:
WebConversation webConversation = new WebConversation();
WebResponse response = webConversation.getResponse("http://localhost:8080/httpUnit/");
WebLink link = response.getLinkWith("HttpUnit ABC");
WebRequest clickRequest = link.getRequest();
WebResponse linkPage =webConversation.getResponse(clickRequest);
测试5:测试Cookie:
如果被测试的网页需要Cookie信息,您可以使用WebConversation的addCookie()方法发送Cookie信息
给网页,例如:
WebConversation webConversation = new WebConversation();
webConversation.addCookie("user", "taobaoge");
WebResponse response = webConversation.getResponse("http://localhost:8080/httpUnit/");
如果网页中包含了Cookie,您可以使用getCookieValue()方法取得网页中包含的Cookie信息,若网页包括下面的Scriptlet:
<%
Cookie cookie = new Cookie("customerId", "12345");
response.addCookie(cookie);
%>
可使用下面的方式來测试传回的Cookie信息:
assertEquals("taobaoge",webConversation.getCookieValue("user"));
测试6:Authorization:
如果您的网页中有预设的HTTP基本验证,则可以使用WebConversation的setAuthorization ()方法來设定验证信息,例如:
webConversation.setAuthorization("justin", "123456");
测试7:设定代理:
有的時候,您测试的目的网页可能必须通过代理服务器才能连上,你可以通过设定系统属性来设定HttpUnit使用代理,例如:
System.getProperties().put("proxySet", "true");
System.getProperties().put("proxyHost", "proxy.ntu.edu.tw");
System.getProperties().put("proxyPort", "8080");
如此之來,HttpUnit就會通过指定的代理服务器來发送请求与接受相应。
<!--EndFragment-->
分享到:
相关推荐
可以利用httpunit做网站接口测试和网站自动化测试
继承了httpunit selenium两组插件包。 包含完全源代码和JAVA包。 自己花了20多个积分,东拼西凑,用了一个星期,总算把程序调通。希望后来者能够减少弯路。 驱动器 C 中的卷是 系统 卷的序列号是 68C8-B91E C:\...
NULL 博文链接:https://jisonami.iteye.com/blog/2202505
使用HttpUnit进行集成测试使用HttpUnit进行集成测试使用HttpUnit进行集成测试使用HttpUnit进行集成测试
HttpUnit 是基于JUnit构建的一个开源测试框架,专门针对Web应用的测试,解决使用JUnit框架无法对远程Web内容进行测试,支持JS,cookies。。。。
内部含有httpunit单元测试所必需的jar包 httpunit.jar, js.jar, nekohtml.jar, xercesImpl.jar, xercesSamples.jar, servlet-api.jar, resolver.jar
使用HttpUnit提供的类完成集成测试源码
如何使用HttpUnit进行集成测试软件测试1HttpUnit简介HttpUnit是SourceForge下面的一个开源项目,它是基于JUnit的一个测试框架,主要关注于测试Web应用,解决使用JUnit框架无法对远程Web内容进行测试的弊端。...
java爬取工具httpunit全部jar包(包含httpunit使用需要的全部jar包),经过测试,可以使用
HttpUnit是SourceForge下面的一个开源项目,它是基于JUnit的一个测试框架,主要关注于测试Web应用,解决使用JUnit框架无法对远程Web内容进行测试的弊端。 HttpUnit让测试者可以通过Java类和服务器进行交互,并且将...
HttpUnit所有依赖打包下载,HttpUnit是一个集成测试工具,主要关注Web应用的测试,提供的帮助类让测试者可以通过Java类和服务器进行交互,并且将服务器端的响应当作文本或者DOM对象进行处理。HttpUnit还提供了一个...
HttpUnit 小结 HttpUnit 是基于JUnit构建的一个开源测试框架,专门针对Web应用的测试,解决使用JUnit框架无法对远程Web内容进行测试,支持JS,cookies。。。。
例子参见:http://blog.sina.com.cn/s/blog_025270e901017uz1.html 本人测试成功。jar包一个不少。
附件是一个完整的web工程(带源码),用jdk1.8及以上编译后可运行, HttpUtilTest类中有两个测试实例,所涉及的数据都有来自ajax异步的
java实现的web测试工具
用于以编程方式测试网站的库 org.httpunit/httpunit/1.7.2/httpunit-1.7.2.jar
单元测试时的两个第三方包 httpunit-1.7.zip junitperf-1.9.1.zip
xUnit的成员有很多,如JUnit,NUnit.PythonUnit,HtmlUnit,HttpUnit等。CppUnit就是xUnit家族中的一员,它是一个专门面向C++的单元测试框架。 CppUnit是Micheal Feathers由JUnit移植过来的一个在GNU LGPL条约下的并在...
一个关于junit的时候会用到的公共方法,如映射访问私有函数,给类属性设置值,httpunit设置访问web端测试