接口功能自动化测试问题汇总
作者:韩延玲 发布时间:[ 2017/3/6 13:27:07 ] 推荐标签:自动化测试 接口测试
问题一:使用httpClient实现NameValuePair方式的post请求,出现错误“响应状态:HTTP/1.1 400 Bad Request”,打印response的内容,发现参数未传入。
原因:发送的请求缺少头信息。
解决方法:加入如下的头信息
HttpPost httpPost = new HttpPost(path);
httpPost.addHeader("Accept","text/javascript, text/html, application/xml, text/xml");
httpPost.addHeader("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");
httpPost.addHeader("Accept-Encoding", "gzip,deflate,sdch");
httpPost.addHeader("Connection", "Keep-Alive");
httpPost.addHeader("Cache-Control", "no-cache");
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
问题二:接口返回内容中带有html格式的数据,如下内容,需要获取result中的data-coid的数据进行校验。
{"msg":"succeed","result":{"collectHtml":" <div class="collect-item f-fl" data-coid="33628">
<a href="/detail/favorite/33628" target="_blank">
<div class="collect-wrap f-fl">
<div class="collect-title f-toe">
<span>API: Edit G list</span>
</div>
<div class="collect-image">
<img class="collect-i" src="
http://acs.nosdn.127.net/15e4c8419e994e74a60186f8969b266c.jpeg?imageView&type=png&enlarge=1&quality=100&axis=0&thumbnail=190y190
">
<div class="collect-number">1</div>
</div>
<div class="collect-guanzhu">
<img src="/src/image/homePage/guanzhu.jpg?7f14d069d28438c6b7feeab00c2a9448">
<span>0人关注</span>
</div>
</div>
</a>
</div>
","isAll":true},"code":200}
解决方法:
1. 使用elments
以上数据为http请求的response内容,为json格式,因此将其返回给JSONObject response;
使用JSONObject result = response.getJSONObject("result");获取到result的内容;
String collectHtml = result.getString("collectHtml");获取到collectHtml内容;
使用Jsoup对collectHtml进行分析:
Document doc = Jsoup.parse(collectHtml, "UTF-8");
Elements g_lists = doc.getElementsByClass("collect-item");
String g_list_id = "";
for (Element g_list:g_lists){
g_list_id = g_list.attributes().get("data-coid").trim();
logger.info(g_list_id);
}
}
2. 使用select
doc = Jsoup.parse(response, "UTF-8");
setting_nick = doc.select("div.username").text().trim();
logger.info(setting_nick);

sales@spasvo.com