今天寫了個http-unit
LoginTest.java?1
package?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
?*/
15
import?java.net.URL;
16
import?junit.framework.TestCase;
17
import?junit.framework.TestSuite;
18
import?junit.textui.TestRunner;
19
20
import?com.meterware.httpunit.WebConversation;
21
import?com.meterware.httpunit.WebRequest;
22
import?com.meterware.httpunit.WebResponse;
23
import?com.meterware.httpunit.WebForm;
24
import?com.meterware.httpunit.GetMethodWebRequest;
25
26
import?com.asmiling.test.util.LoginTestInf;
27
public?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
}

?2

?3


?4

?5

?6

?7

?8

?9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27



28

29

30

31

32



33

34

35

36

37

38



39

40

41

42



43

44

45

46

47

48



49

50

51

52

53

54

55

56

57



58

59

60

61

62

63

64

65

66



67

68

69

70



71

72

73

LoginTestInf.java
?1
package?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
16
public?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
}

?2

?3


?4

?5

?6

?7

?8

?9

10

11

12

13

14

15

16



17

18

19

20

21

一共四個方法,通過了兩個,testValidPage和testGoodLogin通過了,testInLoginPage和testBadLogin沒過。
posted on 2006-12-13 18:26 SYBIL@BLOGJAVA 閱讀(255) 評論(0) 編輯 收藏