//用System.out.println將獲取的內容打印在控制臺上
System.out.println( resp.getText() );
大家關注一下上面代碼中打了下劃線的兩處內容,應該可以看到,使用Get、Post方法訪問頁面的區別就是使用的請求對象不同。
4.2 處理頁面中的鏈接
這里的演示是找到頁面中的某一個鏈接,然后模擬用戶的單機行為,獲得它指向文件的內容。比如在我的頁面HelloWorld.html中有一個鏈接,它顯示的內容是TestLink,它指向我另一個頁面TestLink.htm. TestLink.htm里面只顯示TestLink.html幾個字符。
下面是處理代碼:
System.out.println("獲取頁面中鏈接指向頁面的內容:");
//建立一個WebConversation實例
WebConversation wc = new WebConversation();
//獲取響應對象
WebResponse resp = wc.getResponse( " http://localhost:6888/HelloWorld.html " );
//獲得頁面鏈接對象
WebLink link = resp.getLinkWith( "TestLink" );
//模擬用戶單擊事件
link.click();
//獲得當前的響應對象
WebResponse nextLink = wc.getCurrentPage();
//用getText方法獲取相應的全部內容
//用System.out.println將獲取的內容打印在控制臺上
System.out.println( nextLink.getText() );
文章來源于領測軟件測試網 http://www.k11sc111.com/