import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
public class TestHtmlUnit extends TestCase{
/**
* 测试主页标题
* @throws FailingHttpStatusCodeException
* @throws MalformedURLException
* @throws IOException
*/
public void testHomePage() throws FailingHttpStatusCodeException, MalformedURLException, IOException{
final WebClient webClient = new WebClient();
final HtmlPage startPage = webClient.getPage("http://htmlunit.sourceforge.net");
// assertEquals("HtmlUnit - Welcome to HtmlUnit", startPage.getTitleText());
System.out.println("title:"+startPage.getTitleText());
}
/**
* 测试浏览器
* @throws Exception
*/
public void testHomePage_Firefox() throws Exception {
//火狐2/3
// final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_2);
//IE6/7
final WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_7);
final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
// assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText());
System.out.println("title:"+page.getTitleText());
}
/**
* 测试获取一个div元素里面的值
* @throws Exception
*/
public void testGetElements() throws Exception {
final WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_7);
final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
//随便找一个div有ID名字的,测试的是一个id为breadcrumbs的标签
final HtmlDivision div = page.getHtmlElementById("breadcrumbs");
System.out.println("div content as text:\n"+div.asText());
System.out.println("\ndiv content as xml:\n"+div.asXml());
/**
* 输出如下:
div content as text:
Last Published: 21 April 2009 | Version: 2.5
div content as xml:
<div id="breadcrumbs">
<div class="xleft">
Last Published: 21 April 2009
?| Version: 2.5
</div>
<div class="xright">
</div>
<div class="clear">
<hr/>
</div>
</div>
*说明:
*作为文本输出的话,只读出标签内的内容;
*作为xml输出的话,就讲div标签对的内容原封不动的输出,有利于自己的进一步处理。
*
*/
}
public void testSubmittingForm() throws Exception {
final WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_6);
// Get the first page
// 获取首页
// final HtmlPage page1 = webClient.getPage("http://some_url");
HtmlPage page = webClient.getPage(http://219.238.180.***:8080/logincheck.php?USERNAME=******&PASSWORD=******);
System.out.println(page.getTitleText());
// page = webClient.getPage("http://219.238.180.***:8080/general/mytable/intel_view/");
// HtmlElement e = page.getElementById("module_1");
// String noticeList = e.asXml();
// System.out.println(noticeList);
// Get the form that we are dealing with and within that form,
// find the submit button and the field that we want to change.
// 根据form的名字获取页面表单,也可以通过索引来获取:page.getForms().get(0)
// final HtmlForm form = page1.getFormByName("myform");
//
// final HtmlSubmitInput button = form.getInputByName("submitbutton");
// final HtmlTextInput textField = form.getInputByName("userid");
//
// // Change the value of the text field
// //设置表单域的值
// textField.setValueAttribute("root");
//
// // Now submit the form by clicking the button and get back the second page.
// //提交表单,返回提交表单后跳转的页面
// final HtmlPage page2 = button.click();
}
public Map<String,Object> getNoticeList(String username) {
Map<String,Object> map = new HashMap<String,Object>();
WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_6);
// WebClient webClient = new WebClient();
// webClient.setJavaScriptEnabled(false);
// webClient.setThrowExceptionOnScriptError(false);
HtmlPage page = null;
String noticeList="";
String errorInfo="";
int success = 0;
try {
//采用get方式获取数据,此处的url地址应该根据配置文件进行设置
page = (HtmlPage) webClient.getPage("http://219.238.180.***:8080/logincheck.php?USERNAME=*****&PASSWORD=123456");
page = webClient.getPage("http://219.238.180.***:8080/general/mytable/intel_view/");
HtmlElement e = page.getElementById("module_1");
noticeList = e.asXml();
// noticeList = e.asText();
success = 1;
} catch (FailingHttpStatusCodeException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
map.put("getFlag", success);
map.put("result", noticeList);
map.put("errorInfo",errorInfo);
return map;
}
public static void main(String [] args) {
// Map<String,Object> map = new TestHtmlUnit().getNoticeList("");
// String result = (String)map.get("result");
// System.out.println(result);
}
}
小刚写的一段测试代码,以后或许还能用上