HttpClient做接口测试时自定义参数长度

发布时间:2020-07-16 18:14:19 作者:拖鞋的畅想
来源:网络 阅读:746

在做http接口测试的时候,老是要做一大堆的参数和值,整个界面看着相当别扭,于是就把参数部分组合放到一个方法中,把执行放到一个方法中,主函数就只要获取response字符串做处理做断言就好了,这样这个代码看上去清爽多了,这里用到了httpclient、dom4j、testng的jar包。


import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.util.ArrayList;

import java.util.List;

import org.apache.http.HttpEntity;

import org.apache.http.NameValuePair;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.util.EntityUtils;

import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.DocumentHelper;

import org.dom4j.Element;

import org.testng.AssertJUnit;


public class HttpInterfaceTest {

private static String url;

private static String[][] keyVlaueList = {{"cdkey","6SDK"},{"password","160489"}}; //根据需要自定义接口参数个数及参数值

private static List<NameValuePair> postPara = new ArrayList<NameValuePair>();

private static String responseEntiy;


        /**

* 组装请求参数

* @param keyVlaueList

*/

public static List<NameValuePair> getPostPara(String[][] keyVlaueList) throws DocumentException{

for(int i = 0;i<keyVlaueList.length;i++)

{

postPara.add(new BasicNameValuePair(keyVlaueList[i][0],keyVlaueList[i][1]));

}

return postPara;

}


        

/**

* 获取接口返回字符串

* @param url

*/

public static String getResponse(String url){

try {

postPara = getPostPara(keyVlaueList);

CloseableHttpClient httpclient = HttpClients.createDefault();

HttpPost post = new HttpPost(url);

post.setEntity(new UrlEncodedFormEntity(postPara));

CloseableHttpResponse response = httpclient.execute(post);

HttpEntity entiyResponse = response.getEntity();

responseEntiy = EntityUtils.toString(entiyResponse).trim();


} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}catch (DocumentException e) {

e.printStackTrace();

}

return responseEntiy;

}

public static void main(String[] args){

url = "http://xxxxxxx/querybalance.action";

responseEntiy = getResponse(url);

//获取的字符串:responseEntiy = <?xml version="1.0" encoding="UTF-8"?><response><error>0</error><message>1442.4</message></response>

//根据接口返回的字符类型做相应的解析,自由发挥!

try {

Document document = DocumentHelper.parseText(responseEntiy);

Element rootElement = document.getRootElement();  

Element nodeElement = rootElement.element("error");

String errorString = nodeElement.getTextTrim();

   if (!errorString.equals("0")){

    AssertJUnit.fail(responseEntiy);

   }

}catch (DocumentException e) {

e.printStackTrace();

}

}

}

推荐阅读:
  1. 使用EOLINKER做接口测试最佳路径(上)
  2. httpclient请求

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

自定义 接口测试 参数长度

上一篇:Google Translate新增Phrasebook个性化常用语手册功能

下一篇:php实现简单工厂模式

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》