1.通过httpclient封装 发送请求的类
package com.test.httprequest.v2;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
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 java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* @version: V1.0
* @author:
* @className: HttpUtil
* @packageName: com.zokoo.httprequest.v2
* @description: 将V1下的post请求和get 请求优化,封装成工具类
* @data:
**/
public class HttpUtil {
/**
* @Author yang sp
* @Description post 请求
* @Date 15:23 2020/3/1
* @Param [url, hashMap]
* @return java.lang.String
**/
public static String postRequest(String url, HashMap<String,String> hashMap){
//设置接口地址(作为参数传入)
//2.设置请求方式
HttpPost httpPost=new HttpPost(url);
//3.设置测试数据(封装成为map,作为参数传入)
String result="";//作用域
try {
//4.将参数封装至请求体,setEntity()的参数为HttpEntity entity对象,所以通过构造方法新建对象
//构造方法的传入参数为List<?extend NameValuePair>
List<BasicNameValuePair> parameter =new ArrayList<BasicNameValuePair>();
//通过循环将参数一个个取出
for (String key: hashMap.keySet()) {
parameter.add(new BasicNameValuePair(key, (String) hashMap.get(key)));
}
//将参数封装至请求体
httpPost.setEntity(new UrlEncodedFormEntity(parameter,"UTF-8"));
//5.设置客户端
CloseableHttpClient httpClient = HttpClients.createDefault();
//6.发送请求
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
//7.从响应结果取值
int statusCode= httpResponse.getStatusLine().getStatusCode();
// System.out.println(statusCode);
result= EntityUtils.toString(httpResponse.getEntity());
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* @Author yang sp
* @Description GET 请求
* @Date 20:53 2020/3/15
* @Param [url, hashMap]
* @return java.lang.String
**/
public static String getRequest(String url, HashMap<String,String> hashMap){
String result="";//作用域,先声明
//1.设置接口地址(作为参数传入)
//2.设置测试数据(作为参数传入)将参数从map取值出来,拼接在URL后面
for (String key:hashMap.keySet()) {
int mark=1;
if(mark==1){
url+=("?"+key+"="+hashMap.get(key));
}else {
url+=("&"+key+"="+hashMap.get(key));
}
mark++;
}
//3.设置请求方法(get请求是将参数拼接在接口地址后面)
HttpGet httpGet=new HttpGet(url);
//4.创建客户端
CloseableHttpClient httpClient= HttpClients.createDefault();
try {//5.发送请求
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
//6.从响应结果获取值
int statusCode = httpResponse.getStatusLine().getStatusCode();
// System.out.println(statusCode);
result = EntityUtils.toString(httpResponse.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public static String doService(String url, String requestMode,HashMap<String,String> hashMap){
String result=null;
if (requestMode.equals("post")){
result=postRequest(url,hashMap);
}else {
result=getRequest(url,hashMap);
}
return result;
}
}
2.使用poi数据驱动从excel 读取数据
(1)创建Case的类,从将excel读取的数据封装至该类中,这样才不会每次的去解析excel
ps:case的成员变量:url method parameter expectedResponseData actualResponseData
也可以将url method 信息分开 放在restful的类中
(2)解析excel,通过反射将excel的解析到Case类中
public static <T> void loadV2(String path, String sheetName, Class<T> tClass) {
//为封装对象,获取字节码
// Class<Case> caseClass = Case.class;无需再创建字节码
//作用域
Object object = null;
try {
//获取workbook
Workbook workbook = WorkbookFactory.create(new File(path));
//获取sheet
Sheet workbookSheet = workbook.getSheet(sheetName);
//以下为获取excel标题名称
//获取第一行
Row row = workbookSheet.getRow(0);
//获取总的列数
int lastCellNum = row.getLastCellNum();
//声明一数组
Object[] title = new Object[lastCellNum];
for (int i = 0; i < lastCellNum; i++) {
Cell cell = row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
//将cell格式化输出
String stringCellValue = new DataFormatter().formatCellValue(cell);
if (stringCellValue.contains("(")) {
String stringCellValueSubstring = stringCellValue.substring(0, stringCellValue.indexOf("("));
title[i] = stringCellValueSubstring;
} else {
title[i] = stringCellValue;
}
}
//以下获取表格中的测试数据
//获取总行数
int lastRowNum = workbookSheet.getLastRowNum();
for (int i = 0; i < lastRowNum; i++) {
//获取当前行
Row row1 = workbookSheet.getRow(i + 1);
//判断行内容和列是否为空
if (row1 == null || isEmptyRow(row1)) {
continue;
}
object = tClass.newInstance();
for (int j = 0; j < lastCellNum; j++) {
Cell cell = row1.getCell(j);
String cellValue = new DataFormatter().formatCellValue(cell);
//以上获取到表标题以及测试数据,可通过反射封装至case对象
//set+标题名称,获取要反射的方法名,调用getMethod()获取要反射的方法对象
Method method = tClass.getMethod("set" + title[j], String.class);
//通过invoke完成方法的反射(对象,值)
method.invoke(object, cellValue);
}
//若是excelutil.load的接口返回返回Arraylist<Case>,接口的复用性不高,因此该方法最好返回空,将cs放在CaseUtil.allCaseList的
if (tClass == Case.class) {
Case object1 = (Case) object;
CaseUtil.allCaseList.add(object1);
} else if (tClass == Rest.class) {
Rest object1 = (Rest) object;
RestfulUtil.arrayList.add(object1);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}