您的位置:軟件測(cè)試 > 開源軟件測(cè)試 > 開源單元測(cè)試工具 > HttpUnit
應(yīng)用HttpUnit進(jìn)行Web測(cè)試
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2013/4/8 16:05:52 ] 推薦標(biāo)簽:

  4.2 獲取指定頁面的內(nèi)容

  4.2.1 通過 getText 直接獲取頁面的所有內(nèi)容

  // 建立一個(gè)“瀏覽器”實(shí)例

  WebConversation wc = new WebConversation();

  // 將指定URL的請(qǐng)求傳給wc,然后獲取相應(yīng)的響應(yīng)

  WebResponse wr = wc.getResponse( "http://www.sqalab.com" );

  // 用wc的getText方法獲取相應(yīng)的全部?jī)?nèi)容

  System.out.println( wr.getText() );

  4.2.2 增加參數(shù)通過Get方法訪問頁面

  // 建立一個(gè)WebConversation實(shí)例

  WebConversation wc = new WebConversation();

  // 向指定的URL發(fā)出請(qǐng)求

  WebRequest req = new GetMethodWebRequest( "http://www.sqalab.com/search" );

  // 給請(qǐng)求加上參數(shù)

  req.setParameter("keyword","httpunit");

  // 獲取響應(yīng)對(duì)象

  WebResponse resp = wc.getResponse( req );

  // 用getText方法獲取相應(yīng)的全部?jī)?nèi)容

  System.out.println( resp.getText() );

  4.2.3 增加參數(shù)通過Post方法訪問頁面

  //建立一個(gè)WebConversation實(shí)例

  WebConversation wc = new WebConversation();

  //向指定的URL發(fā)出請(qǐng)求

  WebRequest req = new PostMethodWebRequest( "http://www.sqalab.com/search" );

  //給請(qǐng)求加上參數(shù)

  req.setParameter("keyword","httpunit");

  //獲取響應(yīng)對(duì)象

  WebResponse resp = wc.getResponse( req );

  //用getText方法獲取相應(yīng)的全部?jī)?nèi)容

  //用System.out.println將獲取的內(nèi)容打印在控制臺(tái)上

  System.out.println( resp.getText() );

  4.3 處理頁面的鏈接(links)

  模擬用戶點(diǎn)擊請(qǐng)求頁面中的某一個(gè)鏈接,然后獲得它指向文件的內(nèi)容。比如在頁面index.html中有一個(gè)鏈接 "應(yīng)用 HttpUnit 進(jìn)行Web測(cè)試 ",它顯示的內(nèi)容是這篇文章的內(nèi)容,它鏈向的頁面是http://www.sqalab.com/article/html/article_59.html.

  // 建立一個(gè)WebConversation實(shí)例

  WebConversation wc = new WebConversation();

  // 獲取響應(yīng)對(duì)象

  WebResponse resp = wc.getResponse( "http://www.sqalab.com/index.html" );

  // 獲得頁面鏈接對(duì)象

  WebLink link = resp.getLinkWith( "應(yīng)用 HttpUnit 進(jìn)行Web測(cè)試 " );

  // 模擬用戶單擊事件

  link.click();

  // 獲得當(dāng)前的響應(yīng)對(duì)象

  WebResponse nextLink = wc.getCurrentPage();

  // 用getText方法獲取相應(yīng)的全部?jī)?nèi)容,并打印

  System.out.println( nextLink.getText() );

上一頁123456下一頁
軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd