Be happy with Java

今天写了个http-unit

LoginTest.java
 1package com.asmiling.test.login;
 2
 3/**
 4 * <p>Title: </p>
 5 *
 6 * <p>Description: </p>
 7 *
 8 * <p>Copyright: Copyright (c) 2006</p>
 9 *
10 * <p>Company: </p>
11 *
12 * @author sybil
13 * @version 1.0
14 */

15import java.net.URL;
16import junit.framework.TestCase;
17import junit.framework.TestSuite;
18import junit.textui.TestRunner;
19
20import com.meterware.httpunit.WebConversation;
21import com.meterware.httpunit.WebRequest;
22import com.meterware.httpunit.WebResponse;
23import com.meterware.httpunit.WebForm;
24import com.meterware.httpunit.GetMethodWebRequest;
25
26import com.asmiling.test.util.LoginTestInf;
27public class LoginTest extends TestCase implements LoginTestInf{
28        private WebConversation brower;
29        private WebRequest request;
30        private WebResponse response;
31        private String urlBlogdriver="http://192.168.22.75:8080/sms2401";
32        public void setUp() throws Exception{
33                brower=new WebConversation();
34                request=new GetMethodWebRequest(urlBlogdriver);
35                response=brower.getResponse(request);
36        }

37        //输入登录地址的页面地址,验证该页面是否被正常访问
38        public void testValidPage() throws Exception{
39                assertNotNull("Summary在网络上不存在!",response);
40        }

41        //验证被访问的页面是否是登录页面
42        public void testIsLoginPage() throws Exception{
43                URL currentUrl=response.getURL();
44                String currentUrlStr=currentUrl.getProtocol()+"://"+currentUrl.getHost()+currentUrl.getPath();
45                assertEquals("登录页面不是Summary首页!",currentUrlStr,urlBlogdriver);
46        }

47        //输入非法用户名、密码,验证登录失败
48        public void testBadLogin() throws Exception{
49                WebForm form=response.getForms()[0];
50                form.setParameter("txtUserName","smsAdmin@test.cu.com");
51                form.setParameter("txtPassword","sms@))*");
52                request=form.getRequest();
53                response=brower.getResponse(request);
54                assertTrue("您的用户名和密码在IOSMS没有备案!",response.getText().indexOf("出错信息")!=-1);
55        }

56        //输入合法用户名、密码、验证登录成功
57        public void testGoodLogin() throws Exception{
58                WebForm form=response.getForms()[0];
59                form.setParameter("txtUserName","smsAdmin@test.cu.com");
60                form.setParameter("txtPassword","sms@))*");//此处需要填写真实密码
61                request=form.getRequest();
62                response=brower.getResponse(request);
63                assertTrue("转到Summary页面失败!",response.getText().indexOf("Summary")!=-1);
64        }

65
66        public static TestSuite suite(){
67                return new TestSuite(LoginTest.class);
68        }

69
70        public static void main(String args[]){
71                TestRunner.run(suite());
72        }

73}

LoginTestInf.java
 1package com.asmiling.test.util;
 2
 3/**
 4 * <p>Title: </p>
 5 *
 6 * <p>Description: </p>
 7 *
 8 * <p>Copyright: Copyright (c) 2006</p>
 9 *
10 * <p>Company: </p>
11 *
12 * @author sybil
13 * @version 1.0
14 */

15
16public interface LoginTestInf{
17    public void testValidPage() throws Exception;
18    public void testIsLoginPage() throws Exception;
19    public void testBadLogin() throws Exception;
20    public void testGoodLogin() throws Exception;
21}

一共四个方法,通过了两个,testValidPage和testGoodLogin通过了,testInLoginPage和testBadLogin没过。

posted on 2006-12-13 18:26 SYBIL@BLOGJAVA 阅读(250) 评论(0)  编辑  收藏


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


网站导航: