httpunit基础教程

上传人:ji****en 文档编号:107687362 上传时间:2019-10-20 格式:PDF 页数:24 大小:429.17KB
返回 下载 相关 举报
httpunit基础教程_第1页
第1页 / 共24页
httpunit基础教程_第2页
第2页 / 共24页
httpunit基础教程_第3页
第3页 / 共24页
httpunit基础教程_第4页
第4页 / 共24页
httpunit基础教程_第5页
第5页 / 共24页
点击查看更多>>
资源描述

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

1、TIB 自动化测试工作室 HttpUnit 简介简介 主页: HttpUnit 是 SourceForge 下面的一个开源项目,它是基于 JUnit 的一个测试框架,主要关注 于测试 Web 应用,解决使用 JUnit 框架无法对远程 Web 内容进行测试的弊端。 HttpUnit 让测试者可以通过 Java 类和服务器进行交互,并且将服务器端的响应当作文本或 者 DOM 对象进行处理。HttpUnit 还提供了一个模拟 Servlet 容器,让你可以不需要发布 Servlet,就可以对 Servlet 的内部代码进行测试。 为了让 HtpUnit 正常运行,需要安装 JDK1.3.1 或者以

2、上版本。 Automated 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 you

3、 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 bro

4、wser 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 fair

5、ly 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

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

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

8、tpUnit 因为关注点是这些控件的内容,所以不管你的外在表现形式如何变化,都 不影响你已确定测试的可重用性。 目前最新版本: 20 May 2008 HttpUnit 1.7 released 下载并解压 HttpUnit 之后,目录结构应该如下所示: httpunit +- jars /包含创建、测试以及运行 HttpUnit 所必须的 jar | +- lib/ 包含 HttpUnit jar | +- doc/文档 | |+- tutorial/基于 servlet web 网站的测试优先开发的简单教程 | |+- api/ javadoc | |+- manual/ 用户手册 | +

9、- examples / 采用 HttpUnit 编写的一些示例程序 | +- src/ HttpUnit 源代码 | +- test/ HttpUnit 单元测试的一些很好的例子 只有 lib 和 jars 两个目录对运行 HttpUnit 是必须的。 至少你必须将 HttpUnit jar 添加到系 统的 classpath,而其他的一些 jar 均为可选。 HttpUnit 包括许多可选的功能。 如果你并不需要这些功能, 则不必在 classpath 中包含相 应的库但至少你必须有一个 HTML 解析器 (JTidy 和 NekoHTML 都可被支持) 和一个与 jaxp 兼容的解析器(

10、在发行版中包含了 xerces 2.2) 。 Jar 名称所需关系相关文档 nekohtml.jarHTML 解析器即使是再 糟糕的 HTML 也可适用。需 要 xerces-j 2.2 或更高版本 www.apache.org/andyc/neko/doc/html/index.html. tidy.jar要求苛刻的 HTML 解析器。 可与任何兼容 jaxp 解析器配 合使用 xmlParserAPIs.jar支持 xerces-j 的通用解析 APIxml.apache.org xercesImpl.jarxerces-j 2.2 可执行单元xml.apache.org js.jar支持

11、 javascriptwww.mozilla.org/rhino servlet.jarservlet单 元 测 试 工 具 ServletUnit 所必须的 J junit.jar运行单元测试www.junit.org mail.jar测试文件的上传功能(运行 HttpUnit 本身并不需要) J activation.jar测试文件的上传功能(运行 HttpUnit 本身并不需要) J 直接获取页面内容直接获取页面内容 The very first step in any interaction with a web site is to obtain a start page. To d

12、o 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 = wc.getResponse( “ “ ); System.

13、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.SAXException; import com.met

14、erware.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.printStackTrace();

15、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.s

16、etParameter(“in“,“home“); /获取响应对象 WebResponse resp; try resp = wc.getResponse( req ); /用getText方法获取相应的全部内容 /用System.out.println将获取的内容打印在控制台上 System.out.println( resp.getText() ); catch (IOException e) e.printStackTrace(); catch (SAXException e) e.printStackTrace(); 测试测试 WebTours 的例子:的例子: import java.io.IOException; import org.xml.sax.SAXException; import com.meterware.httpunit.*; public class Test /* *

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 电子/通信 > 综合/其它

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