51Testing软件测试论坛

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

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 3033|回复: 6
打印 上一主题 下一主题

[原创] 接口测试相关问题(java+testng+selenium)

[复制链接]
  • TA的每日心情
    奋斗
    2017-9-19 09:36
  • 签到天数: 5 天

    连续签到: 1 天

    [LV.2]测试排长

    跳转到指定楼层
    1#
    发表于 2017-9-14 11:11:49 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    自动化新人一枚,最近在研究接口自动化,使用的是java+testng+selenium框架,请求是post请求,消息提示json格式的,目前请求拼出来了,但是不知道怎么把请求发送到url,然后去得到响应,上网查了一些资料,有很多是说用HttpURLConnection和httpclient模拟post请求,但是这两个我都不太懂,有没有大神能够指导一下?多谢!!!目前把请求拼出来如下:后面不会操作了
    JSONObject object = new JSONObject();
                       ReqestHead reqHead = new ReqestHead();
                    reqHead.setChannelId("ZT");
                    reqHead.setRequestTime(DateUtil.getCurrentTime());
                    reqHead.setAreaCode("025");
                    reqHead.setTranId(DateUtil.getNowDate(DateUtil.FORMAT_1)+"123456");
                    reqHead.setAuth(MD5Util.encryptByMD5(reqHead.getTranId()+"888888"));          
                              
                    JSONObject body = new JSONObject();
                    body.put("goodsNbr", "test20150623000926");
                    body.put("qrGoodsType", "0");
                    
                    object.put("head", reqHead);
                    object.put("body", body);
                   String requestion=object.toJSONString();
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏1
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    2018-8-27 15:56
  • 签到天数: 322 天

    连续签到: 1 天

    [LV.8]测试军长

    2#
    发表于 2017-9-14 13:41:43 | 只看该作者
    1.         /**
    2.          * @Description:使用HttpURLConnection发送post请求
    3.          */
    4.         public static String sendHttpURLPost(String urlParam, Map<String, Object> params, String charset, int timeout) {
    5.                 StringBuffer resultBuffer = null;
    6.                 // 构建请求参数
    7.                 StringBuffer sbParams = new StringBuffer();
    8.                 if (params != null && params.size() > 0) {
    9.                         for (Entry<String, Object> e : params.entrySet()) {
    10.                                 sbParams.append(e.getKey());
    11.                                 sbParams.append("=");
    12.                                 sbParams.append(e.getValue());
    13.                                 sbParams.append("&");
    14.                         }
    15.                 }
    16.                 HttpURLConnection con = null;
    17.                 OutputStreamWriter osw = null;
    18.                 BufferedReader br = null;
    19.                 // 发送请求
    20.                 try {
    21.                         URL url = new URL(urlParam);
    22.                         con = (HttpURLConnection) url.openConnection();
    23.                         con.setRequestMethod("POST");
    24.                         con.setDoOutput(true);
    25.                         con.setDoInput(true);
    26.                         con.setUseCaches(false);
    27.                         con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    28.                         con.setConnectTimeout(timeout*1000);
    29.                         if (sbParams != null && sbParams.length() > 0) {
    30.                                 osw = new OutputStreamWriter(con.getOutputStream(), charset);
    31.                                 osw.write(sbParams.substring(0, sbParams.length() - 1));
    32.                                 osw.flush();
    33.                         }
    34.                         // 读取返回内容
    35.                         resultBuffer = new StringBuffer();
    36.                         int contentLength = Integer.parseInt(con.getHeaderField("Content-Length"));
    37.                         if (contentLength > 0) {
    38.                                 br = new BufferedReader(new InputStreamReader(con.getInputStream(), charset));
    39.                                 String temp;
    40.                                 while ((temp = br.readLine()) != null) {
    41.                                         resultBuffer.append(temp);
    42.                                 }
    43.                         }
    44.                 } catch (Exception e) {
    45.                         throw new RuntimeException(e);
    46.                 } finally {
    47.                         if (osw != null) {
    48.                                 try {
    49.                                         osw.close();
    50.                                 } catch (IOException e) {
    51.                                         osw = null;
    52.                                         throw new RuntimeException(e);
    53.                                 } finally {
    54.                                         if (con != null) {
    55.                                                 con.disconnect();
    56.                                                 con = null;
    57.                                         }
    58.                                 }
    59.                         }
    60.                         if (br != null) {
    61.                                 try {
    62.                                         br.close();
    63.                                 } catch (IOException e) {
    64.                                         br = null;
    65.                                         throw new RuntimeException(e);
    66.                                 } finally {
    67.                                         if (con != null) {
    68.                                                 con.disconnect();
    69.                                                 con = null;
    70.                                         }
    71.                                 }
    72.                         }
    73.                 }

    74.                 return resultBuffer.toString();
    75.         }
    复制代码

    评分

    参与人数 1测试积点 +10 收起 理由
    lsekfe + 10 奖励原因:乐于助人

    查看全部评分

    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2018-8-27 15:56
  • 签到天数: 322 天

    连续签到: 1 天

    [LV.8]测试军长

    3#
    发表于 2017-9-14 13:42:27 | 只看该作者
    1. /**
    2.          * @Description:使用HttpClient发送post请求
    3.          */
    4.         public static String httpClientPost(String urlParam, Map<String, Object> params, String charset) {
    5.                 StringBuffer resultBuffer = null;
    6.                 HttpClient client = new DefaultHttpClient();
    7.                 HttpPost httpPost = new HttpPost(urlParam);
    8.                 // 构建请求参数
    9.                 List<NameValuePair> list = new ArrayList<NameValuePair>();
    10.                 Iterator<Entry<String, Object>> iterator = params.entrySet().iterator();
    11.                 while (iterator.hasNext()) {
    12.                         Entry<String, Object> elem = iterator.next();
    13.                         list.add(new BasicNameValuePair(elem.getKey(), String.valueOf(elem.getValue())));
    14.                 }
    15.                 BufferedReader br = null;
    16.                 try {
    17.                         if (list.size() > 0) {
    18.                                 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset);
    19.                                 httpPost.setEntity(entity);
    20.                         }
    21.                         HttpResponse response = client.execute(httpPost);
    22.                         // 读取服务器响应数据
    23.                         resultBuffer = new StringBuffer();

    24.                         //br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    25.                         br = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), charset));
    26.                         String temp;
    27.                         while ((temp = br.readLine()) != null) {
    28.                                 resultBuffer.append(temp);
    29.                         }
    30.                 } catch (Exception e) {
    31.                         throw new RuntimeException(e);
    32.                 } finally {
    33.                         if (br != null) {
    34.                                 try {
    35.                                         br.close();
    36.                                 } catch (IOException e) {
    37.                                         br = null;
    38.                                         throw new RuntimeException(e);
    39.                                 }
    40.                         }
    41.                 }
    42.                
    43.                 return resultBuffer.toString();
    44.         }
    复制代码
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2017-9-19 09:36
  • 签到天数: 5 天

    连续签到: 1 天

    [LV.2]测试排长

    4#
     楼主| 发表于 2017-9-14 15:21:49 | 只看该作者

    多谢大神!能请教一下:Map<String, Object> params, String charset  这两个参数是什么吗?Map<String, Object> params是请求吗?
                    object.put("head", reqHead);
                    object.put("body", body);
           String requestion=object.toJSONString();
                   String url="http://192.168.189.134:10000/centrality-client/usc/goodsDetailProvider/queryGoodsDetail";
                   
                   ReqPost.sendHttpURLPost(url, requestion, charset, 2);
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2018-8-27 15:56
  • 签到天数: 322 天

    连续签到: 1 天

    [LV.8]测试军长

    5#
    发表于 2017-9-15 10:15:07 | 只看该作者
    菜鸟小丸子 发表于 2017-9-14 15:21
    多谢大神!能请教一下:Map params, String charset  这两个参数是什么吗?Map params是请求吗?
                  ...

    是请求参数跟编码格式
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2017-9-19 09:36
  • 签到天数: 5 天

    连续签到: 1 天

    [LV.2]测试排长

    6#
     楼主| 发表于 2017-9-15 15:40:57 | 只看该作者
    seagull1985 发表于 2017-9-15 10:15
    是请求参数跟编码格式

    多谢!!!!
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

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

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

    GMT+8, 2024-5-5 07:51 , Processed in 0.071087 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

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