使用测试工具进行HTTP测试。

使用测试工具进行HTTP测试。

在选择什么样的测试工具之前,需要分析在实际工作工程中有哪几种请求类型需要模拟,然后根据实际测试需要选择合适的工具。在这里工具不一定是一种,可以根据实际的场景来选择,甚至几种工具组合使用。通常情况下需要模拟的消息请求场景有以下几种。

1) 最简单的请求,请求访问某个网页。

2) 通过Get方法访问页面并且加入参数。

3) 通过Post方法访问页面并且加入参数

4) 通过Post方法访问页面并且需要提交表单

同样如果对验证点进行总结,大致也有以下几个方面的内容需要进行验证。

1)      原因短语字段

2)      网页的内容(包含链接,图片)

3)      服务器处理结果验证(包含返回的Soap消息,sessionId等)

本书将主要几个测试案例来说明如何使用HTTPUNITXMLUNIT来实现以上几种消息请求的模拟和结果的验证。

场景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          //登陆页面链接有传递的api_server地址和相关参数在服务器端经过计算返回给response

26          WebForm loginForm = response.getForms()[0];

27          request = loginForm.getRequest();

29          request.setParameter("username", "Sip");

30          request.setParameter("pwd", "1");

31         

32          response = conversation.getResponse(request);

33          System.out.println(response.getText());

34          String XmlTest = response.getText();

35          if (XmlTest.indexOf("Success") >=0)

37                  assertTrue(true);

38           else

39                  assertTrue(false);

40    }

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          //登陆页面链接有传递的api_server地址和相关参数在服务器端经过计算返回给response

26          WebForm loginForm = response.getForms()[0];

27          request = loginForm.getRequest();

29          request.setParameter("username", "Sip");

30          request.setParameter("pwd", "1");

31         

32          response = conversation.getResponse(request);

33          System.out.println(response.getText());

34          String XmlTest = response.getText();

35          if (XmlTest.indexOf("Success") >=0)

37                  assertTrue(true);

38           else

39                  assertTrue(false);

40    }

 

以上两个场景都侧重描述如何模拟消息的请求,而消息的验证都使用字符串对比的方法,而实际上现在很多测试工具都提供了现成的断言方法,并且可以对结果加以分析,使得测试结果更加直观。如对Soap消息中Xml文本内容的对比方法就有XMLUNIT等工具可以实现。限于篇幅本书只引摘Xmlunit中的例子来做简要说明其方法,读者有兴趣可以做深入研究。

01    import java.io.File;

02    import java.io.FileReader;

03    import java.util.List;

04   

05    import javax.xml.transform.stream.StreamSource;

06   

07    import org.w3c.dom.Document;

08    import org.w3c.dom.Element;

09    import org.w3c.dom.Node;

10    import org.w3c.dom.Text;

11   

12    import org.custommonkey.xmlunit.*;

13    public class MyXMLTestCase extends XMLTestCase {

14        public MyXMLTestCase(String name) {

15            super(name);

16        }

17                  //判断两个xml文档是否相同

18        public void testForEquality() throws Exception {

19            String myControlXML = "<msg><uuid>0x00435A8C</uuid></msg>";

20            String myTestXML = "<msg><localId>2376</localId></msg>";

21            assertXMLEqual("comparing test xml to control xml", myControlXML, myTestXML);

22   

23            assertXMLNotEqual("test xml not similar to control xml", myControlXML, myTestXML);

24        }

25                  //例举文档中相同的地方和不同的地方

26        public void testIdentical() throws Exception {

27            String myControlXML = "<struct><int>3</int><boolean>false</boolean></struct>";

28            String myTestXML = "<struct><boolean>false</boolean><int>3</int></struct>";

29            Diff myDiff = new Diff(myControlXML, myTestXML);

30            assertTrue("pieces of XML are similar " + myDiff, myDiff.similar());

31            assertTrue("but are they identical? " + myDiff, myDiff.identical());

32        }

01    import java.io.File;

02    import java.io.FileReader;

03    import java.util.List;

04   

05    import javax.xml.transform.stream.StreamSource;

06   

07    import org.w3c.dom.Document;

08    import org.w3c.dom.Element;

09    import org.w3c.dom.Node;

10    import org.w3c.dom.Text;

11   

12    import org.custommonkey.xmlunit.*;

13    public class MyXMLTestCase extends XMLTestCase {

14        public MyXMLTestCase(String name) {

15            super(name);

16        }

17                  //判断两个xml文档是否相同

18        public void testForEquality() throws Exception {

19            String myControlXML = "<msg><uuid>0x00435A8C</uuid></msg>";

20            String myTestXML = "<msg><localId>2376</localId></msg>";

21            assertXMLEqual("comparing test xml to control xml", myControlXML, myTestXML);

22   

23            assertXMLNotEqual("test xml not similar to control xml", myControlXML, myTestXML);

24        }

25                  //例举文档中相同的地方和不同的地方

26        public void testIdentical() throws Exception {

27            String myControlXML = "<struct><int>3</int><boolean>false</boolean></struct>";

28            String myTestXML = "<struct><boolean>false</boolean><int>3</int></struct>";

29            Diff myDiff = new Diff(myControlXML, myTestXML);

30            assertTrue("pieces of XML are similar " + myDiff, myDiff.similar());

31            assertTrue("but are they identical? " + myDiff, myDiff.identical());

32        }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值