轻松

记述我学习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 轻松 阅读(3469) 评论(2)  编辑  收藏 所属分类: Junit相关

评论

# re: [软件测试]HttpUnit-测试用例(例子)[ZZ] 2005-07-25 13:03 amy

能测试成功吗?根本不能检测被访问的页面是否是登陆页面!  回复  更多评论   

# re: [软件测试]HttpUnit-测试用例(例子)[ZZ] 2005-07-25 20:03 relax

可以的。只要断言页面的标题就行了  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: