輕松

          記述我學習java的里程

          常用鏈接

          統計

          積分與排名

          友情鏈接

          最新評論

          [軟件測試]HttpUnit-測試用例(例子)[ZZ]

          (1)環境設置:導入HttpUnit

          (2)開始實踐,寫一個測試接口,起名為LoginTestInf:

          /*
           * Created on 2004-12-17
           *
           * TODO To change the template for this generated file go to
           * Window - Preferences - Java - Code Style - Code Templates
           */
          package org.apollo.test.util;

          /**
           * @author SixSun
           *
           * TODO To change the template for this generated type comment go to
           * Window - Preferences - Java - Code Style - Code Templates
           */

          /**
           *測試用例編號 : 0001
           *測試用例名稱 : HttpUnit 登陸驗證測試用例
           *測試目標 : 驗證用戶登陸是否成功
           *測試過程 :
           *1、輸入登陸地址的頁面地址,驗證該頁面是否可被正常訪問。
           *2、驗證被訪問的頁面是否是登陸頁面。
           *3、輸入非法用戶名、密碼,驗證登陸失敗。
           *4、輸入合法用戶名、密碼,驗證登陸成功。
           */     
          public interface LoginTestInf {
              public void testValidPage() throws Exception;
              public void testIsLoginPage() throws Exception;
              public void testBadLogin() throws Exception;
              public void testGoodLogin() throws Exception;
          }

          (3)實現一個Junit TestCase 同時 implements LoginTestInf 接口:

          /*
           * Created on 2004-12-17
           *
           * TODO To change the template for this generated file go to
           * Window - Preferences - Java - Code Style - Code Templates
           */
          package org.apollo.test.util;

          import java.net.URL;
          import junit.framework.TestCase;
          import junit.framework.TestSuite;
          import junit.textui.TestRunner;
           
          import com.meterware.httpunit.WebConversation;
          import com.meterware.httpunit.WebRequest;
          import com.meterware.httpunit.WebResponse;
          import com.meterware.httpunit.WebForm;
          import com.meterware.httpunit.GetMethodWebRequest;

          import org.apollo.test.util.LoginTestInf;

          /**
           * @author sixsun
           *
           * TODO To change the template for this generated type comment go to
           * Window - Preferences - Java - Code Style - Code Templates
           */
          public class LoginTest extends TestCase implements LoginTestInf {

           private String username = "suibian";
           private String password = "suibian";
           
              private WebConversation browser;
              private WebRequest requestIndex;
              private WebRequest requestLogin;
              private WebResponse responseIndex;
              private WebResponse responseLogin;
              private String urlSystem = "系統首頁網址";
              private String urlLogin = "登陸界面網址";
           /*
            * @see TestCase#setUp()
            */
           protected void setUp() throws Exception {
                  browser =  new WebConversation();
                  requestIndex = new GetMethodWebRequest(urlSystem);
                  responseIndex  = browser.getResponse(requestIndex);
                  requestLogin = new GetMethodWebRequest(urlLogin);
                  responseLogin  = browser.getResponse(requestLogin);       
           }
           
              //輸入登陸地址的頁面地址,驗證該頁面是否可被正常訪問
              public void testValidPage() throws Exception{
                     assertNotNull("zsonline在網絡上不存在!",responseIndex);
              }
             
              //驗證被訪問的頁面是否是登陸頁面
              public void testIsLoginPage() throws Exception{
                     URL currentUrl = responseLogin.getURL();
                     String currentUrlStr = currentUrl.getProtocol() + "://" +currentUrl.getHost() + currentUrl.getPath();
                     assertEquals("登陸頁面不是zsonline首頁!" ,currentUrlStr,urlLogin);
              }
             
              //輸入非法用戶名、密碼,驗證登陸失敗
              public void testBadLogin() throws Exception{
                    WebForm form = responseLogin.getForms()[0];
                    form.setParameter("username","badname");
                    form.setParameter("password","badpassword");
                    requestLogin = form.getRequest();
                    responseLogin =  browser.getResponse(requestLogin);
                    assertTrue("用戶名不存在,請確認用戶名輸入是否完全正確(區分大小寫)!",
                            responseLogin.getText().indexOf("用戶名不存在,請確認用戶名輸入是否完全正確(區分大小寫)!") != -1);
              }
             
             //輸入合法用戶名、密碼,驗證登陸成功
              public void testGoodLogin() throws Exception{
                    WebForm form = responseLogin.getForms()[0];
                    form.setParameter("username",username);
                    form.setParameter("password",password);//此處需要填寫真實密碼
                    requestLogin = form.getRequest();
                    responseLogin =  browser.getResponse(requestLogin);
                    assertTrue("轉到'zsonline'【suibian】用戶首頁失敗!",responseLogin.getText().indexOf("用戶測試用戶_zsonline,您好!") != -1);     
              }
           
              public static TestSuite suite(){
                  return new TestSuite(LoginTest.class);
                }
                public static void main(String args[]){
                  TestRunner.run(suite());
                }
          }

          posted on 2005-01-31 15:06 輕松 閱讀(3489) 評論(2)  編輯  收藏 所屬分類: Junit相關

          評論

          # re: [軟件測試]HttpUnit-測試用例(例子)[ZZ] 2005-07-25 13:03 amy

          能測試成功嗎?根本不能檢測被訪問的頁面是否是登陸頁面!  回復  更多評論   

          # re: [軟件測試]HttpUnit-測試用例(例子)[ZZ] 2005-07-25 20:03 relax

          可以的。只要斷言頁面的標題就行了  回復  更多評論   

          主站蜘蛛池模板: 元江| 左云县| 化州市| 永胜县| 桑植县| 崇明县| 平舆县| 小金县| 罗城| 宿迁市| 铜鼓县| 怀仁县| 平凉市| 天水市| 思茅市| 商丘市| 宁安市| 塘沽区| 恩平市| 富锦市| 阿合奇县| 甘德县| 广宁县| 墨江| 获嘉县| 肃北| 潮安县| 兰坪| 深泽县| 团风县| 江津市| 衡水市| 宁城县| 湖州市| 内黄县| 仙居县| 武夷山市| 德化县| 嵊泗县| 新巴尔虎左旗| 苗栗县|