diff --git a/pom.xml b/pom.xml index fc157b3..f03e7e1 100644 --- a/pom.xml +++ b/pom.xml @@ -299,5 +299,11 @@ + + + org.reflections + reflections + 0.9.11 + diff --git a/src/main/java/luckyclient/caserun/publicdispose/ActionContext.java b/src/main/java/luckyclient/caserun/publicdispose/ActionContext.java new file mode 100644 index 0000000..dab8c66 --- /dev/null +++ b/src/main/java/luckyclient/caserun/publicdispose/ActionContext.java @@ -0,0 +1,65 @@ +package luckyclient.caserun.publicdispose; + +import luckyclient.caserun.publicdispose.actionkeyword.Action; +import luckyclient.caserun.publicdispose.actionkeyword.ActionKeyWordParser; +import luckyclient.publicclass.LogUtil; +import org.reflections.Reflections; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collections; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +/** + * 步骤动作容器,根据参数生成不同的动作关键字类型,执行相应的解析 + * @author: sunshaoyan@ + * @date: Created on 2019/4/13 + */ +public class ActionContext { + + private static Logger logger = LoggerFactory.getLogger(ActionContext.class); + + private static Map allActions; + + static { + Reflections reflections = new Reflections("luckyclient.caserun.publicdispose.actionkeyword"); + Set> annotatedClasses = + reflections.getTypesAnnotatedWith(Action.class); + allActions = new ConcurrentHashMap(); + for (Class classObject : annotatedClasses) { + Action action = (Action) classObject + .getAnnotation(Action.class); + allActions.put(action.name(), classObject); + } + allActions = Collections.unmodifiableMap(allActions); + } + + private ActionKeyWordParser action; + + public ActionContext(String name){ + + if (allActions.containsKey(name)) { + logger.info("Created Action name is {}", name); + try { + action = (ActionKeyWordParser) allActions.get(name).newInstance(); + } catch (InstantiationException | IllegalAccessException ex) { + logger.error("Instantiate Action failed", ex); + } + } else { + logger.error("Specified Action name {} does not exist", name); + } + } + + public String parse(String actionKeyWord,String testResult) { + if(null != action){ + testResult = action.parse(actionKeyWord, testResult); + }else { + testResult="未检索到对应动作关键字,直接跳过此动作,请检查关键字:"+actionKeyWord; + LogUtil.APP.error("未检索到对应动作关键字,直接跳过此动作,请检查关键字:"+actionKeyWord); + } + return testResult; + } + +} diff --git a/src/main/java/luckyclient/caserun/publicdispose/ActionManageForSteps.java b/src/main/java/luckyclient/caserun/publicdispose/ActionManageForSteps.java index 0556a38..040539b 100644 --- a/src/main/java/luckyclient/caserun/publicdispose/ActionManageForSteps.java +++ b/src/main/java/luckyclient/caserun/publicdispose/ActionManageForSteps.java @@ -1,6 +1,5 @@ package luckyclient.caserun.publicdispose; -import luckyclient.driven.SubString; import luckyclient.publicclass.LogUtil; /** @@ -13,12 +12,11 @@ import luckyclient.publicclass.LogUtil; * @date 2019年1月15日 */ public class ActionManageForSteps { + /** * 解析用例步骤 - * @param projectcase - * @param step - * @param taskid - * @param caselog + * @param stepsaction + * @param testresult * @return */ public static String actionManage(String stepsaction,String testresult){ @@ -28,7 +26,7 @@ public class ActionManageForSteps { LogUtil.APP.info("Action(动作)无需处理......"); return testresult; } - stepsaction=stepsaction.toLowerCase().trim(); + stepsaction=stepsaction.trim(); String[] temp=stepsaction.split("\\|",-1); for(String actionorder:temp){ if(null!=actionorder&&!"".equals(actionorder.trim())){ @@ -37,93 +35,29 @@ public class ActionManageForSteps { } return testresult; } - + /** * 动作关键字执行 - * @param actionorder - * @param testresult + * @param actionKeyWord + * @param testResult * @return */ - private static String actionExecute(String actionorder,String testresult){ + private static String actionExecute(String actionKeyWord,String testResult){ try{ - // 处理动作事件 - if(actionorder.endsWith("#wait")){ - if(ChangString.isInteger(actionorder.substring(0, actionorder.lastIndexOf("#wait")))){ - try { - // 获取步骤间等待时间 - int time=Integer.parseInt(actionorder.substring(0, actionorder.lastIndexOf("#wait"))); - if (time > 0) { - LogUtil.APP.info("Action(Wait):线程等待"+time+"秒..."); - Thread.sleep(time * 1000); - } - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - }else{ - LogUtil.APP.warn("使用等待关键字的参数不是整数,直接跳过此动作,请检查!"); - } - }else if(actionorder.endsWith("#getjv")){ - String actionparams=actionorder.substring(0, actionorder.lastIndexOf("#getjv")); - 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); - }else if(actionorder.endsWith("#subcentrestr")){ - String actionparams=actionorder.substring(0, actionorder.lastIndexOf("#subcentrestr")); - String startstr=""; - String endstr=""; - if(actionparams.startsWith("[")&&actionparams.endsWith("]")){ - startstr=actionparams.substring(actionparams.indexOf("[")+1, actionparams.indexOf("]")); - endstr=actionparams.substring(actionparams.lastIndexOf("[")+1, actionparams.lastIndexOf("]")); - testresult=SubString.subCentreStr(testresult, startstr, endstr); - LogUtil.APP.info("Action(subCentreStr):截取测试结果指定开始及结束位置字符串:"+testresult); - }else{ - testresult="步骤动作:subCentreStr 必须是[\"开始字符\"][\"结束字符\"]#subCentreStr 格式,请检查您的步骤动作关键字:"+actionorder; - LogUtil.APP.warn("步骤动作:subCentreStr 必须是[\"开始字符\"][\"结束字符\"]#subCentreStr 格式,请检查您的步骤动作关键字:"+actionorder); - } - }else if(actionorder.endsWith("#subcentrenum")){ - String actionparams=actionorder.substring(0, actionorder.lastIndexOf("#subcentrenum")); - String startnum=""; - String endnum=""; - if(actionparams.startsWith("[")&&actionparams.endsWith("]")){ - startnum=actionparams.substring(actionparams.indexOf("[")+1, actionparams.indexOf("]")); - endnum=actionparams.substring(actionparams.lastIndexOf("[")+1, actionparams.lastIndexOf("]")); - testresult=SubString.subCentreNum(testresult, startnum, endnum); - LogUtil.APP.info("Action(subCentreNum):截取测试结果指定开始及结束位置字符串:"+testresult); - }else{ - testresult="步骤动作:subCentreNum 必须是[\"开始字符\"][\"结束字符\"]#subCentreNum 格式,请检查您的步骤动作关键字:"+actionorder; - LogUtil.APP.warn("步骤动作:subCentreNum 必须是[\"开始位置(整数)\"][\"结束位置(整数)\"]#subCentreNum 格式,请检查您的步骤动作关键字:"+actionorder); - } - }else if(actionorder.endsWith("#substrrgex")){ - String actionparams=actionorder.substring(0, actionorder.lastIndexOf("#substrrgex")); - 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.subStrRgex(testresult, key, index); - }else{ - key=actionparams; - testresult=SubString.subStrRgex(testresult, key, index); - } - LogUtil.APP.info("Action(subStrRgex):获取JSON字符串指定Key的值是:"+testresult); - }else{ - testresult="未检索到对应动作关键字,直接跳过此动作,请检查关键字:"+actionorder; - LogUtil.APP.warn("未检索到对应动作关键字,直接跳过此动作,请检查关键字:"+actionorder); + + String[] actionArr = actionKeyWord.split("#"); + if(actionArr.length == 2){ + ActionContext actionContext = new ActionContext(actionArr[1]); + testResult = actionContext.parse(actionKeyWord, testResult); + }else { + testResult="关键字语法书写有误,请检查关键字:"+actionKeyWord; + LogUtil.APP.error("关键字语法书写有误,请检查关键字:"+actionKeyWord); } - return testresult; + return testResult; }catch(Exception e){ - testresult="处理步骤动作事件过程中出现异常,直接返回测试结果:"+actionorder; - LogUtil.APP.error("处理步骤动作事件过程中出现异常,直接返回测试结果:"+actionorder); - return testresult; + testResult="处理步骤动作事件过程中出现异常,直接返回测试结果:"+actionKeyWord; + LogUtil.APP.error("处理步骤动作事件过程中出现异常,直接返回测试结果:" ,e); + return testResult; } } diff --git a/src/main/java/luckyclient/caserun/publicdispose/actionkeyword/Action.java b/src/main/java/luckyclient/caserun/publicdispose/actionkeyword/Action.java new file mode 100644 index 0000000..f8bce99 --- /dev/null +++ b/src/main/java/luckyclient/caserun/publicdispose/actionkeyword/Action.java @@ -0,0 +1,17 @@ +package luckyclient.caserun.publicdispose.actionkeyword; + +/** + * 动作关键字注解定义 + * @author: sunshaoyan + * @date: Created on 2019/4/13 + */ + +import java.lang.annotation.*; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +public @interface Action { + String name() default ""; +} diff --git a/src/main/java/luckyclient/caserun/publicdispose/actionkeyword/ActionKeyWordParser.java b/src/main/java/luckyclient/caserun/publicdispose/actionkeyword/ActionKeyWordParser.java new file mode 100644 index 0000000..ec24a5c --- /dev/null +++ b/src/main/java/luckyclient/caserun/publicdispose/actionkeyword/ActionKeyWordParser.java @@ -0,0 +1,11 @@ +package luckyclient.caserun.publicdispose.actionkeyword; + +/** + * 动作关键字的处理接口 + * @author: sunshaoyan + * @date: Created on 2019/4/13 + */ +public interface ActionKeyWordParser { + + String parse(String actionKeyWord, String testResult); +} diff --git a/src/main/java/luckyclient/caserun/publicdispose/actionkeyword/GetJsonActionParser.java b/src/main/java/luckyclient/caserun/publicdispose/actionkeyword/GetJsonActionParser.java new file mode 100644 index 0000000..ca94f49 --- /dev/null +++ b/src/main/java/luckyclient/caserun/publicdispose/actionkeyword/GetJsonActionParser.java @@ -0,0 +1,36 @@ +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 actionKeyWord, String testResult) { + String actionparams=actionKeyWord.substring(0, actionKeyWord.lastIndexOf("#getJV")); + 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; + } +} diff --git a/src/main/java/luckyclient/caserun/publicdispose/actionkeyword/HeaderParser.java b/src/main/java/luckyclient/caserun/publicdispose/actionkeyword/HeaderParser.java new file mode 100644 index 0000000..d1a2ae2 --- /dev/null +++ b/src/main/java/luckyclient/caserun/publicdispose/actionkeyword/HeaderParser.java @@ -0,0 +1,29 @@ +package luckyclient.caserun.publicdispose.actionkeyword; + + +import com.alibaba.fastjson.JSONObject; + +/** + * 动作关键字的处理接口的实现类:从响应header中取出某个header值 + * @author: sunshaoyan + * @date: Created on 2019/4/13 + */ +@Action(name="Header") +public class HeaderParser implements ActionKeyWordParser { + + + /** + * @param actionorder 动作关键字 + */ + @Override + public String parse(String actionorder, String testResult) { + + + // 获取步骤间等待时间 + String headerParam=actionorder.substring(0, actionorder.lastIndexOf("#Header")); + String pre = "RESPONSE_HEAD:【"; + String headerStr = testResult.substring(testResult.indexOf(pre) + pre.length(), testResult.indexOf("】 RESPONSE_CODE")); + return JSONObject.parseObject(headerStr).getJSONArray(headerParam).getString(0); + + } +} diff --git a/src/main/java/luckyclient/caserun/publicdispose/actionkeyword/SubCentreNumActionParser.java b/src/main/java/luckyclient/caserun/publicdispose/actionkeyword/SubCentreNumActionParser.java new file mode 100644 index 0000000..49d3bbd --- /dev/null +++ b/src/main/java/luckyclient/caserun/publicdispose/actionkeyword/SubCentreNumActionParser.java @@ -0,0 +1,36 @@ +package luckyclient.caserun.publicdispose.actionkeyword; + + +import luckyclient.driven.SubString; +import luckyclient.publicclass.LogUtil; + +/** + * 动作关键字的处理接口的实现类:截取测试结果指定开始及结束位置字符串 + * @author: sunshaoyan + * @date: Created on 2019/4/13 + */ +@Action(name="subCentreNum") +public class SubCentreNumActionParser implements ActionKeyWordParser { + + + /** + * 截取测试结果指定开始及结束位置字符串 + * @param actionKeyWord 动作关键字 + * @param testResult 测试结果 + */ + @Override + public String parse(String actionKeyWord, String testResult) { + String actionParams=actionKeyWord.substring(0, actionKeyWord.lastIndexOf("#subCentreNum")); + + if(actionParams.startsWith("[")&&actionParams.endsWith("]")){ + String startnum=actionParams.substring(actionParams.indexOf("[")+1, actionParams.indexOf("]")); + String endnum=actionParams.substring(actionParams.lastIndexOf("[")+1, actionParams.lastIndexOf("]")); + testResult= SubString.subCentreNum(testResult, startnum, endnum); + LogUtil.APP.info("Action(subCentreNum):截取测试结果指定开始及结束位置字符串:"+testResult); + }else{ + testResult="步骤动作:subCentreNum 必须是[\"开始字符\"][\"结束字符\"]#subCentreNum 格式,请检查您的步骤动作关键字:"+actionKeyWord; + LogUtil.APP.error("步骤动作:subCentreNum 必须是[\"开始位置(整数)\"][\"结束位置(整数)\"]#subCentreNum 格式,请检查您的步骤动作关键字:"+actionKeyWord); + } + return testResult; + } +} diff --git a/src/main/java/luckyclient/caserun/publicdispose/actionkeyword/SubCentresStrActionParser.java b/src/main/java/luckyclient/caserun/publicdispose/actionkeyword/SubCentresStrActionParser.java new file mode 100644 index 0000000..28f5669 --- /dev/null +++ b/src/main/java/luckyclient/caserun/publicdispose/actionkeyword/SubCentresStrActionParser.java @@ -0,0 +1,36 @@ +package luckyclient.caserun.publicdispose.actionkeyword; + +import luckyclient.driven.SubString; +import luckyclient.publicclass.LogUtil; + +/** + * 动作关键字的处理接口的实现类:截取测试结果指定开始及结束位置字符串 + * @author: sunshaoyan + * @date: Created on 2019/4/13 + */ +@Action(name="subCentreStr") +public class SubCentresStrActionParser implements ActionKeyWordParser { + + + /** + * 截取测试结果指定开始及结束位置字符串 + * @param actionKeyWord 动作关键字 + * @param testResult 测试结果 + */ + @Override + public String parse(String actionKeyWord, String testResult) { + String actionparams=actionKeyWord.substring(0, actionKeyWord.lastIndexOf("#subCentreStr")); + String startstr=""; + String endstr=""; + if(actionparams.startsWith("[")&&actionparams.endsWith("]")){ + startstr=actionparams.substring(actionparams.indexOf("[")+1, actionparams.indexOf("]")); + endstr=actionparams.substring(actionparams.lastIndexOf("[")+1, actionparams.lastIndexOf("]")); + testResult= SubString.subCentreStr(testResult, startstr, endstr); + LogUtil.APP.info("Action(subCentreStr):截取测试结果指定开始及结束位置字符串:"+testResult); + }else{ + testResult="步骤动作:subCentreStr 必须是[\"开始字符\"][\"结束字符\"]#subCentreStr 格式,请检查您的步骤动作关键字:"+actionKeyWord; + LogUtil.APP.error("步骤动作:subCentreStr 必须是[\"开始字符\"][\"结束字符\"]#subCentreStr 格式,请检查您的步骤动作关键字:"+actionKeyWord); + } + return testResult; + } +} diff --git a/src/main/java/luckyclient/caserun/publicdispose/actionkeyword/SubStrRegxActionParser.java b/src/main/java/luckyclient/caserun/publicdispose/actionkeyword/SubStrRegxActionParser.java new file mode 100644 index 0000000..60c1939 --- /dev/null +++ b/src/main/java/luckyclient/caserun/publicdispose/actionkeyword/SubStrRegxActionParser.java @@ -0,0 +1,36 @@ +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="subStrRgex") +public class SubStrRegxActionParser implements ActionKeyWordParser { + + + /** + * 获取JSON字符串指定Key的值,采用正则匹配 + * @param actionKeyWord 动作关键字 + * @param testResult 测试结果 + */ + @Override + public String parse(String actionKeyWord, String testResult) { + String actionparams=actionKeyWord.substring(0, actionKeyWord.lastIndexOf("#subStrRgex")); + 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.subStrRgex(testResult, key, index); + }else{ + key=actionparams; + testResult= SubString.subStrRgex(testResult, key, index); + } + LogUtil.APP.info("Action(subStrRgex):获取JSON字符串指定Key的值是:"+testResult); + return testResult; + } +} diff --git a/src/main/java/luckyclient/caserun/publicdispose/actionkeyword/ThreadWaitAction.java b/src/main/java/luckyclient/caserun/publicdispose/actionkeyword/ThreadWaitAction.java new file mode 100644 index 0000000..bf56f27 --- /dev/null +++ b/src/main/java/luckyclient/caserun/publicdispose/actionkeyword/ThreadWaitAction.java @@ -0,0 +1,38 @@ +package luckyclient.caserun.publicdispose.actionkeyword; + + +import luckyclient.caserun.publicdispose.ChangString; +import luckyclient.publicclass.LogUtil; + +/** + * 动作关键字的处理接口的实现类:线程等待时间 + * @author: sunshaoyan + * @date: Created on 2019/4/13 + */ +@Action(name="Wait") +public class ThreadWaitAction implements ActionKeyWordParser { + + + /** + * @param actionorder 动作关键字 + */ + @Override + public String parse(String actionorder, String testResult) { + if(ChangString.isInteger(actionorder.substring(0, actionorder.lastIndexOf("#Wait")))){ + try { + // 获取步骤间等待时间 + int time=Integer.parseInt(actionorder.substring(0, actionorder.lastIndexOf("#Wait"))); + if (time > 0) { + LogUtil.APP.info("Action(Wait):线程等待"+time+"秒..."); + Thread.sleep(time * 1000); + } + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + }else{ + LogUtil.APP.error("使用等待关键字的参数不是整数,直接跳过此动作,请检查!"); + } + return testResult; + } +} diff --git a/src/main/java/luckyclient/driven/SubString.java b/src/main/java/luckyclient/driven/SubString.java index cd99c18..2c52b4b 100644 --- a/src/main/java/luckyclient/driven/SubString.java +++ b/src/main/java/luckyclient/driven/SubString.java @@ -184,9 +184,9 @@ public class SubString { Pattern pattern = Pattern.compile(rgex);// 匹配的模式 Matcher m = pattern.matcher(str); while (m.find()) { - int i = 1; - list.add(m.group(i)); - i++; +// int i = 1; + list.add(m.group()); +// i++; } String getstr = "";