如何使用测试工具进行HTTP测试。
白羽 2018-05-31 来源 :网络 阅读 1430 评论 0

摘要:本文将带你了解如何使用测试工具进行HTTP测试,希望本文对大家学测试工具有所帮助。



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


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

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

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

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

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

1)      原因短语字段

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

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


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

场景1:访问"//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( "//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( "//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                         "//hi.baidu.com/baidu",
25                         "//news.baidu.com",
26                         "//tieba.baidu.com",
27                         "//zhidao.baidu.com",
28                         "//mp3.baidu.com",
29                         "//image.baidu.com",
30                         "/search/jiqiao.html",
31                         "/gaoji/advanced.html",
32                         "//hi.baidu.com",
33                         "//www.baidu.com/more",
34                         "//utility.baidu.com/traf/click.php?id=215&url=//www.baidu.com",
35                         "//jingjia.baidu.com",
36                         "//top.baidu.com",
37                         "/home.html",
38                         "//ir.baidu.com",
39                         "//www.baidu.com/duty",
40                         "//www.miibeian.gov.cn",
41                         "//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                         "//hi.baidu.com/baidu",
25                         "//news.baidu.com",
26                         "//tieba.baidu.com",
27                         "//zhidao.baidu.com",
28                         "//mp3.baidu.com",
29                         "//image.baidu.com",
30                         "/search/jiqiao.html",
31                         "/gaoji/advanced.html",
32                         "//hi.baidu.com",
33                         "//www.baidu.com/more",
34                         "//utility.baidu.com/traf/click.php?id=215&url=//www.baidu.com",
35                         "//jingjia.baidu.com",
36                         "//top.baidu.com",
37                         "/home.html",
38                         "//ir.baidu.com",
39                         "//www.baidu.com/duty",
40                         "//www.miibeian.gov.cn",
41                         "//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 = "//www.mylogin.com";
09         //第一次访问页面
10         String url = "//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 = "//www.mylogin.com";
09         //第一次访问页面
10         String url = "//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
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程