不一样的思想~~
http://shop34712791.taobao.com
MSN:wins0910@hotmail.com
httpunit
上一篇 /
下一篇 2008-08-28 22:08:13
/ 个人分类:白盒测试
1\直接获取页面内容
package onlyfun.caterpillar.test;
import java.io.IOException;
import java.net.MalformedURLException;
import org.xml.sax.SAXException;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import junit.framework.TestCase;
public class FirstHttpUnitTest extends TestCase {
public void testCaterpillarOnlyfun()
throws MalformedURLException,
IOException,
SAXException {
WebConversation webConversation =
new WebConversation();
WebRequest request =
new GetMethodWebRequest(
"http://caterpillar.onlyfun.net/phpBB2/");
WebResponse response =
webConversation.getResponse(request);
assertTrue(response.getTitle().startsWith(
"caterpillar.onlyfun.net"));
}
public static void main(String[] args) {
junit.swingui.TestRunner.run(
FirstHttpUnitTest.class);
}
}
2\通过Get方法访问页面并且加入参数
System.out.println("向服务器发送数据,然后获取网页内容:");
//建立一个WebConversation实例
WebConversation wc=new WebConversation();
//向指定的URL发出请求
WebRequest req=new GetMethodWebRequest( " http://localhost:6888/HelloWorld.jsp " );
//给请求加上参数
req.setParameter("username","姓名");
//获取响应对象
WebResponse resp=wc.getResponse( req );
//用getText方法获取相应的全部内容
//用System.out.println将获取的内容打印在控制台上
System.out.println( resp.getText() );
3\通过Post方法访问页面并且加入参数
//建立一个WebConversation实例
WebConversation wc=new WebConversation();
//向指定的URL发出请求
WebRequest req=new PostMethodWebRequest( " http://localhost:6888/HelloWorld.jsp " );
//给请求加上参数
req.setParameter("username","姓名");
//获取响应对象
WebResponse resp= wc.getResponse( req );
//用getText方法获取相应的全部内容
//用System.out.println将获取的内容打印在控制台上
System.out.println( resp.getText() );
4\处理页面中的链接
这里的演示是找到页面中的某一个链接,然后模拟用户的单机行为,获得它指向文件的内容。比如在我的页面HelloWorld.html中有一个链接,它显示的内容是TestLink,它指向我另一个页面TestLink.htm. TestLink.htm里面只显示TestLink.html几个字符
System.out.println("获取页面中链接指向页面的内容:");
//建立一个WebConversation实例
WebConversation wc=new WebConversation();
//获取响应对象
WebResponse resp=wc.getResponse( " http://localhost:6888/HelloWorld.html " );
//获得页面链接对象
WebLink link=resp.getLinkWith( "TestLink" );
//模拟用户单击事件
link.click();
//获得当前的响应对象
WebResponse nextLink=wc.getCurrentPage();
//用getText方法获取相应的全部内容
//用System.out.println将获取的内容打印在控制台上
System.out.println( nextLink.getText() );
5\处理页面中的表格
表格是用来控制页面显示的常规对象,在HttpUnit中使用数组来处理页面中的多个表格,你可以用resp.getTables()方法获取页面所有的表格对象。他们依照出现在页面中的顺序保存在一个数组里面。
[注意] Java中数组下标是从0开始的,所以取第一个表格应该是resp.getTables()[0],其他以此类推。
下面的例子演示如何从页面中取出第一个表格的内容并且将他们循环显示出来:
//建立一个WebConversation实例
WebConversation wc=new WebConversation();
//获取响应对象
WebResponse resp=wc.getResponse( " http://localhost:6888/HelloWorld.html " );
//获得对应的表格对象
WebTable webTable=resp.getTables()[0];
//将表格对象的内容传递给字符串数组
String[][]datas=webTable.asText();
//循环显示表格内容
inti=0,j=0;
intm=datas[0].length;
intn=datas.length;
while(i<n){
j=0;
while(j<m){
System.out.println("表格中第"+(i+1)+"行第"+(j+1)+"列的内容是:"+datas[j]);
++j;
}
++i;
}
6\处理页面中的表单
表单是用来接受用户输入,也可以向用户显示用户已输入信息(如需要用户修改数据时,通常会显示他以前输入过的信息),在HttpUnit中使用数组来处理页面中的多个表单,你可以用resp.getForms()方法获取页面所有的表单对象。他们依照出现在页面中的顺序保存在一个数组里面。
[注意] Java中数组下标是从0开始的,所以取第一个表单应该是resp.getForms()[0],其他以此类推。
下面的例子演示如何从页面中取出第一个表单的内容并且将他们循环显示出来
System.out.println("获取页面中表单的内容:");
//建立一个WebConversation实例
WebConversation wc=new WebConversation();
//获取响应对象
WebResponse resp=wc.getResponse( " http://localhost:6888/HelloWorld.html " );
//获得对应的表单对象
WebForm webForm=resp.getForms()[0];
//获得表单中所有控件的名字
String[]pNames=webForm.getParameterNames();
inti=0;
intm=pNames.length;
//循环显示表单中所有控件的内容
while(i<m){
System.out.println("第"+(i+1)+"个控件的名字是"+pNames+",里面的内容是"+
webForm.getParameterValue(pNames));
++i;
}
收藏
举报
TAG:
白盒测试