使用測試工具進行HTTP測試
歡迎訪問 中國軟件工程網 http://www.rjgc.net
在選擇什么樣的測試工具之前,需要分析在實際工作工程中有哪幾種請求類型需要模擬,然后根據實際測試需要選擇合適的工具。在這里工具不一定是一種,可以根據實際的場景來選擇,甚至幾種工具組合使用。通常情況下需要模擬的消息請求場景有以下幾種。
1) 最簡單的請求,請求訪問某個網頁。
2) 通過Get方法訪問頁面并且加入參數。
3) 通過Post方法訪問頁面并且加入參數
4) 通過Post方法訪問頁面并且需要提交表單
同樣如果對驗證點進行總結,大致也有以下幾個方面的內容需要進行驗證。
1) 原因短語字段
2) 網頁的內容(包含鏈接,圖片)
3) 服務器處理結果驗證(包含返回的Soap消息,sessionId等)
本書將主要幾個測試案例來說明如何使用HTTPUNIT和XMLUNIT來實現以上幾種消息請求的模擬和結果的驗證。
場景1:訪問"http://www.baidu.com",并且驗證相關的消息頭和檢驗網頁中的WebLink是否符合期望。代碼如下
01 import org.junit.Test; 02 import static org.junit.Assert.*; 03 import com.meterware.httpunit.WebConversation; 04 import com.meterware.httpunit.WebResponse; 05 @Test 06 public void TestCase1() throws Exception 07 { 08 System.out.println("直接獲取網頁內容:"); 09 //建立一個WebConversation實例 10 WebConversation wc = new WebConversation(); 11 //向指定的URL發出請求,獲取響應 12 WebResponse wr = wc.getResponse( "http://www.baidu.com" ); 13 //期望返回消息頭 14 String[] Heads={"CP=\" OTI DSP COR IVA OUR IND COM \"","Sat, 15 Mar 2008 09:54:00 GMT" 15 ,"BWS/1.0","BAIDUID=ACBF37FA63FA39B866C6F4C190E96845:FG=1; expires=Sat, 15-Mar-38 09:54:00 GMT; path=/; domain=.baidu.com" 16 ,"text/html","gzip","Sat, 15 Mar 2008 09:54:00 GMT","1559","private"};
01 import org.junit.Test;
02 import static org.junit.Assert.*;
03 import com.meterware.httpunit.WebConversation;
04 import com.meterware.httpunit.WebResponse;
05 @Test
06 public void TestCase1() throws Exception
07 {
08 System.out.println("直接獲取網頁內容:");
09 //建立一個WebConversation實例
10 WebConversation wc = new WebConversation();
11 //向指定的URL發出請求,獲取響應
12 WebResponse wr = wc.getResponse( "http://www.baidu.com" );
13 //期望返回消息頭
14 String[] Heads={"CP=\" OTI DSP COR IVA OUR IND COM \"","Sat, 15 Mar 2008 09:54:00 GMT"
15 ,"BWS/1.0","BAIDUID=ACBF37FA63FA39B866C6F4C190E96845:FG=1; expires=Sat, 15-Mar-38 09:54:00 GMT; path=/; domain=.baidu.com"
16 ,"text/html","gzip","Sat, 15 Mar 2008 09:54:00 GMT","1559","private"};
17 String s1 = wr.getHeaderFieldNames()[0];
18 System.out.println(wr.getHeaderField(s1));
19 assertEquals(Heads[0],wr.getHeaderField(s1));
20 s1 = wr.getHeaderFieldNames()[2];
21 assertEquals(Heads[2],wr.getHeaderField(s1));
22 //準備期望的網頁鏈接
23 String[] Explinks ={
24 "http://hi.baidu.com/baidu",
25 "http://news.baidu.com",
26 "http://tieba.baidu.com",
27 "http://zhidao.baidu.com",
28 "http://mp3.baidu.com",
29 "http://image.baidu.com",
30 "/search/jiqiao.html",
31 "/gaoji/advanced.html",
32 "http://hi.baidu.com",
33 "http://www.baidu.com/more",
34 "http://utility.baidu.com/traf/click.php?id=215&url=http://www.baidu.com",
35 "http://jingjia.baidu.com",
36 "http://top.baidu.com",
37 "/home.html",
38 "http://ir.baidu.com",
39 "http://www.baidu.com/duty",
40 "http://www.miibeian.gov.cn",
41 "http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001092500412"
42 };
43 //對網頁上所有的鏈接進行斷言
44 for(int i = 0; i <= wr.getLinks().length -1; i++)
45 {
46 assertEquals(Explinks[i],wr.getLinks()[i].getURLString());
47 System.out.println(wr.getLinks()[i].getURLString());
48 }
49 }
17 String s1 = wr.getHeaderFieldNames()[0]; 18 System.out.println(wr.getHeaderField(s1)); 19 assertEquals(Heads[0],wr.getHeaderField(s1)); 20 s1 = wr.getHeaderFieldNames()[2]; 21 assertEquals(Heads[2],wr.getHeaderField(s1)); 22 //準備期望的網頁鏈接 23 String[] Explinks ={ 24 "http://hi.baidu.com/baidu", 25 "http://news.baidu.com", 26 "http://tieba.baidu.com", 27 "http://zhidao.baidu.com", 28 "http://mp3.baidu.com", 29 "http://image.baidu.com", 30 "/search/jiqiao.html", 31 "/gaoji/advanced.html", 32 "http://hi.baidu.com", 33 "http://www.baidu.com/more", 34 "http://utility.baidu.com/traf/click.php?id=215&url=http://www.baidu.com", 35 "http://jingjia.baidu.com", 36 "http://top.baidu.com", 37 "/home.html", 38 "http://ir.baidu.com", 39 "http://www.baidu.com/duty", 40 "http://www.miibeian.gov.cn", 41 "http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001092500412" 42 }; 43 //對網頁上所有的鏈接進行斷言 44 for(int i = 0; i <= wr.getLinks().length -1; i++) 45 { 46 assertEquals(Explinks[i],wr.getLinks()[i].getURLString()); 47 System.out.println(wr.getLinks()[i].getURLString()); 48 } 49 } |
測試代碼01-04說明本用例需要的jar包。由于HTTPUNIT是由junit擴展的,讀者在編寫測試代碼的時候也可以直接從HttpUnitTest擴展,但是這樣不是特別靈活,會造成其他框架諸如XmlUnit等無法使用,所以建議還是從junit擴展,然后使用其中功能即可。
場景2:訪問www.mytest.com 跳轉到www.mylogin.com,添加參數用戶名密碼,返回到www.mytest.com。并且驗證放回的soap消息中的內容是否正確,如果成功登陸那么返回消息”Success”。測試代碼如代碼5.2
01 @Test 02 public void TestCase8() throws Exception 03 { 04 String sip_appkey = "100";//app_id 05 String sip_apiname = "ali.ali-7695-sip"; 06 String sip_appsecret = "test_secret"; 07 //跳轉到的登陸頁面 08 String api_server = "http://www.mylogin.com"; 09 //第一次訪問頁面 10 String url = "http://www.mytest.com/"; 11 //先登第一次要訪問的頁面,目的獲得測試的Post 12 WebConversation conversation = new WebConversation(); 13 WebRequest request = new PostMethodWebRequest(url); 14 WebResponse response = conversation.getResponse(request); 15 WebForm. testForm. = response.getForms()[0]; 16 request = testForm.getRequest(); 17 //添加第一頁面需要的參數 18 request.setParameter("sip_appkey", sip_appkey); 19 request.setParameter("sip_apiname", sip_apiname); 20 request.setParameter("sip_appsecret", sip_appsecret); 21 request.setParameter("api_server", api_server); 22 response = conversation.getResponse(request); 23 System.out.println(response.getText()); 24 //由于需要登陸才能訪問所以轉到登陸頁面, 25 //
導入論壇
引用鏈接
收藏
分享給好友
推薦到圈子
管理
舉報
清空Cookie -
聯系我們 -
軟件測試網 -
交流論壇 -
空間列表 -
站點存檔 -
升級自己的空間
Powered by X-Space
4.0.1 UC
© 2001-2008 Comsenz Inc.
相關閱讀:
京ICP備10010545號-5