6 wc.setProxyServer( "your.proxy.com", 80 );
7 WebResponse response = wc.getResponse(request);
8 WebLink httpunitLink =
response.getFirstMatchingLink(WebLink.MATCH_CONTAINED_TEXT,"Cookbook");
9 response = httpunitLink.click();
10 System.out.println("Test successful !!");
11 } catch (Exception e) {
12 System.err.println("Exception: " + e);
13 }
14 }
15 }
清單 1 中的代碼用 your.proxy.com (第 6 行)連接 Internet。如果存在直接 Internet 連接,那么可以把這個語句注釋掉。第 8 行的語句在頁面中搜索包含文本 Cookbook 的 Web 鏈接。第 9 行的語句用于單擊這個鏈接。如果找到鏈接,那么用戶會看到 Test Successful !! 這條消息。
用 jWebUnit 進行的測試更簡單
清單 2 的測試用例用 jWebUnit API 執行和清單 1 相同的任務:
清單 2. 單擊 HttpUnit 主頁上 Cookbook 鏈接的 jWebUnit 代碼
1 public class JWebUnitTest extends WebTestCase{
2 public static void main(String[] args){
3 junit.textui.TestRunner.run(new TestSuite(JWebUnitTest.class));
4 }
5 public void setUp(){
文章來源于領測軟件測試網 http://www.kjueaiud.com/