重新优化jsonpath用法,语法更新,详情请查看后续版本更新说明

This commit is contained in:
seagull 2019-09-03 20:07:58 +08:00
parent 859ee8df8d
commit ce9a6b53b7
10 changed files with 180 additions and 140 deletions

26
.classpath Normal file
View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

23
.project Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>LuckyFrame</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>

View File

@ -235,7 +235,7 @@
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
<dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
@ -297,12 +297,10 @@
<artifactId>reflections</artifactId>
<version>0.9.11</version>
</dependency>
<!-- jsonpath依赖 -->
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.4.0</version>
</dependency>
</dependencies>
</project>

View File

@ -1,18 +1,11 @@
package luckyclient.caserun.exinterface;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import org.apache.commons.compress.utils.Lists;
import org.openqa.selenium.WebDriver;
import io.appium.java_client.android.AndroidDriver;
@ -29,6 +22,7 @@ import luckyclient.caserun.publicdispose.ActionManageForSteps;
import luckyclient.caserun.publicdispose.ParamsManageForSteps;
import luckyclient.dblog.DbLink;
import luckyclient.dblog.LogOperation;
import luckyclient.driven.SubString;
import luckyclient.publicclass.InvokeMethod;
import luckyclient.publicclass.LogUtil;
import luckyclient.serverapi.api.GetServerApi;
@ -51,7 +45,7 @@ public class TestCaseExecution {
protected static final String FUZZY_MATCHING_SIGN = "%=";
protected static final String REGULAR_MATCHING_SIGN = "~=";
protected static final String ASSIGNMENT_GLOBALSIGN = "$A=";
protected static final String JSONPATH_SIGN = "$J=";
protected static final String JSONPATH_SIGN = "$JP#";
private static Map<String, String> VARIABLE = new HashMap<String, String>(0);
/**
@ -292,21 +286,14 @@ public class TestCaseExecution {
//jsonpath断言
else if (expectedresults.length() > JSONPATH_SIGN.length() && expectedresults.startsWith(JSONPATH_SIGN)) {
expectedresults = expectedresults.substring(JSONPATH_SIGN.length());
String jsonpath = expectedresults.split("=")[0];
String exceptResult = expectedresults.split("=")[1];
List<String> exceptResultList = Arrays.asList(exceptResult.split("(?<!&),"));
List<String> exceptResults = Lists.newArrayList();
// 处理期望值里本身包含英文逗号的情况
for (String s : exceptResultList) {
s = s.replace("&,",",");
exceptResults.add(s);
}
Configuration conf = Configuration.defaultConfiguration();
JSONArray datasArray = JSON.parseArray(JSON.toJSONString(JsonPath.using(conf).parse(testnote).read(jsonpath)));
List<String> result = JSONObject.parseArray(datasArray.toJSONString(), String.class);
if (exceptResults.equals(result)) {
String expression = expectedresults.split("(?<!\\\\)=")[0].replace("\\=","=");
String exceptResult = expectedresults.split("(?<!\\\\)=")[1].replace("\\=","=");
//对测试结果进行jsonPath取值
String result = SubString.jsonPathGetParams(expression, testnote);
if (exceptResult.equals(result)) {
setresult = 0;
LogUtil.APP.info("用例:{} 第{}步jsonpath断言预期结果成功预期结果:{} 测试结果: {} 执行结果:true",testcase.getCaseSign(),(i+1),exceptResults,result);
LogUtil.APP.info("用例:{} 第{}步jsonpath断言预期结果成功预期结果:{} 测试结果: {} 执行结果:true",testcase.getCaseSign(),(i+1),exceptResult,result);
} else {
setresult = 1;
LogUtil.APP.warn("用例:{} 第{}步jsonpath断言预期结果失败预期结果:{},测试结果:{}",testcase.getCaseSign(),(i+1),expectedresults.substring(REGULAR_MATCHING_SIGN.length()),testnote);
@ -542,21 +529,14 @@ public class TestCaseExecution {
//jsonpath断言
else if (expectedresults.length() > JSONPATH_SIGN.length() && expectedresults.startsWith(JSONPATH_SIGN)) {
expectedresults = expectedresults.substring(JSONPATH_SIGN.length());
String jsonpath = expectedresults.split("=")[0];
String exceptResult = expectedresults.split("=")[1];
List<String> exceptResultList = Arrays.asList(exceptResult.split("(?<!&),"));
List<String> exceptResults = Lists.newArrayList();
// 处理期望值里本身包含英文逗号的情况
for (String s : exceptResultList) {
s = s.replace("&,",",");
exceptResults.add(s);
}
Configuration conf = Configuration.defaultConfiguration();
JSONArray datasArray = JSON.parseArray(JSON.toJSONString(JsonPath.using(conf).parse(testnote).read(jsonpath)));
List<String> result = JSONObject.parseArray(datasArray.toJSONString(), String.class);
if (exceptResults.equals(result)) {
String expression = expectedresults.split("(?<!\\\\)=")[0].replace("\\=","=");
String exceptResult = expectedresults.split("(?<!\\\\)=")[1].replace("\\=","=");
//对测试结果进行jsonPath取值
String result = SubString.jsonPathGetParams(expression, testnote);
if (exceptResult.equals(result)) {
setresult = 0;
LogUtil.APP.info("用例:{} 第{}步jsonpath断言预期结果成功预期结果:{} 测试结果: {} 执行结果:true",testcase.getCaseSign(),step.getStepSerialNumber(),exceptResults,result);
LogUtil.APP.info("用例:{} 第{}步jsonpath断言预期结果成功预期结果:{} 测试结果: {} 执行结果:true",testcase.getCaseSign(),step.getStepSerialNumber(),exceptResult,result);
} else {
setresult = 1;
LogUtil.APP.warn("用例:{} 第{}步jsonpath断言预期结果失败预期结果:{},测试结果:{}" + expectedresults + ",测试结果:" + result.toString(), "error", String.valueOf(step.getStepSerialNumber()), "");

View File

@ -1,27 +1,21 @@
package luckyclient.caserun.exinterface;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import luckyclient.caserun.exinterface.analyticsteps.InterfaceAnalyticCase;
import luckyclient.caserun.publicdispose.ActionManageForSteps;
import luckyclient.caserun.publicdispose.ParamsManageForSteps;
import luckyclient.dblog.LogOperation;
import luckyclient.driven.SubString;
import luckyclient.publicclass.InvokeMethod;
import luckyclient.publicclass.LogUtil;
import luckyclient.serverapi.entity.ProjectCase;
import luckyclient.serverapi.entity.ProjectCaseParams;
import luckyclient.serverapi.entity.ProjectCaseSteps;
import org.apache.commons.compress.utils.Lists;
/**
* =================================================================
@ -40,7 +34,7 @@ public class ThreadForExecuteCase extends Thread {
private static final String ASSIGNMENT_GLOBALSIGN = "$A=";
private static final String FUZZY_MATCHING_SIGN = "%=";
private static final String REGULAR_MATCHING_SIGN = "~=";
private static final String JSONPATH_SIGN = "$J=";
private static final String JSONPATH_SIGN = "$JP#";
private Integer caseId;
private String caseSign;
@ -180,27 +174,19 @@ public class ThreadForExecuteCase extends Thread {
//jsonpath断言
else if (expectedresults.length() > JSONPATH_SIGN.length() && expectedresults.startsWith(JSONPATH_SIGN)) {
expectedresults = expectedresults.substring(JSONPATH_SIGN.length());
String jsonpath = expectedresults.split("=")[0];
String exceptResult = expectedresults.split("=")[1];
List<String> exceptResultList = Arrays.asList(exceptResult.split("(?<!&),"));
List<String> exceptResults = Lists.newArrayList();
// 处理期望值里本身包含英文逗号的情况
for (String s : exceptResultList) {
s = s.replace("&,",",");
exceptResults.add(s);
}
Configuration conf = Configuration.defaultConfiguration();
JSONArray datasArray = JSON.parseArray(JSON.toJSONString(JsonPath.using(conf).parse(testnote).read(jsonpath)));
List<String> result = JSONObject.parseArray(datasArray.toJSONString(), String.class);
if (exceptResults.equals(result)) {
String expression = expectedresults.split("(?<!\\\\)=")[0].replace("\\=","=");
String exceptResult = expectedresults.split("(?<!\\\\)=")[1].replace("\\=","=");
//对测试结果进行jsonPath取值
String result = SubString.jsonPathGetParams(expression, testnote);
if (exceptResult.equals(result)) {
setcaseresult = 0;
LogUtil.APP.info("用例【{}】 第【{}】步jsonpath断言预期结果成功预期结果:{} 测试结果: {} 执行结果:true",testcase.getCaseSign(),(i+1),exceptResults,result);
LogUtil.APP.info("用例【{}】 第【{}】步jsonpath断言预期结果成功预期结果:{} 测试结果: {} 执行结果:true",testcase.getCaseSign(),(i+1),exceptResult,result);
caselog.insertTaskCaseLog(taskid, caseId, "jsonpath断言预期结果成功预期结果:"+ expectedresults + "测试结果:" + result + "执行结果:true","info", String.valueOf(i + 1), "");
} else {
setcaseresult = 1;
LogUtil.APP.warn("用例:{} 第{}步jsonpath断言预期结果失败预期结果:{},测试结果:{}",testcase.getCaseSign(),(i+1),expectedresults,result);
caselog.insertTaskCaseLog(taskid, caseId, "" + (i + 1) + "步,正则匹配预期结果失败!预期结果:" + exceptResults + ",测试结果:" + result, "error", String.valueOf(i + 1), "");
caselog.insertTaskCaseLog(taskid, caseId, "" + (i + 1) + "步,正则匹配预期结果失败!预期结果:" + exceptResult + ",测试结果:" + result, "error", String.valueOf(i + 1), "");
testnote = "用例第" + (i + 1) + "jsonpath断言预期结果失败";
if (testcase.getFailcontinue() == 0) {
LogUtil.APP.warn("用例【{}】第【{}】步骤执行失败,中断本条用例后续步骤执行,进入到下一条用例执行中......",testcase.getCaseSign(),(i+1));
@ -279,7 +265,4 @@ public class ThreadForExecuteCase extends Thread {
}
}
public static void main(String[] args) {
}
}

View File

@ -1,19 +1,14 @@
package luckyclient.caserun.exinterface;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import luckyclient.caserun.exinterface.analyticsteps.InterfaceAnalyticCase;
import luckyclient.caserun.publicdispose.ActionManageForSteps;
import luckyclient.driven.SubString;
import luckyclient.publicclass.InvokeMethod;
import luckyclient.publicclass.LogUtil;
import luckyclient.serverapi.api.GetServerApi;
@ -21,7 +16,6 @@ import luckyclient.serverapi.api.PostServerApi;
import luckyclient.serverapi.entity.ProjectCase;
import luckyclient.serverapi.entity.ProjectCaseParams;
import luckyclient.serverapi.entity.ProjectCaseSteps;
import org.apache.commons.compress.utils.Lists;
/**
* =================================================================
@ -39,7 +33,7 @@ public class WebTestCaseDebug {
private static final String ASSIGNMENT_SIGN = "$=";
private static final String FUZZY_MATCHING_SIGN = "%=";
private static final String REGULAR_MATCHING_SIGN = "~=";
protected static final String JSONPATH_SIGN = "$J=";
protected static final String JSONPATH_SIGN = "$JP#";
/**
* @param executor
@ -150,24 +144,17 @@ public class WebTestCaseDebug {
//jsonpath断言
else if (expectedresults.length() > JSONPATH_SIGN.length() && expectedresults.startsWith(JSONPATH_SIGN)) {
expectedresults = expectedresults.substring(JSONPATH_SIGN.length());
String jsonpath = expectedresults.split("=")[0];
String exceptResult = expectedresults.split("=")[1];
List<String> exceptResultList = Arrays.asList(exceptResult.split("(?<!&),"));
List<String> exceptResults = Lists.newArrayList();
// 处理期望值里本身包含英文逗号的情况
for (String s : exceptResultList) {
s = s.replace("&,",",");
exceptResults.add(s);
}
Configuration conf = Configuration.defaultConfiguration();
JSONArray datasArray = JSON.parseArray(JSON.toJSONString(JsonPath.using(conf).parse(testnote).read(jsonpath)));
List<String> result = JSONObject.parseArray(datasArray.toJSONString(), String.class);
if (exceptResults.equals(result)) {
String expression = expectedresults.split("(?<!\\\\)=")[0].replace("\\=","=");
String exceptResult = expectedresults.split("(?<!\\\\)=")[1].replace("\\=","=");
//对测试结果进行jsonPath取值
String result = SubString.jsonPathGetParams(expression, testnote);
if (exceptResult.equals(result)) {
setcaseresult = 0;
PostServerAPI.cPostDebugLog(userId, caseId, "INFO", "jsonpath断言预期结果成功预期结果" + expectedresults + " 测试结果: " + result.toString() + "校验结果: true", 0);
PostServerApi.cPostDebugLog(userId, caseId, "INFO", "jsonpath断言预期结果成功预期结果" + exceptResult + " 测试结果: " + result.toString() + "校验结果: true", 0);
} else {
setcaseresult = 1;
PostServerAPI.cPostDebugLog(userId, caseId, "ERROR", "" + (i + 1) + "jsonpath断言预期结果失败预期结果" + expectedresults + ",测试结果:" + result.toString(),0);
PostServerApi.cPostDebugLog(userId, caseId, "ERROR", "" + (i + 1) + "jsonpath断言预期结果失败预期结果" + exceptResult + ",测试结果:" + result.toString(),0);
testnote = "用例第" + (i + 1) + "jsonpath断言预期结果失败";
if (testcase.getFailcontinue() == 0) {
LogUtil.APP.warn("用例【{}】第【{}】步骤执行失败,中断本条用例后续步骤执行,进入到下一条用例执行中......",testcase.getCaseSign(),(i+1));

View File

@ -370,8 +370,8 @@ public class ChangString {
return map;
}
public static void main(String[] args) {
}
public static void main(String[] args) {
}
}

View File

@ -1,35 +1,35 @@
package luckyclient.caserun.publicdispose.actionkeyword;
import luckyclient.driven.SubString;
import luckyclient.publicclass.LogUtil;
/**
* 动作关键字的处理接口的实现类获取JSON字符串指定Key的值的
* @author: sunshaoyan
* @date: Created on 2019/4/13
*/
@Action(name="getjv")
public class GetJsonActionParser implements ActionKeyWordParser {
/**
* 获取JSON字符串指定Key的值是
* @param actionKeyWord 动作关键字
* @param testResult 测试结果
*/
@Override
public String parse(String actionParams, String testResult) {
String key="";
String index="1";
if(actionParams.endsWith("]")&&actionParams.contains("[")){
key=actionParams.substring(0,actionParams.lastIndexOf("["));
index=actionParams.substring(actionParams.lastIndexOf("[")+1, actionParams.lastIndexOf("]"));
testResult= SubString.getJsonValue(testResult, key, index);
}else{
key=actionParams;
testResult=SubString.getJsonValue(testResult, key, index);
}
LogUtil.APP.info("Action(getJV):获取JSON字符串指定Key的值是:{}",testResult);
return testResult;
}
}
package luckyclient.caserun.publicdispose.actionkeyword;
import luckyclient.driven.SubString;
import luckyclient.publicclass.LogUtil;
/**
* 动作关键字的处理接口的实现类获取JSON字符串指定Key的值的
* @author: sunshaoyan
* @date: Created on 2019/4/13
*/
@Action(name="getjv")
public class GetJsonActionParser implements ActionKeyWordParser {
/**
* 获取JSON字符串指定Key的值是
* @param actionKeyWord 动作关键字
* @param testResult 测试结果
*/
@Override
public String parse(String actionParams, String testResult) {
String key="";
String index="1";
if(actionParams.endsWith("]")&&actionParams.contains("[")){
key=actionParams.substring(0,actionParams.indexOf("["));
index=actionParams.substring(actionParams.indexOf("[")+1, actionParams.lastIndexOf("]"));
testResult= SubString.getJsonValue(testResult, key, index);
}else{
key=actionParams;
testResult=SubString.getJsonValue(testResult, key, index);
}
LogUtil.APP.info("Action(getJV):获取JSON字符串指定Key的值是:{}",testResult);
return testResult;
}
}

View File

@ -17,23 +17,16 @@ public class JsonPathActionParser implements ActionKeyWordParser {
/**
* 获取JSON字符串指定Key的值是
* 通过jsonPath表达式获取JSON字符串指定值
* 仅支持返回值是String类型不支持List,如果jsonPath表达式返回的是List将抛出异常
* @param actionKeyWord 动作关键字
* @param testResult 测试结果
*/
@Override
@Override
public String parse(String actionParams, String testResult) {
String key="";
String index="1";
if(actionParams.endsWith("]")&&actionParams.contains("[")){
key=actionParams.substring(0,actionParams.lastIndexOf("["));
index=actionParams.substring(actionParams.lastIndexOf("[")+1, actionParams.lastIndexOf("]"));
testResult= SubString.getJsonValue(testResult, key, index);
}else{
key=actionParams;
testResult=SubString.getJsonValue(testResult, key, index);
}
LogUtil.APP.info("Action(getJV):获取JSON字符串指定Key的值是:{}",testResult);
LogUtil.APP.info("Action(jsonPath):开始处理jsonPath动作...参数:【{}】 待处理json字符串【{}】",actionParams,testResult);
testResult = SubString.jsonPathGetParams(actionParams, testResult);
LogUtil.APP.info("Action(jsonPath):处理jsonPath动作完成...处理结果【{}】",testResult);
return testResult;
}
}

View File

@ -13,6 +13,8 @@ import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
import luckyclient.publicclass.LogUtil;
@ -362,4 +364,52 @@ public class SubString {
return result;
}
/**
* 通过jsonPath表达式获取JSON字符串指定值
* @param actionParams
* @param testResult
* @return
* @author Seagull
* @date 2019年8月28日
*/
public static String jsonPathGetParams(String expressionParams, String jsonString) {
String type="String";
String expression="";
if(expressionParams.endsWith("]")&&expressionParams.contains("[")){
try{
type=expressionParams.substring(0,expressionParams.indexOf("["));
expression=expressionParams.substring(expressionParams.indexOf("[")+1, expressionParams.lastIndexOf("]"));
if("list".equals(type.toLowerCase())){
List<Object> list = JsonPath.parse(jsonString).read(expression);
jsonString="";
for(Object result:list){
result = (String)jsonString+result+",";
jsonString = (String)result;
}
}else{
jsonString=JsonPath.parse(jsonString).read(expression);
}
}catch(PathNotFoundException pnfe){
LogUtil.APP.error("通过jsonPath获取JSON字符串指定值出现异常没有找到对应参数路径请确认JSON字符串【{}】表达式是否正确【{}】!",jsonString,expression);
}catch(Exception e){
LogUtil.APP.error("通过jsonPath获取JSON字符串指定值出现异请检查您的动作参数格式(String/List[表达式])或是被提取的json字符串是否正常",expressionParams);
}
}else{
LogUtil.APP.warn("获取JSON字符串指定jsonPath表达式【{}】异常,请检查您的动作参数格式(String/List[表达式])是否正常!",expressionParams);
}
LogUtil.APP.info("获取JSON字符串指定jsonPath表达式【{}】的值是:{}",expression,jsonString);
return jsonString;
}
public static void main(String[] args) {
String aa="$JP#\\=fd=sa#fdsdsaa\\#jjdj\\=s#ff";
aa = aa.substring("$JP#".length());
String[] bb = aa.split("(?<!\\\\)=");
System.out.println(bb[0]);
for(String s:bb){
s = s.replace("\\=","=");
System.out.println(s);
}
System.out.println(bb[1]);
}
}