谁有htmlunit测试的实例,重谢

CrazyCoder 2005-01-18 02:54:50
rt
...全文
423 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
廖雪峰 2005-01-19
  • 打赏
  • 举报
回复
如果你的web层使用mvc并且逻辑全部放在javabean/ejb中,那么我觉得没有必要用httpunit测试,只需要junit测试逻辑组件就可以了。

如果逻辑放在servelt或者jsp中,那估计测试的时间比写代码的时间还要长好几倍,如果把数据库操作也写到了jsp中,那么基本上能跑起来就已经不错了。
jacklondon 2005-01-19
  • 打赏
  • 举报
回复
用 JUnit 做 unit test, 用 JMeter 做性能测试
CrazyCoder 2005-01-19
  • 打赏
  • 举报
回复
测试结果如下

Hello!
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变成乱码了
CrazyCoder 2005-01-19
  • 打赏
  • 举报
回复
asklxf(xuefeng)所言不错

我只是想测试一下页面显示的内容,所以用htmlUnit
不过发现它对中文支持好像有问题
CrazyCoder 2005-01-19
  • 打赏
  • 举报
回复
package com.ziliu;

import java.net.URL;
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包

67,514

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧