From 2246e416f2358a66ec5020d8234787df0f235091 Mon Sep 17 00:00:00 2001 From: wangkai <646651700@qq.com> Date: Wed, 19 Aug 2020 13:09:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AF=E4=B9=8B=E5=89=8D?= =?UTF-8?q?=E4=B8=8D=E8=83=BD=E4=BD=BF=E7=94=A8idea=E8=B0=83=E8=AF=95?= =?UTF-8?q?=EF=BC=8C=E5=9B=A0=E4=B8=BA=E8=B7=AF=E5=BE=84=E4=B8=8D=E5=AF=B9?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E6=94=B9=E4=BA=86=E4=B8=80=E4=B8=8B=EF=BC=8C?= =?UTF-8?q?=E7=8E=B0=E5=9C=A8=E8=B0=83=E8=AF=95=E5=92=8Cstart=5Fservice.cm?= =?UTF-8?q?d=E9=83=BD=E8=83=BD=E6=AD=A3=E5=B8=B8=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E4=BA=86=20=E5=B0=86System.getProperty("user.dir")=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2=E6=88=90RunService.APPLICATION=5FHOME,?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../execution/BatchCaseExecute.java | 3 ++- .../luckyclient/execution/OneCaseExecute.java | 3 ++- .../execution/RunAutomationTest.java | 3 ++- .../execution/WebDebugExecute.java | 3 ++- .../appium/androidex/AndroidBaseAppium.java | 3 ++- .../execution/appium/iosex/IosBaseAppium.java | 3 ++- .../execution/webdriver/BaseWebDrive.java | 3 ++- .../execution/webdriver/ocr/Ocr.java | 7 +++--- .../java/luckyclient/netty/ClientHandler.java | 4 ++-- .../java/luckyclient/utils/InvokeMethod.java | 3 ++- src/main/java/springboot/HttpImpl.java | 24 +++++++++---------- src/main/java/springboot/RunService.java | 4 +++- 12 files changed, 37 insertions(+), 26 deletions(-) diff --git a/src/main/java/luckyclient/execution/BatchCaseExecute.java b/src/main/java/luckyclient/execution/BatchCaseExecute.java index 6b03323..1d42ded 100644 --- a/src/main/java/luckyclient/execution/BatchCaseExecute.java +++ b/src/main/java/luckyclient/execution/BatchCaseExecute.java @@ -15,6 +15,7 @@ import luckyclient.remote.entity.TaskExecute; import luckyclient.remote.entity.TaskScheduling; import luckyclient.utils.LogUtil; import luckyclient.utils.config.AppiumConfig; +import springboot.RunService; /** * ================================================================= @@ -32,7 +33,7 @@ public class BatchCaseExecute extends TestControl { public static void main(String[] args) { // TODO Auto-generated method stub try { - PropertyConfigurator.configure(System.getProperty("user.dir") + File.separator + "log4j.conf"); + PropertyConfigurator.configure(RunService.APPLICATION_HOME + File.separator + "log4j.conf"); String taskid = args[0]; String batchcase = args[1]; TaskExecute task = GetServerApi.cgetTaskbyid(Integer.parseInt(taskid)); diff --git a/src/main/java/luckyclient/execution/OneCaseExecute.java b/src/main/java/luckyclient/execution/OneCaseExecute.java index bf4f3af..88fc3d5 100644 --- a/src/main/java/luckyclient/execution/OneCaseExecute.java +++ b/src/main/java/luckyclient/execution/OneCaseExecute.java @@ -15,6 +15,7 @@ import luckyclient.remote.entity.TaskExecute; import luckyclient.remote.entity.TaskScheduling; import luckyclient.utils.LogUtil; import luckyclient.utils.config.AppiumConfig; +import springboot.RunService; /** * ================================================================= @@ -31,7 +32,7 @@ public class OneCaseExecute extends TestControl { public static void main(String[] args) { try{ - PropertyConfigurator.configure(System.getProperty("user.dir")+ File.separator +"log4j.conf"); + PropertyConfigurator.configure(RunService.APPLICATION_HOME+ File.separator +"log4j.conf"); String taskId = args[0]; String caseId = args[1]; TaskExecute task = GetServerApi.cgetTaskbyid(Integer.parseInt(taskId)); diff --git a/src/main/java/luckyclient/execution/RunAutomationTest.java b/src/main/java/luckyclient/execution/RunAutomationTest.java index 80b33cf..0054f61 100644 --- a/src/main/java/luckyclient/execution/RunAutomationTest.java +++ b/src/main/java/luckyclient/execution/RunAutomationTest.java @@ -11,6 +11,7 @@ import luckyclient.remote.api.GetServerApi; import luckyclient.remote.entity.TaskExecute; import luckyclient.remote.entity.TaskScheduling; import luckyclient.utils.LogUtil; +import springboot.RunService; /** * ================================================================= @@ -26,7 +27,7 @@ import luckyclient.utils.LogUtil; public class RunAutomationTest extends TestControl { public static void main(String[] args) { try { - PropertyConfigurator.configure(System.getProperty("user.dir") + File.separator + "log4j.conf"); + PropertyConfigurator.configure(RunService.APPLICATION_HOME + File.separator + "log4j.conf"); String taskid = args[0]; TaskExecute task = GetServerApi.cgetTaskbyid(Integer.parseInt(taskid)); TaskScheduling taskScheduling = GetServerApi.cGetTaskSchedulingByTaskId(Integer.parseInt(taskid)); diff --git a/src/main/java/luckyclient/execution/WebDebugExecute.java b/src/main/java/luckyclient/execution/WebDebugExecute.java index 2290e13..37c5377 100644 --- a/src/main/java/luckyclient/execution/WebDebugExecute.java +++ b/src/main/java/luckyclient/execution/WebDebugExecute.java @@ -7,6 +7,7 @@ import org.apache.log4j.PropertyConfigurator; import luckyclient.execution.httpinterface.TestControl; import luckyclient.execution.httpinterface.WebTestCaseDebug; import luckyclient.utils.LogUtil; +import springboot.RunService; /** * ================================================================= @@ -23,7 +24,7 @@ public class WebDebugExecute extends TestControl{ public static void main(String[] args) { try { - PropertyConfigurator.configure(System.getProperty("user.dir") + File.separator + "log4j.conf"); + PropertyConfigurator.configure(RunService.APPLICATION_HOME + File.separator + "log4j.conf"); String caseIdStr = args[0]; String userIdStr = args[1]; WebTestCaseDebug.oneCaseDebug(caseIdStr, userIdStr); diff --git a/src/main/java/luckyclient/execution/appium/androidex/AndroidBaseAppium.java b/src/main/java/luckyclient/execution/appium/androidex/AndroidBaseAppium.java index cbb466c..fd18b93 100644 --- a/src/main/java/luckyclient/execution/appium/androidex/AndroidBaseAppium.java +++ b/src/main/java/luckyclient/execution/appium/androidex/AndroidBaseAppium.java @@ -17,6 +17,7 @@ import io.appium.java_client.android.AndroidTouchAction; import io.appium.java_client.touch.WaitOptions; import io.appium.java_client.touch.offset.PointOption; import luckyclient.utils.LogUtil; +import springboot.RunService; /** * ================================================================= @@ -35,7 +36,7 @@ public class AndroidBaseAppium { */ public static void screenShot(AndroidDriver appium, String imagname) { imagname = imagname + ".png"; - String relativelyPath = System.getProperty("user.dir"); + String relativelyPath = RunService.APPLICATION_HOME; String pngpath = relativelyPath + File.separator + "log" + File.separator + "ScreenShot" + File.separator + imagname; diff --git a/src/main/java/luckyclient/execution/appium/iosex/IosBaseAppium.java b/src/main/java/luckyclient/execution/appium/iosex/IosBaseAppium.java index 79c189e..13ab7e2 100644 --- a/src/main/java/luckyclient/execution/appium/iosex/IosBaseAppium.java +++ b/src/main/java/luckyclient/execution/appium/iosex/IosBaseAppium.java @@ -17,6 +17,7 @@ import io.appium.java_client.ios.IOSTouchAction; import io.appium.java_client.touch.WaitOptions; import io.appium.java_client.touch.offset.PointOption; import luckyclient.utils.LogUtil; +import springboot.RunService; /** * ================================================================= @@ -36,7 +37,7 @@ public class IosBaseAppium { */ public static void screenShot(IOSDriver appium, String imagname){ imagname = imagname + ".png"; - String relativelyPath = System.getProperty("user.dir"); + String relativelyPath = RunService.APPLICATION_HOME; String pngpath=relativelyPath +File.separator+ "log"+File.separator+"ScreenShot" +File.separator+ imagname; try { diff --git a/src/main/java/luckyclient/execution/webdriver/BaseWebDrive.java b/src/main/java/luckyclient/execution/webdriver/BaseWebDrive.java index 4724e9d..8748b8d 100644 --- a/src/main/java/luckyclient/execution/webdriver/BaseWebDrive.java +++ b/src/main/java/luckyclient/execution/webdriver/BaseWebDrive.java @@ -15,6 +15,7 @@ import org.openqa.selenium.remote.Augmenter; import cn.hutool.core.util.BooleanUtil; import luckyclient.utils.LogUtil; import luckyclient.utils.config.SysConfig; +import springboot.RunService; /** * ================================================================= @@ -35,7 +36,7 @@ public class BaseWebDrive { * @param imgname 图片名称 */ public static void webScreenShot(WebDriver driver, String imgname) { - String relativelyPath = System.getProperty("user.dir"); + String relativelyPath = RunService.APPLICATION_HOME; String pngpath=relativelyPath +File.separator+ "log"+File.separator+"ScreenShot" +File.separator+ imgname + ".png"; // 对远程系统进行截图 diff --git a/src/main/java/luckyclient/execution/webdriver/ocr/Ocr.java b/src/main/java/luckyclient/execution/webdriver/ocr/Ocr.java index be2b184..c1e2d40 100644 --- a/src/main/java/luckyclient/execution/webdriver/ocr/Ocr.java +++ b/src/main/java/luckyclient/execution/webdriver/ocr/Ocr.java @@ -17,6 +17,7 @@ import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.Augmenter; +import springboot.RunService; /** * ================================================================= @@ -33,15 +34,15 @@ public class Ocr { /** * 默认读取工程根目录下的文件 */ - private static final String readtextpath = System.getProperty("user.dir")+"\\CAPTCHA.txt"; + private static final String readtextpath = RunService.APPLICATION_HOME+"\\CAPTCHA.txt"; /** * 默认把截图放在工程根目录 */ - private static final String screenshotpath = System.getProperty("user.dir")+"\\CAPTCHA.png"; + private static final String screenshotpath = RunService.APPLICATION_HOME+"\\CAPTCHA.png"; /** * 批处理文件路径 */ - private static final String cmdpath = System.getProperty("user.dir"); + private static final String cmdpath = RunService.APPLICATION_HOME; /** * 读取生成的TXT文件中的验证码 diff --git a/src/main/java/luckyclient/netty/ClientHandler.java b/src/main/java/luckyclient/netty/ClientHandler.java index 48172aa..311b601 100644 --- a/src/main/java/luckyclient/netty/ClientHandler.java +++ b/src/main/java/luckyclient/netty/ClientHandler.java @@ -114,7 +114,7 @@ public class ClientHandler extends ChannelHandlerAdapter { sendMessage(re.toString()); } else if ("download".equals(json.get("method"))) { String loadpath = json.get("path").toString(); - String path = System.getProperty("user.dir") + loadpath; + String path = RunService.APPLICATION_HOME + loadpath; String fileName = json.get("fileName").toString(); //发出http请求下载文件 try { @@ -142,7 +142,7 @@ public class ClientHandler extends ChannelHandlerAdapter { @SuppressWarnings("unchecked") Map jsonparams = (Map) json.get("data"); String fileName = jsonparams.get("imgName").toString(); - String ctxPath = System.getProperty("user.dir") + File.separator + "log" + File.separator + "ScreenShot" + File.separator + fileName; + String ctxPath = RunService.APPLICATION_HOME + File.separator + "log" + File.separator + "ScreenShot" + File.separator + fileName; File file = new File(ctxPath); int start = Integer.parseInt(json.get("start").toString()); FileUploadFile fileUploadFile = new FileUploadFile(); diff --git a/src/main/java/luckyclient/utils/InvokeMethod.java b/src/main/java/luckyclient/utils/InvokeMethod.java index e75cc58..f822d0b 100644 --- a/src/main/java/luckyclient/utils/InvokeMethod.java +++ b/src/main/java/luckyclient/utils/InvokeMethod.java @@ -17,6 +17,7 @@ import luckyclient.remote.api.GetServerApi; import luckyclient.remote.entity.ProjectProtocolTemplate; import luckyclient.remote.entity.ProjectTemplateParams; import luckyclient.utils.httputils.HttpClientTools; +import springboot.RunService; /** * ================================================================= @@ -223,7 +224,7 @@ public class InvokeMethod { result = hct.sendURLPost(packagename, params, headmsg, ppt); break; case "getandsavefile": - String fileSavePath = System.getProperty("user.dir") + "\\HTTPSaveFile\\"; + String fileSavePath = RunService.APPLICATION_HOME + "\\HTTPSaveFile\\"; result = hct.sendGetAndSaveFile(packagename, params, fileSavePath, headmsg, ppt); break; case "httpurlget": diff --git a/src/main/java/springboot/HttpImpl.java b/src/main/java/springboot/HttpImpl.java index 52adb8e..baebd9d 100644 --- a/src/main/java/springboot/HttpImpl.java +++ b/src/main/java/springboot/HttpImpl.java @@ -69,7 +69,7 @@ public class HttpImpl { log.info("TaskId:{},SchedulingName:{},LoadPath:{}",runTaskEntity.getTaskId(),runTaskEntity.getSchedulingName(),runTaskEntity.getLoadPath()); try{ log.info("开始获取客户端驱动路径..."); - File file =new File(System.getProperty("user.dir")+runTaskEntity.getLoadPath()); + File file =new File(RunService.APPLICATION_HOME+runTaskEntity.getLoadPath()); log.info("客户端驱动路径:{}",file.getAbsolutePath()); if (!file .isDirectory()) { @@ -84,11 +84,11 @@ public class HttpImpl { log.info("启动任务模式测试程序...调度名称:【{}】 任务ID:【{}】",runTaskEntity.getSchedulingName(),runTaskEntity.getTaskId()); if(OS.startsWith("win")){ log.info("开始调起windows命令行窗口..."); - run.exec("cmd.exe /k start " + "task.cmd" +" "+ sbf.toString(), null,new File(System.getProperty("user.dir")+File.separator)); + run.exec("cmd.exe /k start " + "task.cmd" +" "+ sbf.toString(), null,new File(RunService.APPLICATION_HOME+File.separator)); log.info("调起windows命令行窗口完成..."); }else{ log.info("开始调起Linux命令脚本..."); - Process ps = Runtime.getRuntime().exec(System.getProperty("user.dir")+File.separator+"task.sh"+ " " +sbf.toString()); + Process ps = Runtime.getRuntime().exec(RunService.APPLICATION_HOME+File.separator+"task.sh"+ " " +sbf.toString()); ps.waitFor(); log.info("调起Linux命令脚本完成..."); } @@ -127,7 +127,7 @@ public class HttpImpl { log.info("批量测试用例:{}",batchCase); try{ log.info("开始获取客户端驱动路径..."); - File file =new File(System.getProperty("user.dir")+loadPath); + File file =new File(RunService.APPLICATION_HOME+loadPath); log.info("客户端驱动路径:{}",file.getAbsolutePath()); if (!file .isDirectory()) { @@ -143,11 +143,11 @@ public class HttpImpl { log.info("启动批量用例模式测试程序...测试项目:{} 任务ID:{}",projectName,taskId); if(OS.startsWith("win")){ log.info("开始调起windows命令行窗口..."); - run.exec("cmd.exe /k start " + "task_batch.cmd" + " " +sb.toString(), null,new File(System.getProperty("user.dir")+File.separator)); + run.exec("cmd.exe /k start " + "task_batch.cmd" + " " +sb.toString(), null,new File(RunService.APPLICATION_HOME+File.separator)); log.info("调起windows命令行窗口完成..."); }else{ log.info("开始调起Linux命令脚本..."); - Process ps = Runtime.getRuntime().exec(System.getProperty("user.dir")+File.separator+"task_batch.sh"+ " " +sb.toString()); + Process ps = Runtime.getRuntime().exec(RunService.APPLICATION_HOME+File.separator+"task_batch.sh"+ " " +sb.toString()); ps.waitFor(); log.info("调起Linux命令脚本完成..."); } @@ -180,7 +180,7 @@ public class HttpImpl { WebDebugCaseEntity webDebugCaseEntity = JSONObject.parseObject(sbd.toString(), WebDebugCaseEntity.class); log.info("Web端调试用例ID:{} 发起人ID:{}",webDebugCaseEntity.getCaseId(),webDebugCaseEntity.getUserId()); try{ - File file =new File(System.getProperty("user.dir")+webDebugCaseEntity.getLoadpath()); + File file =new File(RunService.APPLICATION_HOME+webDebugCaseEntity.getLoadpath()); if (!file .isDirectory()) { log.warn("客户端测试驱动桩路径不存在,请检查【{}】",file.getPath()); @@ -192,9 +192,9 @@ public class HttpImpl { sb.append(webDebugCaseEntity.getUserId()).append(" "); sb.append(webDebugCaseEntity.getLoadpath()); if(OS.startsWith("win")){ - run.exec("cmd.exe /k start " + "web_debugcase.cmd" + " " +sb.toString(), null,new File(System.getProperty("user.dir")+File.separator)); + run.exec("cmd.exe /k start " + "web_debugcase.cmd" + " " +sb.toString(), null,new File(RunService.APPLICATION_HOME+File.separator)); }else{ - Process ps = Runtime.getRuntime().exec(System.getProperty("user.dir")+File.separator+"web_debugcase.sh"+ " " +sb.toString()); + Process ps = Runtime.getRuntime().exec(RunService.APPLICATION_HOME+File.separator+"web_debugcase.sh"+ " " +sb.toString()); ps.waitFor(); } } catch (Exception e) { @@ -213,7 +213,7 @@ public class HttpImpl { @GetMapping("/getLogdDetail") private String getLogdDetail(HttpServletRequest req) { String fileName=req.getParameter("filename"); - String ctxPath = System.getProperty("user.dir")+File.separator+"log"; + String ctxPath = RunService.APPLICATION_HOME+File.separator+"log"; String downLoadPath = ctxPath +File.separator+ fileName; String str; @@ -252,7 +252,7 @@ public class HttpImpl { @GetMapping("/getLogImg") private byte[] getLogImg(HttpServletRequest req) { String imgName=req.getParameter("imgName"); - String ctxPath = System.getProperty("user.dir")+File.separator+"log"+File.separator+"ScreenShot"; + String ctxPath = RunService.APPLICATION_HOME+File.separator+"log"+File.separator+"ScreenShot"; String downLoadPath = ctxPath+File.separator+imgName; byte[] b = null; try { @@ -290,7 +290,7 @@ public class HttpImpl { String name = jarfile.getOriginalFilename(); String loadpath = req.getParameter("loadpath"); - String path = System.getProperty("user.dir")+loadpath; + String path = RunService.APPLICATION_HOME+loadpath; if (!new File(path) .isDirectory()) { log.warn("客户端测试驱动桩路径不存在,请检查【{}】",path); diff --git a/src/main/java/springboot/RunService.java b/src/main/java/springboot/RunService.java index 1a3ed24..8379d3c 100644 --- a/src/main/java/springboot/RunService.java +++ b/src/main/java/springboot/RunService.java @@ -12,6 +12,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import cn.hutool.core.util.BooleanUtil; import luckyclient.netty.NettyClient; import luckyclient.utils.config.SysConfig; +import org.springframework.boot.system.ApplicationHome; import org.springframework.context.annotation.EnableAspectJAutoProxy; /** @@ -30,12 +31,13 @@ public class RunService { * 如果IDEA工具上运行时出现乱码,请设置VM options,添加-Dfile.encoding=GBK即可 * */ + public static final String APPLICATION_HOME = new ApplicationHome(RunService.class).getSource().getAbsolutePath(); private static final Logger log = LoggerFactory.getLogger(RunService.class); private static final Boolean NETTY_MODEL= BooleanUtil.toBoolean(SysConfig.getConfiguration().getProperty("netty.model")); public static String CLIENT_IP = ""; public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub - PropertyConfigurator.configure(System.getProperty("user.dir") + File.separator +"bootlog4j.conf"); + PropertyConfigurator.configure(APPLICATION_HOME + File.separator +"bootlog4j.conf"); SpringApplication.run(RunService.class, args); try { CLIENT_IP = InetAddress.getLocalHost().getHostAddress();