import java.io.IOException;import java.net.MalformedURLException;import org.xml.sax.SAXException;import com.meterware.httpunit.GetMethodWebRequest;import com.meterware.httpunit.HttpUnitOptions;import com.meterware.httpunit.PostMethodWebRequest;import com.meterware.httpunit.WebConversation;import com.meterware.httpunit.WebForm;import com.meterware.httpunit.WebLink;import com.meterware.httpunit.WebRequest;import com.meterware.httpunit.WebResponse;public class HttpunitTest {public static void main(String[] args) {try {//HttpunitTest.testGetHtmlContent();//HttpunitTest.testGetMethod();//HttpunitTest.testPostMethod();HttpunitTest.testFormSubmit();} catch (Exception e) {e.printStackTrace();}}public static void testGetHtmlContent() throws IOException, SAXException{System.out.println("直接擷取網頁內容:"); HttpUnitOptions.setScriptingEnabled(false); // 建立一個WebConversation執行個體 WebConversation wc = new WebConversation(); // 向指定的URL發出請求,擷取響應 WebResponse wr = wc.getResponse("http://www.baidu.com.cn"); // 用getText方法擷取相應的全部內容 // 用System.out.println將擷取的內容列印在控制台上 System.out.println(wr.getText()); }/** * 用get方法擷取頁面內容 * @throws MalformedURLException * @throws IOException * @throws SAXException */ public static void testGetMethod() throws MalformedURLException, IOException, SAXException { System.out.println("向伺服器發送資料,然後擷取網頁內容:"); // 建立一個WebConversation執行個體 WebConversation wc = new WebConversation(); // 向指定的URL發出請求 WebRequest req = new GetMethodWebRequest("http://localhost:8080/test.html"); // 給請求加上參數 req.setParameter("query", "四氯化碳"); // 擷取響應對象 WebResponse resp = wc.getResponse(req); // 用getText方法擷取相應的全部內容 // 用System.out.println將擷取的內容列印在控制台上 System.out.println(resp.getText()); } /** * 用post方法擷取頁面內容 * @throws MalformedURLException * @throws IOException * @throws SAXException */ public static void testPostMethod() throws MalformedURLException, IOException, SAXException { System.out.println("使用Post方式向伺服器發送資料,然後擷取網頁內容:"); // 建立一個WebConversation執行個體 WebConversation wc = new WebConversation(); // 向指定的URL發出請求 WebRequest req = new PostMethodWebRequest("http://baidu.com/lii"); // 給請求加上參數 req.setParameter("user_name", "test"); req.setParameter("password", "111111"); // 擷取響應對象 WebResponse resp = wc.getResponse(req); // 用getText方法擷取相應的全部內容 // 用System.out.println將擷取的內容列印在控制台上 System.out.println(resp.getText()); } /** * 擷取頁面連結並類比點擊 * @throws MalformedURLException * @throws IOException * @throws SAXException */ public static void testClickLink() throws MalformedURLException, IOException, SAXException { System.out.println("擷取頁面中連結指向頁面的內容:"); // 建立一個WebConversation執行個體 WebConversation wc = new WebConversation(); // 擷取響應對象 WebResponse resp = wc.getResponse("http://www.265.com/"); // 獲得頁面連結化物件 WebLink link = resp.getLinkWith("百度"); // 類比使用者單擊事件 link.click(); // 獲得當前的響應對象 WebResponse nextLink = wc.getCurrentPage(); // 用getText方法擷取相應的全部內容 // 用System.out.println將擷取的內容列印在控制台上 System.out.println(nextLink.getText()); } /** * 測試WebForm的處理表單和提交能力 */public static void testFormSubmit(){HttpUnitOptions.setScriptingEnabled(false);WebConversation wc = new WebConversation();WebRequest request = new PostMethodWebRequest("http://baidu.com/li/");WebResponse response = null;try {response = wc.getResponse(request);//獲得Html中的form表單,HttpUnit將他封裝成WebForm對象WebForm form = response.getForms()[0];//WebForm對象提供getParameterValue的方法將根據表單中的name獲得對應的value,而不用管該元素的類型。//對錶單進行處理操作form.setParameter("username", "le");form.setParameter("password", "le");//提交表單 獲得新的responseresponse = form.submit();System.out.println(response.getText());System.out.println("----------------------------");// 獲得頁面連結化物件 WebLink link = response.getLinkWith("新聞中心"); //類比使用者單擊事件 link.click(); // 獲得當前的響應對象 WebResponse nextLink = wc.getCurrentPage(); // 用getText方法擷取相應的全部內容 // 用System.out.println將擷取的內容列印在控制台上 System.out.println(nextLink.getText()); } catch (IOException e) {e.printStackTrace();} catch (SAXException e) {e.printStackTrace();}}}
常用語句:
initialPage.getLinkWithID( "go" ).click();
wc.getResponse( getHostPath() + "/TextPage.txt" ).getReceivedPage().getTitle();
structuredPage.getDOM()
wc.getClientProperties().setOverrideContextType( "text/html" );
simplePage.getTitle() );
simplePage.getCharacterSet()
new GetMethodWebRequest( "file:" + file.getAbsolutePath() )
addResourceHeader( "SimplePage.html", "Refresh: 2;URL=NextPage.html" );
wc.getClientProperties().setAutoRefresh( true );
simplePage.getMetaTagContent("name","robots"));
simplePage.getMetaTagContent("name","keywords"));
simplePage.getMetaTagContent("http-equiv","Expires")
simplePage.getExternalStyleSheet()
simplePage.getElementWithID( "aForm" )
simplePage.getElementsWithName( "aForm" )
simplePage.getElementsWithAttribute( "class", "first" )
wc.getResponse("/NoLinks.html" ).getLinks()
simplePage.getLinkWith( "no link" )
getLinkWithImageText( "Next -->" )
HttpUnitOptions.setImagesTreatedAsAltText( true );
simplePage.getLinkWith( "Next -->" )
simplePage.getFirstMatchingLink( WebLink.MATCH_URL_STRING, "/other.html" )
simplePage.getLinkWithName( "nextLink" )
simplePage.getElementWithID( "activeID" )
initialPage.getLinkWithImageText("Blah Blah")
nextPage.getTitle()
link.click()
link.getTarget() ).getTitle()
wc.getResponse( link.getRequest() )
request.getRequestParameterNames()
HttpUnitOptions.setLoggingHttpHeaders(false);
HttpUnitOptions.setScriptingEnabled( false );
simplePage.getImages()
WebImage image = simplePage.getImageWithAltText( "one" )
simplePage.getImageWithSource( "junk.png" )
link.getURLString()
imageRequest.getURL().toExternalForm()
wc.getFrameNames()
wc.getFrameContents( "_top" )
e.getResponseCode()
e.getResponseMessage()
response.getText()
response.getContentType()
wc.getCookieValue( "name" )
wc.getCookieNames()
wc.getCookieDetails("age")
wc.putCookie( "EMPTYVALUE", "non-empty" );
wc.getClientProperties().setUserAgent( "me alone" );
wc.setHeaderField( "junky", "Mozilla 6" );
wc.setAuthorization( "user", "password" );
wc.setProxyServer( "localhost", getHostPort(), "user", "password" );
WebForm form = wr.getFormWithID( "main" );
form.setParameter( "name", "master" );
form.submit();
SubmitButton button = form.getSubmitButton( "save", button.click()
page.getFormWithID( "form" ).getSubmitButton( "update" )
page.getFormWithName( "nobody" )
form.getParameterNames()
form.hasParameterNamed( "third" )
form.getParameterValue( "name" )
WebForm form = _wc.getResponse( getHostPath() + "/OneForm.html" ).getForms()[0];
WebRequest request = form.getRequest();
request.setParameter( "name", "master" );
form.reset()
response.getTextBlocks() 擷取段落
BlockElement paragraph = response.getTextBlocks()[1];
paragraph.getLinks()
paragraph.getLinkWithID( "httpunit" )
response.getTextBlocks()[0].getLinkWithID( "httpunit" )
paragraph.getFirstMatchingLink( WebLink.MATCH_CONTAINED_TEXT, "home page" )
paragraph.getLinkWithID( "httpunit" ).getRequest().getURL().toExternalForm()
request.getURL().toExternalForm()
table.getRowCount()
table.getColumnCount()
table.getCellAsText( 2, 0 )
page.getTableWithID("table")
table.getTableCellWithID("id2")
form.getMethod()
form.getAction()
form.getScriptableObject().setAction( "/tell" )
form.getSubmitButtons()
form.getButtonWithID("doit")
參考文章: http://www.myexception.cn/software/753419.html
http://blog.csdn.net/zhongweijian/article/details/7619299