首页 > 代码库 > httpclient + TestNG 接口自动测试 第二章
httpclient + TestNG 接口自动测试 第二章
请求地址由参数加参数签名形式生成,例如:
http://ip/server?method=getPlans&planDate=2014-08-25&cinemaId=101&uid=remote&enc=d0fe8420c641dd87d4165c09fe1d0c70&time_stamp=1408960607250
1.构建url
1)首先构建参数对
public static HashMap<String, String> cinemas() { HashMap<String, String> params = new HashMap<String, String>(); params.put("method", "getCinemas"); params.put("uid", StartTest.USERNAME); return params; }
2)生成包含参数签名的参数对
public static List<NameValuePair> getFormparams (HashMap<String, String> params) { List<NameValuePair> formparams = new ArrayList<NameValuePair>(); for (Entry<String, String> e : params.entrySet()) { formparams.add(new BasicNameValuePair(e.getKey(), e.getValue())); } formparams.add(new BasicNameValuePair("time_stamp", String.valueOf(new Date().getTime()))); formparams.add(new BasicNameValuePair("enc", GetENC.getEnc(formparams, StartTest.PASSWORD))); return formparams; }
3)生成请求URL
public static URI getUrl(List<NameValuePair> formparams, String HOST, String PATH) { URI uri = null; try { uri = new URIBuilder().setScheme("http").setHost(HOST) .setPath(PATH).setParameters(formparams).build(); } catch (URISyntaxException e) { e.printStackTrace(); } return uri; }
2.构建完URL后发送请求
public static HttpEntity getEntity(HashMap<String, String> params, String HOST, String PATH) { HttpEntity entity = null; CloseableHttpClient httpclient = HttpClients.createDefault(); try { // 创建httpget请求. HttpGet httpget = new HttpGet(getUrl(getFormparams(params), HOST, PATH)); // 执行get请求 CloseableHttpResponse response = httpclient.execute(httpget); try { // 将响应放入entity载体 entity = response.getEntity(); if (entity != null) { entity = new BufferedHttpEntity(entity); if(StartTest.log) { ResultsLog.writefile(EntityUtils.toString(entity, "UTF-8"), StartTest.path); } } } finally { response.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 断开连接 try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return entity; }
3.解析目标JSON数据,返回目标数组
public static String[] targetArray(String sourcejson, String targetfield) { String targetArray[] = null; JSONObject js = JSONObject.fromObject(sourcejson); if (sourcejson.contains("data")) { JSONArray datajsonArray = js.getJSONArray("data"); targetArray = new String[datajsonArray.size()]; for(int i=0; i<datajsonArray.size(); i++){ targetArray[i] = (JSONObject.fromObject(datajsonArray.getString(i))).getString(targetfield); } } else { targetArray[0] = js.getString("errCode") + "----" + js.getString("errMsg"); } return targetArray;}
4.执行请求URL并解析返回JSON数据,获取目标值构成数组
public static String[] targetValueArray(HashMap<String, String> params, String targetfield) { String targetarray[] = null; HttpEntity entity = GetPost.getEntity(params, StartTest.ROUND_HOST, StartTest.ROUND_PATH); try { targetarray = JSONParsing.targetArray( EntityUtils.toString(entity, "UTF-8"), targetfield); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return targetarray; }
至此从构建URL-->发送get请求-->解析JSON数据获取目标值已完成
httpclient + TestNG 接口自动测试 第二章
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。