51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1905|回复: 0
打印 上一主题 下一主题

接口测试页面

[复制链接]
  • TA的每日心情
    无聊
    昨天 09:16
  • 签到天数: 401 天

    连续签到: 2 天

    [LV.9]测试副司令

    跳转到指定楼层
    1#
    发表于 2018-12-14 16:12:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

    1:在项目中新建html页面,将代码粘贴到html文档中

    页面全部代码如下:

    1. <html>
    2.     <head>
    3.         <meta charset="utf-8" />
    4.         <title>接口测试</title>
    5.         <style>
    6.             *{padding: 0;margin: 0;}
    7.             html{background-color: #f8f8f8;font-family:"微软雅黑";font-size: 16px;color: #333333;}
    8.             ul,li{list-style: none;}
    9.             
    10.             .container{position: relative;width: 1200px; margin: 0 auto;}
    11.             .title{padding: 50px 0;text-align: center;}
    12.             .mr30{margin-right: 30px;}
    13.             .test-list{}
    14.             .test-list .list-item{border: 1px solid #ccc; padding: 20px;margin: 15px auto; border-radius: 4px;background-color: #fff;cursor:pointer;line-height:40px;}
    15.             .test-list .list-item:hover{background-color: #e8e8e8;}
    16.             .success,.failed,.error{font-size: 18px;}
    17.             .success{color: green;}
    18.             .failed{color: #FF8A5B ;}
    19.             .error{color: red;}
    20.             .info-code{display: inline-block;vertical-align: middle;margin-right: 30px;margin-bottom: 30px;}
    21.             .btn{position: fixed;z-index:10;display:block;left:50%;top:130px;margin-left: 448px;padding:20px 40px;font-size:18px;border:none;background-color:#e3393c;border-radius:4px;color:#fff;cursor:pointer;}
    22.         </style>
    23.     </head>
    24.     <body>
    25.         
    26.         <div class="container">
    27.             <h1 class="title">接口测试</h1>
    28.             <div>
    29.                 <p class="info-code">成功接口:<span id="success-count" class="success">0</span></p>
    30.                 <p class="info-code">失败接口:<span id="failed-count" class="failed">0</span></p>
    31.                 <p class="info-code">错误接口:<span id="error-count" class="error">0</span></p>
    32.             </div>
    33.             <div>
    34.                 <ul id="test-list" class="test-list"></ul>
    35.             </div>
    36.             <button id="btn" class="btn" type="button">开始检测</button>
    37.         </div>
    38.         
    39.         <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
    40.         <script>
    41.         
    42.              // 用户在此处设置测试接口内容
    43.             var checks = [
    44.                     {
    45.                             title: "敏感词检查",
    46.                             url: "/lemmas/sensitive",
    47.                             type: "post",
    48.                             data:{
    49.                                     k: "义无反顾啊啊啊",
    50.                                     sesitiveWord: []
    51.                             }
    52.                     },{
    53.                     title: "敏感词检查",
    54.                     url: "/lemmas/sensitive1",
    55.                     type: "post",
    56.                     data:{
    57.                         k: "义无反顾啊啊啊",
    58.                         sesitiveWord: []
    59.                     }
    60.                 },{
    61.                     title: "敏感词检查",
    62.                     url: "/lemmas/sensitive2",
    63.                     type: "post",
    64.                     data:{
    65.                         k: "义无反顾啊啊啊",
    66.                         sesitiveWord: []
    67.                     }
    68.                 },{
    69.                     title: "敏感词检查",
    70.                     url: "/lemmas/sensitive",
    71.                     type: "post",
    72.                     data:{
    73.                         k: "义无反顾啊啊啊",
    74.                         sesitiveWord: []
    75.                     }
    76.                 },{
    77.                     title: "敏感词检查",
    78.                     url: "/lemmas/sensitive",
    79.                     type: "post",
    80.                     data:{
    81.                         k: null,
    82.                         sesitiveWord: []
    83.                     }
    84.                 },
    85.             ];
    86.             //接口根路径
    87.             var root = '';
    88.             
    89.             
    90.             
    91.             
    92.             
    93.             
    94.             var sCount = 0, //成功数
    95.                 fCount = 0, //失败数
    96.                 eCount = 0; //错误数
    97.             //方法对象
    98.             var util = {
    99.                 /**
    100.                  *接口测试方法
    101.                  * @param title {string} 接口名称 (必填)
    102.                  * @param url {string} 接口url (必填)
    103.                  * @param data {object} 接口参数 (必填)
    104.                  * @param type {string} 接口类型 (必填)
    105.                  * @param successFun {function} 接口成功后回调 (可选)
    106.                  * @param errorFun {function} 接口失败后回调(可选)
    107.                  */
    108.                 request: function(title, url, type, data, successFun, errorFun){
    109.                         var $dom = $('#test-list');
    110.                     //成功
    111.                     var success = successFun || function(data){
    112.                         var ItemHtml = '';
    113.                         if(data.state == "100"){
    114.                                 ItemHtml = '<li class="list-item"><h3 class="success">成功</h3><p>接口名称:'+ title +'</p><p>接口url:'+ url +'</p></li>';
    115.                                 sCount++;
    116.                                 $('#success-count').text(sCount);
    117.                         }else{
    118.                             ItemHtml = '<li class="list-item"><h3 class="failed">失败</h3><p>接口名称:'+ title +'</p><p>接口url:'+ url +'</p></li>';
    119.                             fCount++;
    120.                             $('#failed-count').text(fCount);
    121.                         }
    122.                         $dom.append(ItemHtml);
    123.                         
    124.                     };
    125.                     //出错
    126.                     var error = errorFun || function(data){
    127.                         var ItemHtml = '';
    128.                         ItemHtml = '<li class="list-item"><h3 class="error">出错</h3><p>接口名称:'+ title +'</p><p>接口url:'+ url +'</p></li>';
    129.                         eCount++;
    130.                         $('#error-count').text(eCount);
    131.                         $dom.append(ItemHtml);
    132.                     };
    133.                     //调取接口
    134.                     $.ajax({
    135.                         url: root + url,
    136.                         data: data,
    137.                         type: type,
    138.                         dataType: 'json',
    139.                         contentType : "application/json;charset=utf-8",
    140.                         cache : false,
    141.                         success: success,
    142.                         error: error
    143.                     });
    144.                 },
    145.                 /**
    146.                  * post方法
    147.                  * @param title {string} 接口名称 (必填)
    148.                  * @param url {string} 接口url (必填)
    149.                  * @param data {object} 接口参数 (必填)
    150.                  * @param successFun {function} 接口成功后回调 (可选)
    151.                  * @param errorFun {function} 接口失败后回调(可选)
    152.                  */
    153.                 post: function(title, url, data, successFun, errorFun){
    154.                     util.request(title, url, 'post', JSON.stringify(data), successFun, errorFun);
    155.                 },
    156.                 /**
    157.                  * get方法
    158.                  * @param title {string} 接口名称 (必填)
    159.                  * @param url {string} 接口url (必填)
    160.                  * @param data {object} 接口参数 (必填)
    161.                  * @param successFun {function} 接口成功后回调 (可选)
    162.                  * @param errorFun {function} 接口失败后回调(可选)
    163.                  */
    164.                 get: function(title, url, data, successFun, errorFun){
    165.                     util.request(title, url, 'get', JSON.stringify(data), successFun, errorFun);
    166.                 }
    167.             };
    168.             
    169.             /* 接口检测方法 */
    170.             var startCheck = function(){
    171.                     $('#test-list').empty();
    172.                     $.each(checks, function(index, item){
    173.                          if(item.type=="post"){
    174.                                  util.post(item.title, item.url, item.data);
    175.                          }else{
    176.                                  util.get(item.title, item.url, item.data);
    177.                          }
    178.                  });
    179.             };

    180.             //检查按钮绑定点击事件
    181.             $('#btn').on('click',function(){
    182.                     //初始化接口检测结果数值
    183.                     sCount = 0; //成功数
    184.                 fCount = 0; //失败数
    185.                 eCount = 0; //错误数
    186.                
    187.                 //执行检查方法
    188.                 startCheck();
    189.                    
    190.             })

    191.         </script>
    192.     </body>
    193. </html>
    复制代码

    2:在项目中运行该html页面,(不在项目中运行,会有跨域问题)

    3:修改代码中checks变量内容,就可以批量测试接口了


    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-4-24 08:13 , Processed in 0.060672 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表