Commit 9164b834 by 左磊磊

add:RAYS7服务接口自动化代码

parent 9cb0e631
......@@ -19,6 +19,7 @@ import org.testng.annotations.BeforeSuite;
import java.io.FileInputStream;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
......@@ -108,16 +109,26 @@ public class BaseCase {
return flag;
}
public boolean assertSql(String expectValue, String beforeSqlResult, String afterSqlResult) {
boolean flag = false;
if (beforeSqlResult == null && Objects.equals(expectValue, afterSqlResult)) {
flag = true;
}
return flag;
}
/**
* 适用于非Excel接口和用例类型的调用。
*
* @param expectValue
* @param body
* @return
*/
@Step("响应结果断言")
public Boolean assertRespon(String expectValue,String body){
public Boolean assertRespon(String expectValue, String body) {
// 定义断言是否成功,默认失败
boolean flag=false;
boolean flag = false;
//
Object jsonObj = JSONObject.parse(expectValue);
// 如果Case中expectValue是数组类型的JSON格式,那么采用多字段匹配断言逻辑。
......
......@@ -27,7 +27,7 @@ public class PayCase extends BaseCase {
@Test(dataProvider = "datas0")
@Description("获取商户号支付信息")
public void testPay(int id, String casename, String url, String expectValue) throws Exception {
String cookie = "checkUserInfo=userId=84; userInfo=officialAccountsId=997&channelId=1362&wechatUserId=182833248&hasSnapsis=0&userType=REGULAR&bookId=12567110&adviserId=1362&sceneId=3930381&sourceType=QRCODE&spreadType=0";
// String cookie = "checkUserInfo=userId=84; userInfo=officialAccountsId=997&channelId=1362&wechatUserId=182833248&hasSnapsis=0&userType=REGULAR&bookId=12567110&adviserId=1362&sceneId=3930381&sourceType=QRCODE&spreadType=0";
// 创建get请求
HttpGet get = new HttpGet(url);
// 传参为JSON类型
......
package com.lemon.cases;
import io.qameta.allure.Description;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.util.Arrays;
public class Rays7ServerCase extends BaseCase {
@Test(dataProvider = "datas0")
@Description("获取商户号支付信息")
public void testManager(int id, String casename, String url, String json,String expectValue) throws Exception {
// String token = "430f837c00f9978dffec9d4c0c05c751";
// 创建post请求
HttpPost post= new HttpPost(url);
post.setHeader("Content-Type", "application/json");
post.setEntity(new StringEntity(json));
DefaultHttpClient client = new DefaultHttpClient();
// 执行get请求,并使用response来存放执行后返回的响应内容
HttpResponse response = client.execute(post);
Header[] header = response.getAllHeaders(); // 响应头
System.out.println("请求链接:"+url);
System.out.println("请求参数:"+json);
System.out.print("id:" + id + "\n"+ "casename:" + casename+ "\n");
System.out.print("响应头信息:::" + Arrays.toString(header) + "\n");
int status = response.getStatusLine().getStatusCode();
System.out.print("状态码:::" + status + "\n");
HttpEntity entity = response.getEntity(); // 响应体
String body = EntityUtils.toString(entity);
System.out.print("预期结果: " + expectValue+ "\n");
System.out.print("响应结果: " + body+ "\n");
Boolean assertResponseFlag = assertRespon(expectValue, body);
String assertContent = (assertResponseFlag) ? "Pass" : "Fail";
System.out.print("assertContent:"+assertContent);
Assert.assertEquals(assertContent, "Pass");
}
@DataProvider(name = "datas0")
public Object[][] datas0() {
//Object[m][n] m代表执行次数,n代表参数个数
Object[][] datas = {
{0, "", "http://192.168.8.40:8270/aibrain/v1.0/digitalRange/testManager", "{\"event\":\"SDK_MSG_REPORT\",\"origin\":\"WECHAT_OFFICIAL\",\"wxSdkMsgDTO\":{\"content\":{\"textMsgDTO\":{\"content\":\"泛悦城\"}},\"contentType\":\"Text\",\"digitalPersonId\":1,\"msgId\":\"24123340480981103\",\"msgRemoteId\":\"24123340480981103\",\"sdkCode\":\"66059\",\"senderSdkCode\":\"o9vkPwnl1Nd4JdBDSpcujb4d4JsU\",\"userId\":182833195}}","{\"errCode\":0,\"message\":\"操作成功!\"}"}, // RAYS跳RAYS
};
return datas;
}
}
package com.lemon.cases;
import com.alibaba.fastjson.JSONPath;
import com.lemon.constants.Constants;
import com.lemon.pojo.API;
import com.lemon.pojo.Case;
import com.lemon.utils.AuthorizationUtils;
import com.lemon.utils.ExcelUtils;
import com.lemon.utils.SqlUtils;
import io.qameta.allure.Description;
import io.qameta.allure.Step;
import org.apache.commons.lang3.StringUtils;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.Assert;
import java.math.BigDecimal;
import java.text.DecimalFormat;
public class ServerCase extends BaseCase {
@Test(dataProvider = "data1", description = "数字人推送消息") // DTO数据传输
@Description("数字人推送消息")
public void testManager(API api, Case cas) throws Exception {
//1、参数化替换
String params = replace(cas.getParams());
String sql = replace(cas.getCheckSQL());
String Bsql = replace(cas.getCheckBSQL());
cas.setParams(params);
cas.setCheckSQL(sql);
cas.setCheckBSQL(Bsql);
//2、数据库前置查询结果(断言必须在接口执行前后都查询)
Object beforeASqlResult = SqlUtils.querySingle(cas.getCheckSQL());
Object beforeBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
//3、调用接口
String body = call(api, cas, false);
// 3.1 将登录的token存储(仅限于登录接口特有)
AuthorizationUtils.storeToken(body);
// System.out.println(AuthorizationUtils.env);
//4、断言响应结果,Excel中预期响应数据与实际响应数据进行对比
boolean assertResponseFlag = assertResponse(cas, body);
//5、添加接口响应回写excel内容
addWBD(Integer.parseInt(cas.getId()), Constants.ACTURL_WRITER_BACK_CELL_NUM, body);
//6、数据库后置查询结果
Object afterASqlResult = SqlUtils.querySingle(cas.getCheckSQL());
Object afterBSqlResult = SqlUtils.querySingle(cas.getCheckBSQL());
//7-1、A段数据库断言
boolean sqlAFlag = assertSql(cas.getaExpectValue(), beforeASqlResult == null ? null : beforeASqlResult.toString(), afterASqlResult == null ? null : afterASqlResult.toString());
String assertSqlAFlag = (sqlAFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.A_ASSERT_RESULT_CELL_NUM, assertSqlAFlag);
System.out.println("数据库A-sql断言结果:" + sqlAFlag);
//7-2、B段数据库断言
boolean sqlBFlag = assertSql(cas.getbExpectValue(), beforeBSqlResult == null ? null : beforeBSqlResult.toString(), afterBSqlResult == null ? null : afterBSqlResult.toString());
String assertSqlBFlag = (sqlAFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.B_ASSERT_RESULT_CELL_NUM, assertSqlBFlag);
System.out.println("数据库B-sql断言结果:" + sqlBFlag);
//8、添加断言回写内容
String assertContent = (assertResponseFlag && sqlAFlag && sqlBFlag) ? "Pass" : "Fail";
addWBD(Integer.parseInt(cas.getId()), Constants.ASSERT_RESULT_CELL_NUM, assertContent);
//9、添加日志
//10、报表断言
Assert.assertEquals(assertContent, "Pass");
}
// 使用Excel文件传输接口信息和请求信息
@DataProvider(name = "data1")
public Object[][] data1() {
Object[][] data = ExcelUtils.getAPIAndCaseByApiId("1"); // 传参apiID
return data;
}
}
......@@ -17,7 +17,7 @@ public class Constants {
*/
//excel用例路径
public static final String EXCEL_PATH = "src/test/resources/睿思选题.xls";
public static final String EXCEL_PATH = "src/test/resources/RAYS7.xls";
// token鉴权版本
// public static final String HEADER_MEDIA_TYPE_NAME = "X-Requested-With"; // X-Lemonban-Media-Type
// token鉴权方式值
......@@ -32,19 +32,22 @@ public class Constants {
// Excel回写的列数:判断回写结果列
public static final int ASSERT_RESULT_CELL_NUM = 6;
// Excel回写的列数:A段sql是否验证通过
public static final int A_ASSERT_RESULT_CELL_NUM = 9;
// "jdbc:mysql://api.lemonban.com:3306/futureloan?useUnicode=true&characterEncoding=utf-8"; future / 123456
// Excel回写的列数:B段sql是否验证通过
public static final int B_ASSERT_RESULT_CELL_NUM = 12;
// "jdbc:mysql://api.lemonban.com:3306/futureloan?useUnicode=true&characterEncoding=utf-8"; future / 123456
// "jdbc:mysql://192.168.92.42:3306/myuser?useUnicode=true&characterEncoding=utf-8"; rays110 whLg2016.
// UAT环境RAYS数据库连接信息
public static final String JDBC_URL = "jdbc:mysql://192.168.92.42:3306/myuser?useUnicode=true&characterEncoding=utf-8";
// RAYS7服务线上数据库查询权限
public static final String JDBC_URL = "jdbc:mysql://192.168.8.48:3306/aibrain?useUnicode=true&characterEncoding=utf-8";
//数据库用户名
public static final String JDBC_USERNAME = "rays110";
public static final String JDBC_USERNAME = "aibrain110";
//数据库密码
public static final String JDBC_PASSWORD = "whLg2016.";
public static final String JDBC_PASSWORD = "C1euBfvS";
}
......@@ -20,19 +20,28 @@ public class Case {
private String apiID;
@Excel(name = "期望响应数据")
private String expectValue;
@Excel(name = "校验SQL")
@Excel(name = "A段校验SQL")
private String checkSQL;
@Excel(name = "A段预期结果")
private String aExpectValue;
@Excel(name = "B段校验SQL")
private String checkBSQL;
@Excel(name = "B段预期结果")
private String bExpectValue;
public Case() {
}
public Case(String id, String desc, String params, String apiID, String expectValue, String checkSQL) {
public Case(String id, String desc, String params, String apiID, String expectValue, String checkSQL, String aExpectValue, String checkBSQL, String bExpectValue) {
this.id = id;
this.desc = desc;
this.params = params;
this.apiID = apiID;
this.expectValue = expectValue;
this.checkSQL = checkSQL;
this.aExpectValue = aExpectValue;
this.checkBSQL = checkBSQL;
this.bExpectValue = bExpectValue;
}
public String getId() {
......@@ -59,6 +68,18 @@ public class Case {
return checkSQL;
}
public String getaExpectValue() {
return aExpectValue;
}
public String getCheckBSQL() {
return checkBSQL;
}
public String getbExpectValue() {
return bExpectValue;
}
public void setId(String id) {
this.id = id;
}
......@@ -83,6 +104,18 @@ public class Case {
this.checkSQL = checkSQL;
}
public void setaExpectValue(String aExpectValue) {
this.aExpectValue = aExpectValue;
}
public void setCheckBSQL(String checkBSQL) {
this.checkBSQL = checkBSQL;
}
public void setbExpectValue(String bExpectValue) {
this.bExpectValue = bExpectValue;
}
@Override
public String toString() {
return "Case{" +
......@@ -92,6 +125,9 @@ public class Case {
", apiID='" + apiID + '\'' +
", expectValue='" + expectValue + '\'' +
", checkSQL='" + checkSQL + '\'' +
", aExpectValue='" + aExpectValue + '\'' +
", checkBSQL='" + checkBSQL + '\'' +
", bExpectValue='" + bExpectValue + '\'' +
'}';
}
}
package com.lemon.pojo;
import cn.afterturn.easypoi.excel.annotation.Excel;
/**
* @program: Lemon
* @description:
* @author: ray
* @create: 2022-07-17 14:10
**/
public class Parameter {
// @Email 验证该行是否满足email的格式,若不满足则过滤
@Excel(name = "参数")
private String params;
@Excel(name = "数据")
private String data;
public Parameter() {
}
public Parameter(String params, String data) {
this.params = params;
this.data = data;
}
public String getParams() {
return params;
}
public String getData() {
return data;
}
public void setParams(String params) {
this.params = params;
}
public void setData(String data) {
this.data = data;
}
@Override
public String toString() {
return "Case{" +
"params='" + params + '\'' +
", data='" + data + '\'' +
'}';
}
}
......@@ -5,6 +5,7 @@ import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.lemon.constants.Constants;
import com.lemon.pojo.API;
import com.lemon.pojo.Case;
import com.lemon.pojo.Parameter;
import com.lemon.pojo.WriteBackData;
import org.apache.poi.ss.usermodel.*;
......@@ -26,6 +27,8 @@ public class ExcelUtils {
//读取Excel中第2个sheet,Case数据
public static List<Case> caseList = read(1, Case.class);
// 读取excel中第3个sheet页,Parameter数据
public static List<Parameter> parameterList = read(2, Parameter.class);
//回写Excel数据集合
public static List<WriteBackData> wbdList = new ArrayList<>();
......@@ -129,6 +132,24 @@ public class ExcelUtils {
if (apiId.equals(aCase.getApiID())) {
wantCaseList.add(aCase);
}
//从parameter参数替换中将数据替换到case中
for (Parameter parameter : parameterList) {
if (aCase.getParams().contains(parameter.getParams())) {
String params = parameter.getParams();
String replace = aCase.getParams().replace(params, parameter.getData());
aCase.setParams(replace); // 替换到参数中
}
if (aCase.getCheckSQL() != null && aCase.getCheckSQL().contains(parameter.getParams())) {
String params = parameter.getParams();
String replacesql = aCase.getCheckSQL().replace(params, parameter.getData());
aCase.setCheckSQL(replacesql); // 替换到A段校验sql中
}
if(aCase.getCheckSQL() != null && aCase.getCheckBSQL().contains(parameter.getParams())) {
String params = parameter.getParams();
String replacebsql = aCase.getCheckBSQL().replace(params, parameter.getData());
aCase.setCheckBSQL(replacebsql); // 替换到B段校验sql中
}
}
}
// wantCaselist和wantAPI是有关联的,他们的apiId相等。一个API接口对应多条Case
Object[][] datas = new Object[wantCaseList.size()][2];
......@@ -190,7 +211,8 @@ public class ExcelUtils {
public static void main(String[] args) throws Exception {
List<API> apiList = read(0, API.class);
List<Case> caseList = read(1, Case.class);
System.out.println(apiList);
List<Parameter> parameterList = read(2, Parameter.class);
System.out.println(parameterList);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment