目前的項目結構
image.png
1.準備工作
在pom.xml中添加一下
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.11.0</version>
</dependency>
<!-- POI EXCEL 文件讀寫 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-excelant</artifactId>
<version>3.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
2.還是先來看用例
元素:接口url
動作:請求方式支持get、post
參數(shù):是以json保存,現(xiàn)主要由兩個節(jié)點param節(jié)點(請求參數(shù))托酸、headers(請求頭參數(shù))
期望:請求期望的反參
image.png
3.然后然后根據(jù)用例封裝model數(shù)據(jù)(也是先讀取excel文件中的數(shù)據(jù),在依次封裝model)
4.最最最最重要的okhttp請求工具類
get請求
public String get(String url, String param) {
JSONObject paramHeaderJO = JSONObject.parseObject(param);
JSONObject paramJO = (JSONObject) paramHeaderJO.get("param");
JSONObject headerJO = (JSONObject) paramHeaderJO.get("headers");
String mergeUrl = MergeParam(url, paramJO);
okhttp3.Request.Builder requestBuilder = new Request.Builder().url(mergeUrl);
//遍歷添加header
if (null != headerJO) {
for (String key : headerJO.keySet()) {
requestBuilder.addHeader(key, headerJO.getString(key));
}
}
Request request = requestBuilder.build();
Call call = okHttpClient.newCall(request);
Response response;
// request.
try {
response = call.execute();
// System.out.println(response.code());
return response.body().string();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "異常:請求錯誤發(fā)生";
}
//拼裝geturl
public String MergeParam(String url, JSONObject param) {
// JSONObject paramJO = JSONObject.parseObject(param);
StringBuffer stringBuffer = new StringBuffer(url);
stringBuffer.append("?");
boolean isFrist = true;
for (String key : param.keySet()) {
if (!isFrist) {
stringBuffer.append("&");
} else {
isFrist = false;
}
stringBuffer.append(key);
stringBuffer.append("=");
stringBuffer.append(param.get(key));
}
url = stringBuffer.toString();
return url;
}
post請求
public String post(String url, String param) {
JSONObject paramHeaderJO = JSONObject.parseObject(param);
JSONObject paramJO = (JSONObject) paramHeaderJO.get("param");
JSONObject headerJO = (JSONObject) paramHeaderJO.get("headers");
Builder formBuilder = new FormBody.Builder();
//遍歷添加請求參數(shù)
if (null != paramJO) {
for (String key : paramJO.keySet()) {
formBuilder.add(key, paramJO.getString(key));
}
}
FormBody formBody = formBuilder.build();
// RequestBody body = RequestBody.create(type, param);
// System.out.println(param);
okhttp3.Request.Builder requestBuilder = new Request.Builder().url(url).post(formBody);
//遍歷添加header參數(shù)
if (null != headerJO) {
for (String key : headerJO.keySet()) {
requestBuilder.addHeader(key, headerJO.getString(key));
}
}
// Headers headers = new Headers(null);
Request request = requestBuilder.build();
Response response;
Call call = okHttpClient.newCall(request);
try {
response = call.execute();
response.headers();
System.out.println(response.headers());
return response.body().string();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.err.println();
return "請求錯誤aaa";
}
}
5.然后是APITest(基本與之前的webtest差不多柒巫,只是修改了執(zhí)行的方法)
package com.testng.testng;
import org.testng.annotations.Test;
import TestAutomationZ.apiCase.APICaseModel;
import TestAutomationZ.apiCase.APIStepModel;
import TestAutomationZ.okHttp.OkHttpUtil;
import static org.testng.Assert.assertEquals;
import java.util.List;
import org.testng.annotations.DataProvider;
public class APITest {
List<APICaseModel> caseList;
OkHttpUtil okHttpUtil;
public APITest(List<APICaseModel> caseLiset) {
// TODO Auto-generated constructor stub
okHttpUtil = new OkHttpUtil();
this.caseList = caseLiset;
}
@Test(dataProvider = "dp")
public void f(APICaseModel caseModel) {
doCase(caseModel);
}
// 根據(jù)case模型執(zhí)行用例
public void doCase(APICaseModel caseModel) {
for (int i = 0; i < caseModel.getStepModels().size(); i++) {
APIStepModel apiStepModel = caseModel.getStepModels().get(i);
if (apiStepModel.getAction().equals("get")) {
//執(zhí)行get請求
String actual = okHttpUtil.get(apiStepModel.getUrl(), apiStepModel.getValue());
assertEquals(actual, apiStepModel.getExpect());
} else if (apiStepModel.getAction().equals("post")) {
//執(zhí)行post請求
String actual = okHttpUtil.post(apiStepModel.getUrl(), apiStepModel.getValue());
assertEquals(actual, apiStepModel.getExpect());
} else {
System.err.println("haimeixie");
}
}
}
@DataProvider
public Object[] dp() {
// caseList = APICaseModel.getCaseList("caseexcel/apicase/APIExample.xlsx");
// 獲取用例
int size = caseList.size();
Object[] objects = new Object[size];
for (int i = 0; i < objects.length; i++) {
objects[i] = caseList.get(i);
}
return objects;
}
}
6.在Test工廠類中添加相應的構建APItest的動作
package com.testng.testng;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
//import org.testng.TestNG;
import org.testng.annotations.Factory;
import TestAutomationZ.Case.CaseModel;
import TestAutomationZ.apiCase.APICaseModel;
public class TestFactory {
@Factory
public Object[] createInstances() {
List<String> webPathList = getWebCasePathList();
List<String> APIPathList = getAPICasePathList();
int resultSize = webPathList.size() + APIPathList.size();
Object[] result = new Object[resultSize];
//生成webTest
for (int i = 0; i < webPathList.size(); i++) {
// CaseModel.getCaseList(pathList.get(i), CaseType.publicCase);
result[i] = new WebTest(CaseModel.getCaseList(webPathList.get(i)));
}
//生成APITest
for (int i = 0, j = webPathList.size(); j < result.length; i++, j++) {
result[j] = new APITest(APICaseModel.getCaseList(APIPathList.get(i)));
}
return result;
}
// 獲取web測試用例
static public ArrayList<String> getWebCasePathList() {
ArrayList<String> files = new ArrayList<String>();
File file = new File("caseexcel/webcase/");
File[] tempList = file.listFiles();
if (tempList.length > 0) {
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
System.out.println("文 件:" + tempList[i]);
files.add(tempList[i].toString());
}
if (tempList[i].isDirectory()) {
// System.out.println("文件夾:" + tempList[i]);
}
}
} else {
throw new NullPointerException("沒有找到case");
}
return files;
}
// 獲取接口測試用例
static public ArrayList<String> getAPICasePathList() {
ArrayList<String> files = new ArrayList<String>();
File file = new File("caseexcel/apicase/");
File[] tempList = file.listFiles();
if (tempList.length > 0) {
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
System.out.println("文 件:" + tempList[i]);
files.add(tempList[i].toString());
}
if (tempList[i].isDirectory()) {
// System.out.println("文件夾:" + tempList[i]);
}
}
} else {
throw new NullPointerException("沒有找到case");
}
return files;
}
}