java 接口自动化测试之数据请求的简单封装
Posted 不当咸鱼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 接口自动化测试之数据请求的简单封装相关的知识,希望对你有一定的参考价值。
我们自己用java写接口自动化测试框架或者做个接口自动化测试平台的话,是需要自己进行相关的请求的,因此我们需要简单的封装下httpclient,我新建了一个http工具类,将get方法和post方法进行了一个简单的封装。
如果是开发的话,比如APP开发,无论是ios还是android,是需要将数据请求进行十分严密的封装的,因为需要对各种网络状态,请求状态做相应的判断处理,因为需要通过这些判断来做出相应的UI交互界面来给用户提示,那么我们做接口测试的话是不需要这么严密的,因为测试的前提就是要保证测试环境的稳定,我们只考虑接口在正常的访问下通过对传参的各种控制来判断接口是否做出了正确的数据传输。
前几篇已经记录下了简单的HTTPClient的get和post请求,下面是对它们进行了个简单的封装,使之更容易被我们使用
package com.httpclientget; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Map; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig.Builder; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; public class HttpUtil { //连接池 private static PoolingHttpClientConnectionManager connectionMgr; //超时时间 private static final int MAX_TIMEOUT = 7000; private static RequestConfig requestConfig; static{ //设置连接池 connectionMgr = new PoolingHttpClientConnectionManager(); //设置连接池大小 connectionMgr.setMaxTotal(100); connectionMgr.setDefaultMaxPerRoute(connectionMgr.getMaxTotal()); RequestConfig.Builder configBuilder = RequestConfig.custom(); //设置连接超时 configBuilder.setConnectTimeout(MAX_TIMEOUT); //设置读取超时 configBuilder.setSocketTimeout(MAX_TIMEOUT); //设置从连接池获取连接实例的超时 configBuilder.setConnectionRequestTimeout(MAX_TIMEOUT); requestConfig = configBuilder.build(); } /** * GET 请求 带输入参数 * @param Url 请求host地址 * @param params 参数 * @return */ public static String httpGet(String Url,Map<String, Object>params) { //返回结果 String result = null; //拼接url StringBuilder builder = new StringBuilder(Url); if (Url.contains("?")) { builder.append("&"); }else{ builder.append("?"); } int i=0; for (String key : params.keySet()) { if (i != 0 ) { builder.append("&"); } builder.append(key); builder.append("="); builder.append(params.get(key)); i++; } String apiUrl = builder.toString(); //创建client HttpClient client = HttpClients.createDefault(); try { HttpGet get = new HttpGet(apiUrl); HttpResponse response = client.execute(get); //获取请求状态码 int statusCode = response.getStatusLine().getStatusCode(); System.out.println(statusCode); HttpEntity entity = response.getEntity(); if (entity != null) { result = EntityUtils.toString(entity,"UTF-8"); } } catch (Exception e) { e.printStackTrace(); } return result; } /** * POST 请求 * @param url 请求url * @param params post提交参数 * @return */ public static String httpPost(String url,Map<String, Object>params) { HttpClient client = HttpClients.createDefault(); String result = null; try { HttpPost post = new HttpPost(url); //添加post提交参数 ArrayList<NameValuePair> pairList = new ArrayList<NameValuePair>(); for(Map.Entry<String, Object> entry:params.entrySet()) { NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue().toString()); pairList.add(pair); } post.setEntity(new UrlEncodedFormEntity(pairList,"UTF-8")); HttpResponse response = client.execute(post); //获取状态码 int statueCode = response.getStatusLine().getStatusCode(); System.out.println(statueCode); HttpEntity entity = response.getEntity(); if (entity != null) { result = EntityUtils.toString(entity); } } catch (Exception e) { e.printStackTrace(); } return result; } }
做个简单的测试:
HttpUtil util = new HttpUtil(); Map<String, Object> params = new HashMap<String, Object>(); params.put("q", "good"); params.put("salt", "1496238482428"); params.put("sign", "02E15CDAF871B698FE04EE32FD2CF155"); params.put("from", "en"); params.put("appKey", "7743eee7f7e11d75"); params.put("to", "zh-CHS"); String result = util.httpPost(urlStr, params); System.out.println(result);
输出结果:
{ "tSpeakUrl": "https://dict.youdao.com/dictvoice?audio=%E5%A5%BD&le=auto&channel=7743eee7f7e11d75&rate=4", "web": [ { "value": [ "好", "善", "商品" ], "key": "Good" }, { "value": [ "公共物品", "公益事业", "公共财" ], "key": "public good" }, { "value": [ "干的出色", "干得好", "好工作" ], "key": "Good Job" } ], "query": "good", "translation": [ "好" ], "errorCode": "0", "basic": { "us-phonetic": "ɡ?d", "phonetic": "g?d", "uk-phonetic": "g?d", "explains": [ "n. 好处;善行;慷慨的行为", "adj. 好的;优良的;愉快的;虔诚的", "adv. 好", "n. (Good)人名;(英)古德;(瑞典)戈德" ] }, "speakUrl": "https://dict.youdao.com/dictvoice?audio=good&le=auto&channel=7743eee7f7e11d75&rate=4" }
如果不需要传入参数,把第二个传入的参数置为空即可,这只是简单地封装了下,在后续要写的自动化测试框架我们可能还是要加入各种各样的判断以及日志输出,毕竟测试最重要的还是断言等这样的测试结果。
以上是关于java 接口自动化测试之数据请求的简单封装的主要内容,如果未能解决你的问题,请参考以下文章