如果你的web层使用mvc并且逻辑全部放在javabean/ejb中,那么我觉得没有必要用httpunit测试,只需要junit测试逻辑组件就可以了。如果逻辑放在servelt或者jsp中,那估计测试的时间比写代码的时间还要长好几倍,如果把数据库操作也写到了jsp中,那么基本上能跑起来就已经不错了。
解决方案 »
- java web同一账号不能同时登陆?
- 往数据库中插入数据时插入一部分就报 Too many variables.错误 急救答案
- 配置了php+apache+mySql就无法启动MySql服务和IIS服务了
- spring中注入接口,配置文件该怎么写啊?
- iReport 问题
- java小白求看下代码
- 请教怎样用ant进行JUnit自动测试!
- 求Jboss4文档,多谢!!!
- 在哪里可以找到JBuilder7.0的序列号?
- spring data mongodb自动注入mongoTemplate为null
- spring中ClassPathXmlApplicationContext的一点疑惑
- resin中session的丢失问题?
import java.util.Iterator;
import java.util.List;import junit.framework.TestCase;import org.w3c.dom.Document;import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlTable;
import com.gargoylesoftware.htmlunit.html.HtmlTableCell;
import com.gargoylesoftware.htmlunit.html.HtmlTableRow;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;public class htmlUnit extends TestCase{
private HtmlPage homePage = null;
private HtmlPage searchResultPage = null; private Document pageDocument = null;
private WebClient webClient = null;
protected void setUp() throws Exception {
webClient = new WebClient();
final URL url = new URL("http://www.baidu.com");
System.out.println("Hello!");
homePage = (HtmlPage)webClient.getPage(url);
assertNotNull(homePage);
}
public void testHomePage() throws Exception {
System.out.println("testHomePage!");
System.out.println("TitleText:" + homePage.getTitleText());
// assertEquals("百度——全球最大中文搜索引擎 ", homePage.getTitleText()); }
public void testSearch() throws Exception {
//set the parameters for search
System.out.println("testSearch!");
List formList = (List) homePage.getAllForms();
HtmlForm searchForm = (HtmlForm) formList.get(0); HtmlTextInput inputTitle = (HtmlTextInput)searchForm.getInputByName("wd");
inputTitle.setValueAttribute("Toshiba");
try {
searchResultPage = (HtmlPage)searchForm.submit();
assertNotNull(searchResultPage);
System.out.println("TitleText:" + searchResultPage.getTitleText());
HtmlTable table = (HtmlTable)searchResultPage.getDocumentElement().getHtmlElementsByTagName("table").get(2);
List rows = table.getRows();
//System.out.println( "Cell (0,0)="+ table.getCellAt(0,0).asText()); Iterator rowIterator = rows.iterator();
while( rowIterator.hasNext() ) {
HtmlTableRow row = (HtmlTableRow)rowIterator.next();
System.out.println("Found row");
List cells = row.getCells();
Iterator cellIterator = cells.iterator();
while( cellIterator.hasNext() ) {
HtmlTableCell cell = (HtmlTableCell)cellIterator.next();
System.out.println("Found cell: "+cell.asText());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected void tearDown() {
webClient = null;
}}自己写了一个,需要http://htmlunit.sourceforge.net/下载htmlUnit包
不过发现它对中文支持好像有问题
testHomePage!
TitleText:°??????????ò×??ó???????÷????
Hello!
testSearch!
2005-1-19 14:37:48 com.gargoylesoftware.htmlunit.html.HtmlPage loadJavaScriptFromUrl
警告: Expected content type of text/javascript or application/x-javascript for remotely loaded javascript element http://post.baidu.com/f?ct=301989888&tn=baiduForumSearch1&rn=10&pn=0&word=Toshiba but got [text/html]
TitleText:°??????÷_Toshiba
Found row
Found cell:
Found cell: 找toshiba上淘宝网淘宝
Found cell: 找toshiba信息在阿里巴巴
Found cell: 找toshiba信息在搜捕网
Found cell: 找toshiba相关项目title变成乱码了