HttpUnit基础教程.doc

上传人:大米 文档编号:559374624 上传时间:2023-12-06 格式:DOC 页数:24 大小:247KB
返回 下载 相关 举报
HttpUnit基础教程.doc_第1页
第1页 / 共24页
HttpUnit基础教程.doc_第2页
第2页 / 共24页
HttpUnit基础教程.doc_第3页
第3页 / 共24页
HttpUnit基础教程.doc_第4页
第4页 / 共24页
HttpUnit基础教程.doc_第5页
第5页 / 共24页
点击查看更多>>
资源描述

《HttpUnit基础教程.doc》由会员分享,可在线阅读,更多相关《HttpUnit基础教程.doc(24页珍藏版)》请在金锄头文库上搜索。

1、TIB自动化测试工作室http:/ testing is a great way to ensure that code being maintained works. The Extreme Programming (XP) methodology relies heavily on it, and practitioners have available to them a range of testing frameworks, most of which work by making direct calls to the code being tested. But what if

2、you want to test a web application? Or what if you simply want to use a web-site as part of a distributed application?In either case, you need to be able to bypass the browser and access your site from a program. HttpUnit makes this easy. Written in Java, HttpUnit emulates the relevant portions of b

3、rowser behavior, including form submission, JavaScript, basic http authentication, cookies and automatic page redirection, and allows Java test code to examine returned pages either as text, an XML DOM, or containers of forms, tables, and links. When combined with a framework such as JUnit, it is fa

4、irly easy to write tests that very quickly verify the functioning of a web site.The same techniques used to test web sites can be used to test and develop servlets without a servlet container using ServletUnit, included in the download.HTTPUnit的工作原理:HttpUnit通过模拟浏览器的行为,处理页面框架(frames),cookies,页面跳转(red

5、irects)等。通过HttpUnit提供的功能,你可以和服务器端进行信息交互,将返回的网页内容作为普通文本、XML DOM对象或者是作为链接、页面框架、图像、表单、表格等的集合进行处理。可以结合使用JUnit框架进行测试。还可以导向一个新的页面,然后进行新页面的处理,这个功能使你可以处理一组在一个操作链中的页面。WebConversation类模拟浏览器与网站服务器进行交互WebRequest类发送请求WebResponse类接收响应getTextgetURLgetTablesgetLinks、getLinkWithgetForms可以测试:1、测试某个指定的页面是否存在2、测试页面跳转是否

6、正确3、测试页面内容是否正确4、测试链接5、测试表单HTTPUnit和其他商业工具的对比:商业工具一般使用记录、回放的功能来实现测试,但是这里有个缺陷,就是当页面设计被修改以后,这些被记录的行为就不能重用了,需要重新录制才能继续测试。 举个例子:如果页面上有个元素最先的设计是采用单选框,这个时候你开始测试,那么这些工具记录的就是你的单项选择动作,但是如果你的设计发生了变化,比如说我改成了下拉选择,或者使用文本框接受用户输入,这时候,你以前录制的测试过程就无效了,必须要重新录制。 而HttpUnit因为关注点是这些控件的内容,所以不管你的外在表现形式如何变化,都不影响你已确定测试的可重用性。目前

7、最新版本:20 May 2008HttpUnit 1.7 released下载并解压HttpUnit之后,目录结构应该如下所示:httpunit +- jars /包含创建、测试以及运行HttpUnit所必须的jar | +- lib / 包含HttpUnit jar | +- doc /文档 | | | +- tutorial /基于servlet web网站的测试优先开发的简单教程 | | | +- api / javadoc | | | +- manual / 用户手册 | +- examples / 采用HttpUnit编写的一些示例程序 | +- src / HttpUnit 源代码

8、 | +- test / HttpUnit单元测试的一些很好的例子只有lib和jars两个目录对运行HttpUnit是必须的。至少你必须将HttpUnit jar添加到系统的classpath,而其他的一些jar均为可选。HttpUnit包括许多可选的功能。如果你并不需要这些功能,则不必在classpath中包含相应的库但至少你必须有一个HTML解析器(JTidy和NekoHTML都可被支持)和一个与jaxp兼容的解析器(在发行版中包含了xerces 2.2)。Jar名称所需关系相关文档nekohtml.jarHTML解析器即使是再糟糕的HTML也可适用。需要xerces-j 2.2 或更高版

9、本www.apache.org/andyc/neko/doc/html/index.html.tidy.jar要求苛刻的HTML解析器。可与任何兼容jaxp解析器配合使用 2.2可执行单元xml.apache.orgjs.jar支持javascriptwww.mozilla.org/rhinoservlet.jarservlet单元测试工具ServletUnit 所必须的Jjunit.jar运行单元测试www.junit.orgmail.jar测试文件的上传功能(运行HttpUnit本身并不需要)J very first step in any interaction with a web s

10、ite is to obtain a start page. To do this, we create a WebConversation object to play the role of the web browser. This object will store browser state such a cookies, windows, and so on. We then ask for the page by specifying the desired URL:WebConversation wc = new WebConversation();WebResponse wr

11、 = wc.getResponse( http:/ );System.out.println( wr.getText() );This example will simply print out the text of the retrieved page. Obviously, given this text, it is easy to search for particular strings on the page, if that is desired.在Eclipse中使用HttpUnit:import java.io.IOException;import org.xml.sax.

12、SAXException;import com.meterware.httpunit.*;public class Test /* * param args */public static void main(String args) WebConversation wc = new WebConversation();WebResponse wr = null;try wr = wc.getResponse( http:/127.0.0.1:1080/WebTours/ );System.out.println( wr.getText() ); catch (IOException e) e

13、.printStackTrace(); catch (SAXException e) e.printStackTrace();通过Get方法访问页面并且加入参数 例:System.out.println(向服务器发送数据,然后获取网页内容:); /建立一个WebConversation实例 WebConversation wc = new WebConversation(); /向指定的URL发出请求 WebRequest req = new GetMethodWebRequest( http:/127.0.0.1:1080/WebTours/nav.pl); /给请求加上参数 req.setParameter(in,home

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号