Jelajahi Sumber

1. 支持执行自定义脚本
2. 自动生成traceID

LiHaoyu 5 tahun lalu
induk
melakukan
dccd4d6607

+ 2 - 1
configs/ignore.conf

@@ -29,4 +29,5 @@ text#不同意
 text#更新
 text#下载
 text#安装
-text#退出
+text#退出
+text#不使用

+ 4 - 10
src/main/java/net/mooctest/www/android_auto_test/controller/AutoTestController.java

@@ -1,16 +1,13 @@
 package net.mooctest.www.android_auto_test.controller;
 
 import net.mooctest.www.android_auto_test.common.exceptions.LackParamsException;
+import net.mooctest.www.android_auto_test.utils.SnowFlake;
 import net.mooctest.www.android_auto_test.vo.TraceMetaInfo;
 import net.mooctest.www.android_auto_test.vo.TraceStatusResult;
 import net.mooctest.www.android_auto_test.services.AutoTestService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
 
 /**
  * @author henrylee
@@ -23,15 +20,12 @@ public class AutoTestController {
 
     @RequestMapping(value = "/api/v1/runTest", method = RequestMethod.POST)
     public TraceStatusResult runTest(@RequestBody TraceMetaInfo traceInfo) throws LackParamsException {
-        List<String> leakParams = new ArrayList<>();
         if (traceInfo.getTraceId() == null){
-            leakParams.add("traceId");
+            long genId = SnowFlake.getInstance().nextId();
+            traceInfo.setTraceId(String.valueOf(genId));
         }
         if (traceInfo.getDownloadUrl() == null){
-            leakParams.add("downloadUrl");
-        }
-        if (leakParams.size() != 0){
-            throw new LackParamsException("缺少参数:" + Arrays.toString(leakParams.toArray()));
+            throw new LackParamsException("缺少参数: downloadUrl");
         }
         return autoTestService.executeAutoTestTask(traceInfo);
     }

+ 2 - 0
src/main/java/net/mooctest/www/android_auto_test/services/ApkService.java

@@ -21,4 +21,6 @@ public interface ApkService {
     String downloadApk(String downloadUrl, String traceId);
     
     void saveApkInfo(ApkInfo apkInfo, String apkPath, String traceId);
+
+    String downloadCustomScript(String url);
 }

+ 17 - 0
src/main/java/net/mooctest/www/android_auto_test/services/Impl/ApkServiceImpl.java

@@ -104,6 +104,23 @@ public class ApkServiceImpl implements ApkService {
         }
     }
 
+    @Override
+    public String downloadCustomScript(String url) {
+        if (url == null || !url.endsWith(".java")) {
+            return null;
+        }
+        String[] temp = url.split("/");
+        String fileName = temp[temp.length-1];
+        String path = "scripts" + File.separator + fileName;
+        try {
+            FileUtils.copyURLToFile(new URL(url), new File(path));
+            return fileName;
+        }catch (Exception ignored){
+
+        }
+        return null;
+    }
+
     private String saveIcon(String apkPath, String iconPath, String traceId){
         try {
             ZipFile zFile = null;

+ 4 - 0
src/main/java/net/mooctest/www/android_auto_test/services/Impl/AutoTestServiceImpl.java

@@ -74,12 +74,15 @@ public class AutoTestServiceImpl implements AutoTestService {
             traceService.updateTraceStatue(traceId, TraceStatus.INIT);
             String filePath;
             ApkInfo apkInfo;
+            String customScriptName;
             try {
                 PrintUtil.print("Start download apkFile, TraceId: " + traceId, TAG);
                 filePath = apkService.downloadApk(downloadUrl, traceId);
                 PrintUtil.print("Start parse apkFile, TraceId: " + traceId, TAG);
                 apkInfo = apkService.parseAPK(filePath, traceId);
                 apkService.saveApkInfo(apkInfo, filePath, traceId);
+                PrintUtil.print("Try to download custom script: " + traceId, TAG);
+                customScriptName = apkService.downloadCustomScript(traceInfo.getCustomScriptUlr());
             } catch (ApkDownloadException e){
                 PrintUtil.print(String.format("Download failed, trace %s end.", traceId), TAG);
                 traceService.updateTraceStatue(traceId, TraceStatus.DOWNLOAD_FAILED);
@@ -118,6 +121,7 @@ public class AutoTestServiceImpl implements AutoTestService {
                     PrintUtil.printErr("开始Device任务, 这个路径不会被触发。", TAG, device.getUdid());
                 }
                 CoverageTest coverageTest = new CoverageTest(apkInfo, filePath, device, traceId);
+                coverageTest.setCustomScriptName(customScriptName);
                 coverageTest.setName(Consts.AUTO_TEST_THREAD_NAME_PREFIX + device.getUdid());
                 coverageTest.start();
                 oneTraceTasks.add(coverageTest);

+ 13 - 4
src/main/java/net/mooctest/www/android_auto_test/utils/CoverageTest.java

@@ -13,6 +13,7 @@ import net.mooctest.www.android_auto_test.common.constant.Consts;
 import net.mooctest.www.android_auto_test.common.exceptions.AppiumDriverInitException;
 import net.mooctest.www.android_auto_test.common.exceptions.TraceTimeoutException;
 import net.mooctest.www.android_auto_test.common.constant.enums.DeviceRunningStatus;
+import net.mooctest.www.android_auto_test.groovy.GroovyExecutor;
 import net.mooctest.www.android_auto_test.models.Device;
 import net.mooctest.www.android_auto_test.services.DeviceService;
 import org.openqa.selenium.NoSuchSessionException;
@@ -62,6 +63,7 @@ public class CoverageTest extends Thread{
     private AbstractBaseScript script;
     private DeviceService deviceService;
     private String coldStartTime = "unknown";
+    private String customScriptName = null;
 
     public CoverageTest(ApkInfo apkInfo, String apkPath, Device device, String traceId){
         deviceService = (DeviceService) BeanFactory.getBean(DeviceService.class);
@@ -76,6 +78,10 @@ public class CoverageTest extends Thread{
         DeviceUtil.saveDeviceInfo(traceId, udid);
     }
 
+    public void setCustomScriptName(String name){
+        customScriptName = name;
+    }
+
     @Override
     public void run() {
         deviceService.updateDeviceRunningStatus(traceId, udid, DeviceRunningStatus.PREPARE);
@@ -290,10 +296,13 @@ public class CoverageTest extends Thread{
     }
 
     private void executeScript(){
-        PrintUtil.print("use the default script " + udid, TAG, udid);
-//        script = new DemoScript(udid, driver, apkInfo, traceId);
-        script = new DefaultScript(udid, driver, apkInfo, traceId);
-        script.runAndroidAutoTest();
+        if (customScriptName == null) {
+            PrintUtil.print("use the default script " + udid, TAG, udid);
+            script = new DefaultScript(udid, driver, apkInfo, traceId);
+            script.runAndroidAutoTest();
+        }else {
+            GroovyExecutor.executeMethod(customScriptName, "test", new Object[]{driver});
+        }
     }
 
     private void install() {

+ 115 - 0
src/main/java/net/mooctest/www/android_auto_test/utils/SnowFlake.java

@@ -0,0 +1,115 @@
+package net.mooctest.www.android_auto_test.utils;
+
+/**
+ * twitter的snowflake算法 -- java实现
+ *
+ * @author beyond
+ * @date 2016/11/26
+ */
+public class SnowFlake {
+
+    /**
+     * 起始的时间戳
+     */
+    private final static long START_STMP = 1480166465631L;
+
+    /**
+     * 每一部分占用的位数
+     */
+    //序列号占用的位数
+    private final static long SEQUENCE_BIT = 12;
+    //机器标识占用的位数
+    private final static long MACHINE_BIT = 5;
+    //数据中心占用的位数
+    private final static long DATACENTER_BIT = 5;
+
+    /**
+     * 每一部分的最大值
+     */
+    private final static long MAX_DATACENTER_NUM = -1L ^ (-1L << DATACENTER_BIT);
+    private final static long MAX_MACHINE_NUM = -1L ^ (-1L << MACHINE_BIT);
+    private final static long MAX_SEQUENCE = -1L ^ (-1L << SEQUENCE_BIT);
+
+    /**
+     * 每一部分向左的位移
+     */
+    private final static long MACHINE_LEFT = SEQUENCE_BIT;
+    private final static long DATACENTER_LEFT = SEQUENCE_BIT + MACHINE_BIT;
+    private final static long TIMESTMP_LEFT = DATACENTER_LEFT + DATACENTER_BIT;
+
+    //数据中心
+    private long datacenterId;
+    //机器标识
+    private long machineId;
+    //序列号
+    private long sequence = 0L;
+    //上一次时间戳
+    private long lastStmp = -1L;
+
+    private static SnowFlake singleton;
+
+    public static SnowFlake getInstance(){
+        if (singleton == null) {
+            synchronized (SnowFlake.class) {
+                if (singleton == null) {
+                    singleton = new SnowFlake(2,3);
+                }
+            }
+        }
+        return singleton;
+    }
+
+    private SnowFlake(long datacenterId, long machineId) {
+        if (datacenterId > MAX_DATACENTER_NUM || datacenterId < 0) {
+            throw new IllegalArgumentException("datacenterId can't be greater than MAX_DATACENTER_NUM or less than 0");
+        }
+        if (machineId > MAX_MACHINE_NUM || machineId < 0) {
+            throw new IllegalArgumentException("machineId can't be greater than MAX_MACHINE_NUM or less than 0");
+        }
+        this.datacenterId = datacenterId;
+        this.machineId = machineId;
+    }
+
+    /**
+     * 产生下一个ID
+     *
+     * @return
+     */
+    public synchronized long nextId() {
+        long currStmp = getNewstmp();
+        if (currStmp < lastStmp) {
+            throw new RuntimeException("Clock moved backwards.  Refusing to generate id");
+        }
+
+        if (currStmp == lastStmp) {
+            //相同毫秒内,序列号自增
+            sequence = (sequence + 1) & MAX_SEQUENCE;
+            //同一毫秒的序列数已经达到最大
+            if (sequence == 0L) {
+                currStmp = getNextMill();
+            }
+        } else {
+            //不同毫秒内,序列号置为0
+            sequence = 0L;
+        }
+
+        lastStmp = currStmp;
+
+        return (currStmp - START_STMP) << TIMESTMP_LEFT //时间戳部分
+                | datacenterId << DATACENTER_LEFT       //数据中心部分
+                | machineId << MACHINE_LEFT             //机器标识部分
+                | sequence;                             //序列号部分
+    }
+
+    private long getNextMill() {
+        long mill = getNewstmp();
+        while (mill <= lastStmp) {
+            mill = getNewstmp();
+        }
+        return mill;
+    }
+
+    private long getNewstmp() {
+        return System.currentTimeMillis();
+    }
+}

+ 1 - 0
src/main/java/net/mooctest/www/android_auto_test/vo/TraceMetaInfo.java

@@ -16,4 +16,5 @@ public class TraceMetaInfo {
     private boolean needGenerateReport = false;
     private String reportType = "BugReport";
     private boolean needCrowdSourcedAnalysis = false;
+    private String customScriptUlr = null;
 }