Prechádzať zdrojové kódy

接入MergeScript算法,运行成功

xuyr 4 rokov pred
rodič
commit
1ab038b5de

+ 79 - 0
OperationFlow/jlb/res.txt

@@ -0,0 +1,79 @@
+activity:LoginActivity
+behavior:sendKeys
+arg:[张三]
+index:-1
+id:com.yao.club:id/et_username
+text:请输入会员名
+className:android.widget.EditText
+desc:
+packageName:
+enabled:true
+checkable:false
+checked:false
+clickable:true
+focusable:true
+focused:true
+longClickable:true
+scrollable:false
+selected:false
+displayed:true
+
+activity:LoginActivity
+behavior:sendKeys
+arg:[123]
+index:-1
+id:com.yao.club:id/et_pwd
+text:
+className:android.widget.EditText
+desc:
+packageName:
+enabled:true
+checkable:false
+checked:false
+clickable:true
+focusable:true
+focused:false
+longClickable:true
+scrollable:false
+selected:false
+displayed:true
+
+activity:LoginActivity
+behavior:click
+arg:
+index:-1
+id:com.yao.club:id/btn_login
+text:登录
+className:android.widget.Button
+desc:
+packageName:
+enabled:true
+checkable:false
+checked:false
+clickable:true
+focusable:true
+focused:false
+longClickable:false
+scrollable:false
+selected:false
+displayed:true
+
+activity:NewActivity
+behavior:click
+arg:
+index:-1
+id:com.yao.club:id/txt_surplus_money
+text:1000.00
+className:android.widget.TextView
+desc:
+packageName:
+enabled:true
+checkable:false
+checked:false
+clickable:true
+focusable:true
+focused:false
+longClickable:false
+scrollable:false
+selected:false
+displayed:true

+ 0 - 0
lib/linuxaapt → lib/aapt


+ 101 - 47
src/main/java/net/mooctest/www/android_auto_test/Scripts/MergeScript.java

@@ -10,12 +10,18 @@ import io.appium.java_client.touch.WaitOptions;
 import io.appium.java_client.touch.offset.PointOption;
 import net.mooctest.www.android_auto_test.utils.*;
 import net.mooctest.www.android_auto_test.models.*;
+import org.apache.commons.io.FileUtils;
+import org.openqa.selenium.OutputType;
+import org.openqa.selenium.TakesScreenshot;
+import org.openqa.selenium.WebDriverException;
 import org.openqa.selenium.WebElement;
 
 import java.io.*;
 import java.time.Duration;
 import java.util.*;
 
+import static java.nio.charset.StandardCharsets.UTF_8;
+
 public class MergeScript extends AbstractBaseScript {
 
     public static final String TAG = Thread.currentThread() .getStackTrace()[1].getClassName();
@@ -50,7 +56,6 @@ public class MergeScript extends AbstractBaseScript {
     private int taskId;
     private String appPackage;
     private String apkName;
-    BufferedWriter mylogWriter;
     List<String> fatherComponentList = new ArrayList<String>();
     List<String> ignoreIfIdEquals = new ArrayList<String>();
     List<String> ignoreIfIdContains = new ArrayList<String>();
@@ -67,6 +72,7 @@ public class MergeScript extends AbstractBaseScript {
         this.udid = udid;
         this.driver = driver;
         this.apkInfo = apkInfo;
+        this.appPackage = apkInfo.getPackageName();
     }
     private void initWriter(File testScript, File testAction, File myTestLog){
         try {
@@ -180,7 +186,7 @@ public class MergeScript extends AbstractBaseScript {
         //从res文件里读内容
         //TODO地址要改
         try {
-            resList = widgetMergeTools.getResList("D:\\myWorkSpace\\test\\test");
+            resList = widgetMergeTools.getResList("OperationFlow"+File.separator);
             widgetMergeTools.resList2Pic(resList);
             nodeList = widgetMergeTools.getAllPath(resList);
         }catch(Exception e){
@@ -244,7 +250,7 @@ public class MergeScript extends AbstractBaseScript {
         if(outOfAppPackageFlag) {
             if (outAppSemapher == 0) {
                 //此时可能是跳出应用,reset一次尝试
-                PrintUtil.print("Perhaps not in app, reset one time", TAG, udid, mylogWriter, PrintUtil.ANSI_BLUE);
+                PrintUtil.print("Perhaps not in app, reset one time", TAG, udid, myTestLogWriter, PrintUtil.ANSI_BLUE);
                 driver.resetApp();
                 try {
                     Thread.sleep(3000);
@@ -256,8 +262,8 @@ public class MergeScript extends AbstractBaseScript {
             outOfAppPackageFlag=false;
         }
         if(outOfAppPackageFlag){
-            PrintUtil.print("Have tried 2 times, still not in appPackage", TAG, udid, mylogWriter, PrintUtil.ANSI_BLUE);
-            PrintUtil.print("Change isEnd to true", TAG, udid, mylogWriter, PrintUtil.ANSI_BLUE);
+            PrintUtil.print("Have tried 2 times, still not in appPackage", TAG, udid, myTestLogWriter, PrintUtil.ANSI_BLUE);
+            PrintUtil.print("Change isEnd to true", TAG, udid, myTestLogWriter, PrintUtil.ANSI_BLUE);
             stopTestFlag = true;
         }else if(i!=0&&driver.currentActivity().equals(fatherActivity)){
             //记录到达的activity
@@ -289,7 +295,7 @@ public class MergeScript extends AbstractBaseScript {
         if(currentActivityIndex >= 0){
             //若当前访问页面的activity等于activityList中某一activity对象的activityName,说明此子页面之前出现过
             if(fatherActivity!=null&&!activityList.get(currentActivityIndex).getFatherActivity().equals(fatherActivity)){
-                PrintUtil.print("Previous Activity is " + fatherActivity + ", but " + currentActivity + "'s father is " + activityList.get(currentActivityIndex).getFatherActivity() + ", change father", TAG, udid, mylogWriter, PrintUtil.ANSI_GREEN);
+                PrintUtil.print("Previous Activity is " + fatherActivity + ", but " + currentActivity + "'s father is " + activityList.get(currentActivityIndex).getFatherActivity() + ", change father", TAG, udid, myTestLogWriter, PrintUtil.ANSI_GREEN);
                 //变更父子关系,设置该子页面的fatherActivity为最近一次跳转过来的那个Activity
                 activityList.get(currentActivityIndex).setFatherActivity(fatherActivity);
                 fatherNode.add(currentNode);
@@ -306,6 +312,8 @@ public class MergeScript extends AbstractBaseScript {
             fatherNode.add(currentNode);
             pageHaveBeenFlag = false;
             threadSleep(3);
+            takeScreenShotAndSavePageImg();
+            savePageXml();
             //由于该页面不在列表activityList中,新建一个将其加入
             activityList.add(new Activity(currentActivity, fatherActivity, currentPageHash, comList));
             //currentActivityIndex是列表activityList的指针,加入了新元素时指向列表activityList末尾的新元素
@@ -315,13 +323,13 @@ public class MergeScript extends AbstractBaseScript {
         fatherActivity = currentActivity;
         Activity activity4Test = activityList.get(currentActivityIndex);
         if(activity4Test.isDone()){
-            PrintUtil.print("Going to return because this page is over", TAG, udid, mylogWriter, PrintUtil.ANSI_BLUE);
+            PrintUtil.print("Going to return because this page is over", TAG, udid, myTestLogWriter, PrintUtil.ANSI_BLUE);
             returnPrevActivity();
             return;
         }else{
             //遍历该页面所有控件,尝试定位控件并进行操作
             for(int comIndex = 0;comIndex < comList.size();comIndex++){
-                PrintUtil.print(currentActivity + "'s index is " + comIndex + "/" + comList.size(), TAG, udid, mylogWriter, PrintUtil.ANSI_BLUE);
+                PrintUtil.print(currentActivity + "'s index is " + comIndex + "/" + comList.size(), TAG, udid, myTestLogWriter, PrintUtil.ANSI_BLUE);
                 Component component = comList.get(comIndex);
                 WebElement element4Test = null;
                 if(judgeTestedComponent(component) || judgeIgnoreComponent(component)) {
@@ -333,7 +341,7 @@ public class MergeScript extends AbstractBaseScript {
                 if(component.getLocator() != null){
                     try{//定位控件
                         String locator = component.getLocator();
-                        PrintUtil.print("Try to locate " + locator, TAG, udid, mylogWriter, PrintUtil.ANSI_BLUE);
+                        PrintUtil.print("Try to locate " + locator, TAG, udid, myTestLogWriter, PrintUtil.ANSI_BLUE);
                         if(locator.startsWith("//")) {
                             element4Test = driver.findElementByXPath(locator);
                         }
@@ -341,7 +349,7 @@ public class MergeScript extends AbstractBaseScript {
                             element4Test = driver.findElementById(locator);
                         }
                     }catch(Exception exception){	//定位失败,尝试定位该页面下一个控件
-                        PrintUtil.print("Can't locate component " + component.getLocator(), TAG, udid, mylogWriter, PrintUtil.ANSI_RED);
+                        PrintUtil.print("Can't locate component " + component.getLocator(), TAG, udid, myTestLogWriter, PrintUtil.ANSI_RED);
                         continue;
                     }
                     comList.get(comIndex).setHasBeenTested(true);
@@ -349,7 +357,7 @@ public class MergeScript extends AbstractBaseScript {
                     if(judgeFatherComponent(component, currentActivity)){
                         executeClick(component, element4Test);
                         comList = handleFatherComponent(comList, component, i);
-                        PrintUtil.print("After set father component", TAG, udid, mylogWriter, PrintUtil.ANSI_GREEN);
+                        PrintUtil.print("After set father component", TAG, udid, myTestLogWriter, PrintUtil.ANSI_GREEN);
                         printComponentList(comList);
                         executeKeyEvent("Click Return button because handled father components", AndroidKey.BACK);
                         comIndex = -1;
@@ -376,6 +384,7 @@ public class MergeScript extends AbstractBaseScript {
                                 //记录到达的activity
                                 recordActivity(driver.currentActivity());
                                 currentNode.add(videoActivityNode);
+                                takeScreenShotAndSavePageImg();
                                 hasTakenScreenshot = true;
                             }
                             executeKeyEvent("Click Return button cause only enter VideoPlayerActivity", AndroidKey.BACK);
@@ -398,10 +407,10 @@ public class MergeScript extends AbstractBaseScript {
                         int pageHashAfterClick = driver.getPageSource().hashCode();
                         if(pageHashAfterClick == currentPageHash){
                             //点击控件后的页面的pageSource的hash值没变,页面没有任何改变
-                            PrintUtil.print("The page has not changed", TAG, udid, mylogWriter, PrintUtil.ANSI_GREEN);
+                            PrintUtil.print("The page has not changed", TAG, udid, myTestLogWriter, PrintUtil.ANSI_GREEN);
                         }else if(activityAfterClick.equals(currentActivity)){
                             //页面的pageSource的hash值变了,但是activity没变
-                            PrintUtil.print("The UI components have changed", TAG, udid, mylogWriter, PrintUtil.ANSI_GREEN);
+                            PrintUtil.print("The UI components have changed", TAG, udid, myTestLogWriter, PrintUtil.ANSI_GREEN);
                             List<Component> comListAfterClick = getCurrentClickableComList(i, true);
                             if(comList.hashCode() != comListAfterClick.hashCode()) {
                                 //若控件列表的hash值不相同
@@ -412,26 +421,26 @@ public class MergeScript extends AbstractBaseScript {
                                 activityList.get(currentActivityIndex).setComList(comList);
                                 activityList.get(currentActivityIndex).setHash(pageHashAfterClick);
                                 if(sameComponentFlag){
-                                    PrintUtil.print("Update the activity because the different components are less than half", TAG, udid, mylogWriter, PrintUtil.ANSI_BLUE);
+                                    PrintUtil.print("Update the activity because the different components are less than half", TAG, udid, myTestLogWriter, PrintUtil.ANSI_BLUE);
                                 }else{
                                     //不相同控件个数过半,认为是进入了此Activity的另一个页面(更新页面继续进行遍历)
-                                    PrintUtil.print("Reset this activity's index, beacause the different components are more than half", TAG, udid, mylogWriter, PrintUtil.ANSI_BLUE);
+                                    PrintUtil.print("Reset this activity's index, beacause the different components are more than half", TAG, udid, myTestLogWriter, PrintUtil.ANSI_BLUE);
                                 }
                                 if(checkStopFlag()) {
                                     return;
                                 }
                             }else {
-                                PrintUtil.print("The component list hash code has not changed, continue to test current comList", TAG, udid, mylogWriter, PrintUtil.ANSI_BLUE);
+                                PrintUtil.print("The component list hash code has not changed, continue to test current comList", TAG, udid, myTestLogWriter, PrintUtil.ANSI_BLUE);
                             }
                             printComponentList(comList);
                         }else{
                             //页面的PageSource的hash值和Activity都变了
-                            PrintUtil.print("The page has changed", TAG, udid, mylogWriter, PrintUtil.ANSI_GREEN);
-                            PrintUtil.print("Current activity is " + activityAfterClick, TAG, udid, mylogWriter, PrintUtil.ANSI_GREEN);
+                            PrintUtil.print("The page has changed", TAG, udid, myTestLogWriter, PrintUtil.ANSI_GREEN);
+                            PrintUtil.print("Current activity is " + activityAfterClick, TAG, udid, myTestLogWriter, PrintUtil.ANSI_GREEN);
                             //页面的pageSource的hash值、activity都变了
                             if(activityAfterClick.equals(fatherOfCurrentActivity)){
                                 //如果改变后的当前页面为之前页面的父页面,返回(即不认为是开始了一次新的一轮深搜)
-                                PrintUtil.print("I'm going to return because of my dad" + "\r\n", TAG, udid, mylogWriter, PrintUtil.ANSI_BLUE);
+                                PrintUtil.print("I'm going to return because of my dad" + "\r\n", TAG, udid, myTestLogWriter, PrintUtil.ANSI_BLUE);
                                 return;
                             }else{
                                 //页面发生改变,进入的是子页面
@@ -461,13 +470,13 @@ public class MergeScript extends AbstractBaseScript {
                                 if(checkStopFlag()) {
                                     return;
                                 }
-                                PrintUtil.print("Has returned", TAG, udid, mylogWriter, PrintUtil.ANSI_BLUE);
+                                PrintUtil.print("Has returned", TAG, udid, myTestLogWriter, PrintUtil.ANSI_BLUE);
                                 //返回之后马上检查是否有android消息弹窗出现,若有(这种情况一般是返回了MainActivity)就点击取消按钮
                                 //并检查是否在AppPackage中,不在就结束遍历
                                 outOfAppPackageFlag = handleAndroidMsgAndCheckPkg();
                                 if(outOfAppPackageFlag) {
                                     //如果返回后的页面不在AppPackage中
-                                    PrintUtil.print("Change isEnd to true because not in AppPackage", TAG, udid, mylogWriter, PrintUtil.ANSI_BLUE);
+                                    PrintUtil.print("Change isEnd to true because not in AppPackage", TAG, udid, myTestLogWriter, PrintUtil.ANSI_BLUE);
                                     stopTestFlag = true;
                                     return;
                                 }
@@ -477,7 +486,7 @@ public class MergeScript extends AbstractBaseScript {
                                     fatherActivity = driver.currentActivity();
                                     //记录到达的activity
                                     recordActivity(driver.currentActivity());
-                                    PrintUtil.print("Current activity is " + fatherActivity, TAG, udid, mylogWriter, PrintUtil.ANSI_BLUE);
+                                    PrintUtil.print("Current activity is " + fatherActivity, TAG, udid, myTestLogWriter, PrintUtil.ANSI_BLUE);
                                 }catch(Exception e){
                                     PrintUtil.printException(TAG, udid, e);
                                     return;
@@ -485,7 +494,7 @@ public class MergeScript extends AbstractBaseScript {
                                 List<Component> comListAfterReturn = getCurrentClickableComList(i, true);
                                 if(comList.hashCode() != comListAfterReturn.hashCode()){
                                     //此时的页面和之前的页面有区别,就用现在的页面更新了之前页面comList,且去除先前页面已有且测试过的控件,继续遍历这个页面剩下的控件
-                                    PrintUtil.print("Update the activity's component , because current comList is different with before", TAG, udid, mylogWriter, PrintUtil.ANSI_BLUE);
+                                    PrintUtil.print("Update the activity's component , because current comList is different with before", TAG, udid, myTestLogWriter, PrintUtil.ANSI_BLUE);
                                     comList = setTestedComInList(comList, comListAfterReturn);
                                     comIndex = -1;
                                     activityList.get(currentActivityIndex).setComList(comListAfterReturn);
@@ -494,19 +503,64 @@ public class MergeScript extends AbstractBaseScript {
                                     recordActivity(driver.currentActivity());
                                     printComponentList(comList);
                                 }
-                                PrintUtil.print("After return, current index is " + comIndex + "/" + comList.size(), TAG, udid, mylogWriter, PrintUtil.ANSI_BLUE);
+                                PrintUtil.print("After return, current index is " + comIndex + "/" + comList.size(), TAG, udid, myTestLogWriter, PrintUtil.ANSI_BLUE);
                             }
                         }
                     }
                 }
             }
             //本页面所有控件已遍历完
-            PrintUtil.print("Going to return beacuse this page has done" + "\r\n", TAG, udid, mylogWriter, PrintUtil.ANSI_BLUE);
+            PrintUtil.print("Going to return beacuse this page has done" + "\r\n", TAG, udid, myTestLogWriter, PrintUtil.ANSI_BLUE);
             activityList.get(currentActivityIndex).setDone(true);
             returnPrevActivity();
             return;
         }
     }
+
+    private void takeScreenShotAndSavePageImg(){
+        int count = 0;
+        FileReader fr;
+        File newFile = new File(AddressUtil.getPageImgPath(traceId, udid, driver.currentActivity()));
+        while (count < 10){
+            try {
+                File screenShotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
+                FileUtils.copyFile(screenShotFile, newFile);
+                fr = new FileReader(newFile);
+                if (fr.read() != -1){
+                    PrintUtil.print("Screenshot saved successful.", TAG, udid, myTestLogWriter, PrintUtil.ANSI_GREEN);
+                    return;
+                }
+            } catch (IOException e) {
+                PrintUtil.print("Screenshot error ", TAG, udid, myTestLogWriter, PrintUtil.ANSI_RED);
+                PrintUtil.printException(TAG, udid, e);
+                e.printStackTrace();
+            } catch (WebDriverException e){
+                PrintUtil.print(String.format("Cannot take screenshot on this activity, %s.", driver.currentActivity()), TAG, udid, myTestLogWriter, PrintUtil.ANSI_RED);
+                return;
+            } finally {
+                count++;
+            }
+        }
+        PrintUtil.print("Screenshot saved failed.", TAG, udid, myTestLogWriter, PrintUtil.ANSI_RED);
+    }
+    private void savePageXml(){
+        String activityName = driver.currentActivity();
+        String xmlPath = AddressUtil.getPageXmlPath(traceId, udid, activityName);
+        String pageXml = driver.getPageSource();
+        FileOutputStream writerStream;
+        try {
+            writerStream = new FileOutputStream(xmlPath);
+            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(writerStream, UTF_8));
+            writer.write(pageXml);
+            writer.close();
+        } catch (IOException e) {
+            PrintUtil.printException(TAG, udid, e);
+            e.printStackTrace();
+        }
+    }
+
+
+
     //测试主逻辑
     private void loadConfiguration(){
         readIgnoreComponent();
@@ -539,7 +593,7 @@ public class MergeScript extends AbstractBaseScript {
             locateSuccessfulFlag = true;
             if(labels[i].contains("android")){
                 try{
-                    PrintUtil.print("Click button1 for AndroidAlert", TAG, udid, mylogWriter, PrintUtil.ANSI_GREEN);
+                    PrintUtil.print("Click button1 for AndroidAlert", TAG, udid, myTestLogWriter, PrintUtil.ANSI_GREEN);
                     String activityBeforeAction = driver.currentActivity();
                     //记录到达的activity
                     recordActivity(activityBeforeAction);
@@ -560,11 +614,11 @@ public class MergeScript extends AbstractBaseScript {
                     }
                 } catch (Exception e) {
                     locateSuccessfulFlag = false;
-                    PrintUtil.print("Can't locate AndroidAlert compotent", TAG, udid, mylogWriter, PrintUtil.ANSI_RED);
+                    PrintUtil.print("Can't locate AndroidAlert compotent", TAG, udid, myTestLogWriter, PrintUtil.ANSI_RED);
                 }
             }else{
                 try{
-                    PrintUtil.print("Click allow button for AndroidAlert", TAG, udid, mylogWriter, PrintUtil.ANSI_GREEN);
+                    PrintUtil.print("Click allow button for AndroidAlert", TAG, udid, myTestLogWriter, PrintUtil.ANSI_GREEN);
                     String activityBeforeAction = driver.currentActivity();
                     //记录到达的activity
                     recordActivity(activityBeforeAction);
@@ -585,7 +639,7 @@ public class MergeScript extends AbstractBaseScript {
                     }
                 } catch (Exception e) {
                     locateSuccessfulFlag = false;
-                    PrintUtil.print("Can't locate AndroidAlert compotent", TAG, udid, mylogWriter, PrintUtil.ANSI_RED);
+                    PrintUtil.print("Can't locate AndroidAlert compotent", TAG, udid, myTestLogWriter, PrintUtil.ANSI_RED);
                 }
             }
             i++;
@@ -593,7 +647,7 @@ public class MergeScript extends AbstractBaseScript {
     }
     private void clickAndroidMsg(){
         try{
-            PrintUtil.print("Click cancel button for AndroidMsg", TAG, udid, mylogWriter, PrintUtil.ANSI_GREEN);
+            PrintUtil.print("Click cancel button for AndroidMsg", TAG, udid, myTestLogWriter, PrintUtil.ANSI_GREEN);
             String activityBeforeAction = driver.currentActivity();
             //记录到达的activity
             recordActivity(activityBeforeAction);
@@ -613,7 +667,7 @@ public class MergeScript extends AbstractBaseScript {
                 PrintUtil.printException(TAG, udid, e1);
             }
         } catch (Exception e) {
-            PrintUtil.print("Can't locate AndroidMsg component", TAG, udid, mylogWriter, PrintUtil.ANSI_RED);
+            PrintUtil.print("Can't locate AndroidMsg component", TAG, udid, myTestLogWriter, PrintUtil.ANSI_RED);
         }
         threadSleep(2);
     }
@@ -631,7 +685,7 @@ public class MergeScript extends AbstractBaseScript {
         while(isAndroidAlert){
             //若页面上弹出了android警告弹窗则循环
             if(i >= 5) {
-                PrintUtil.print("Can't handle AndroidAlert On Launch", TAG, udid, mylogWriter, PrintUtil.ANSI_RED);
+                PrintUtil.print("Can't handle AndroidAlert On Launch", TAG, udid, myTestLogWriter, PrintUtil.ANSI_RED);
                 break;
             }
             isAndroidAlert = false;
@@ -666,7 +720,7 @@ public class MergeScript extends AbstractBaseScript {
     }
     private void threadSleep(int second){
         try {
-            PrintUtil.print("Sleep " + String.valueOf(second) + " seconds", TAG, udid, mylogWriter, PrintUtil.ANSI_BLUE);
+            PrintUtil.print("Sleep " + String.valueOf(second) + " seconds", TAG, udid, myTestLogWriter, PrintUtil.ANSI_BLUE);
             Thread.sleep(second * 1000);
         } catch (InterruptedException e1) {
             PrintUtil.printException(TAG, udid, e1);
@@ -783,7 +837,7 @@ public class MergeScript extends AbstractBaseScript {
         String timeBeforeAction;
         String timeAfterAction;
         try{
-            PrintUtil.print(msg, TAG, udid, mylogWriter, PrintUtil.ANSI_GREEN);
+            PrintUtil.print(msg, TAG, udid, myTestLogWriter, PrintUtil.ANSI_GREEN);
             activityBeforeAction = driver.currentActivity();
             //记录到达的activity
             recordActivity(activityBeforeAction);
@@ -821,16 +875,16 @@ public class MergeScript extends AbstractBaseScript {
     }
     public void executeInput(Component component, WebElement element){
         String inputValue = null;
-        PrintUtil.print("This is an editText , trying to find input value", TAG, udid, mylogWriter, PrintUtil.ANSI_BLUE);
+        PrintUtil.print("This is an editText , trying to find input value", TAG, udid, myTestLogWriter, PrintUtil.ANSI_BLUE);
         InputFinder finder = new InputFinder();
         try {
             inputValue = finder.getInputValue(AddressUtil.getHumanScriptPath(),component.getResource_id());
         } catch (IOException e) {
-            PrintUtil.print("Find input value error!", TAG, udid, mylogWriter, PrintUtil.ANSI_RED);
+            PrintUtil.print("Find input value error!", TAG, udid, myTestLogWriter, PrintUtil.ANSI_RED);
             e.printStackTrace();
         }
         if(inputValue != null){
-            PrintUtil.print("Input " + component.getLocator() + " use vaule " + inputValue, TAG, udid, mylogWriter, PrintUtil.ANSI_GREEN);
+            PrintUtil.print("Input " + component.getLocator() + " use vaule " + inputValue, TAG, udid, myTestLogWriter, PrintUtil.ANSI_GREEN);
             String activityBeforeAction = driver.currentActivity();
             //记录到达的activity
             recordActivity(activityBeforeAction);
@@ -870,11 +924,11 @@ public class MergeScript extends AbstractBaseScript {
                 PrintUtil.printException(TAG, udid, e1);
                 e1.printStackTrace();
             }
-        }else {PrintUtil.print("Can't find input value for this edittext", TAG, udid, mylogWriter, PrintUtil.ANSI_RED);}
+        }else {PrintUtil.print("Can't find input value for this edittext", TAG, udid, myTestLogWriter, PrintUtil.ANSI_RED);}
     }
     public void executeClick(Component component, WebElement element){
         String locator = component.getLocator();
-        PrintUtil.print("Click component " + locator, TAG, udid, mylogWriter, PrintUtil.ANSI_GREEN);
+        PrintUtil.print("Click component " + locator, TAG, udid, myTestLogWriter, PrintUtil.ANSI_GREEN);
         String activityBeforeAction = driver.currentActivity();
         //记录到达的activity
         recordActivity(activityBeforeAction);
@@ -911,7 +965,7 @@ public class MergeScript extends AbstractBaseScript {
     }
     public void executeClickFatherComponent(Component component){
         String locator = component.getLocator();
-        PrintUtil.print("Click father component " + locator, TAG, udid, mylogWriter, PrintUtil.ANSI_GREEN);
+        PrintUtil.print("Click father component " + locator, TAG, udid, myTestLogWriter, PrintUtil.ANSI_GREEN);
         WebElement element;
         try {
             if(locator.startsWith("//")) {
@@ -920,7 +974,7 @@ public class MergeScript extends AbstractBaseScript {
                 element = driver.findElementById(locator);
             }
         } catch (Exception e) {
-            PrintUtil.print("Can't locate father component " + locator, TAG, udid, mylogWriter, PrintUtil.ANSI_RED);
+            PrintUtil.print("Can't locate father component " + locator, TAG, udid, myTestLogWriter, PrintUtil.ANSI_RED);
             return;
         }
         String activityBeforeAction = driver.currentActivity();
@@ -1093,7 +1147,7 @@ public class MergeScript extends AbstractBaseScript {
                     //如果滑了十次还没有出现clickable为true控件的页面,就跳出循环,因为可能引导页面之后出现的第一个页面就没有任何按钮(虽然此情况极少)
                     break;
                 }
-                PrintUtil.print("Ready to swipe because there is no clickable component", TAG, udid, mylogWriter, PrintUtil.ANSI_BLUE);
+                PrintUtil.print("Ready to swipe because there is no clickable component", TAG, udid, myTestLogWriter, PrintUtil.ANSI_BLUE);
                 executeSwipe();
                 newComList = getCurrentClickableComList(i, true);
             }
@@ -1152,8 +1206,8 @@ public class MergeScript extends AbstractBaseScript {
             currentActivity = driver.currentActivity();
             //记录到达的activity
             recordActivity(currentActivity);
-            PrintUtil.print("Current activity is " + currentActivity, TAG, udid, mylogWriter, PrintUtil.ANSI_BLUE);
-            PrintUtil.print("Return start activity is" + returnStartActivity, TAG, udid, mylogWriter, PrintUtil.ANSI_BLUE);
+            PrintUtil.print("Current activity is " + currentActivity, TAG, udid, myTestLogWriter, PrintUtil.ANSI_BLUE);
+            PrintUtil.print("Return start activity is" + returnStartActivity, TAG, udid, myTestLogWriter, PrintUtil.ANSI_BLUE);
             if(returnCount >= 3) {
                 break;
             }
@@ -1232,7 +1286,7 @@ public class MergeScript extends AbstractBaseScript {
     }
     private boolean judgeTestedComponent(Component component){
         if(component.isHasBeenTested()){
-            PrintUtil.print(component.getLocator() + " has been tested, skip to next one", TAG, udid, mylogWriter, PrintUtil.ANSI_BLUE);
+            PrintUtil.print(component.getLocator() + " has been tested, skip to next one", TAG, udid, myTestLogWriter, PrintUtil.ANSI_BLUE);
             return true;
         }
         return false;
@@ -1394,7 +1448,7 @@ public class MergeScript extends AbstractBaseScript {
             PrintUtil.printException(TAG, udid, e);
         }
         if(ignoreFlag) {
-            PrintUtil.print(component.getLocator() + " is a component in ignore file, skip to next one", TAG, udid, mylogWriter, PrintUtil.ANSI_BLUE);
+            PrintUtil.print(component.getLocator() + " is a component in ignore file, skip to next one", TAG, udid, myTestLogWriter, PrintUtil.ANSI_BLUE);
         }
         return ignoreFlag;
     }
@@ -1413,7 +1467,7 @@ public class MergeScript extends AbstractBaseScript {
             if(component.getFatherComponent() != null ){
                 father = component.getFatherComponent().getLocator();
             }
-            PrintUtil.print(component.getLocator() + ", Tested: " + component.isHasBeenTested() + " , Father: " + father, TAG, udid, mylogWriter, PrintUtil.ANSI_YELLOW);
+            PrintUtil.print(component.getLocator() + ", Tested: " + component.isHasBeenTested() + " , Father: " + father, TAG, udid, myTestLogWriter, PrintUtil.ANSI_YELLOW);
         }
     }
     private boolean countSameComponent(List<Component> comList, List<Component> comListAfterClick){

+ 107 - 0
src/main/java/net/mooctest/www/android_auto_test/utils/RunScriptTest.java

@@ -0,0 +1,107 @@
+package net.mooctest.www.android_auto_test.utils;
+
+import com.sinaapp.msdxblog.apkUtil.entity.ApkInfo;
+import com.sinaapp.msdxblog.apkUtil.utils.ApkUtil;
+import io.appium.java_client.android.AndroidDriver;
+import net.mooctest.www.android_auto_test.Scripts.AbstractBaseScript;
+import net.mooctest.www.android_auto_test.Scripts.MergeScript;
+import net.mooctest.www.android_auto_test.common.constant.Consts;
+import net.mooctest.www.android_auto_test.common.exceptions.ApkParseException;
+import net.mooctest.www.android_auto_test.common.exceptions.AppiumDriverInitException;
+import net.mooctest.www.android_auto_test.models.Device;
+import org.openqa.selenium.WebDriverException;
+import org.openqa.selenium.WebElement;
+import org.openqa.selenium.remote.CapabilityType;
+import org.openqa.selenium.remote.DesiredCapabilities;
+import org.openqa.selenium.remote.UnreachableBrowserException;
+
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+public class RunScriptTest {
+    String fileName = "jlb.apk";
+    String deviceName = "emulator-5554";
+    String udid = deviceName;
+    String traceId = "100";
+    AndroidDriver<WebElement> driver;
+    String port = "4730";
+    String TAG = "just for test";
+
+    ApkInfo apkInfo;
+    String apkPath = "tasks"+ File.separator + traceId + File.separator+fileName;
+    public void runTest(){
+
+        Device device = new Device();
+        device.setId(deviceName);
+        device.setUdid(deviceName);
+        try {
+            apkInfo = new ApkUtil().getApkInfo(apkPath);
+        } catch (Exception e) {
+            throw new ApkParseException(e.getMessage());
+        }
+        initDriver();
+        AbstractBaseScript script = new MergeScript(udid, driver, apkInfo, traceId);
+        //AbstractBaseScript script = new DefaultScript(udid, driver, apkInfo, traceId);
+        script.runAndroidAutoTest();
+
+    }
+    public static void main(String[]args){
+        new RunScriptTest().runTest();
+    }
+    private void uninstallDriver(){
+        String command = String.format("adb -s %s uninstall io.appium.settings", udid);
+        OsUtil.runCommand(command);
+    }
+    private void initDriver() {
+        PrintUtil.print("Uninstall and init driver " + this.udid + " " + port, TAG, this.udid);
+        uninstallDriver();
+        File app = new File(this.apkPath);
+        DesiredCapabilities capabilities = new DesiredCapabilities();
+        capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
+        capabilities.setCapability("platformName", "Android");
+        capabilities.setCapability("deviceName", "Android Emulator");
+        capabilities.setCapability("platformVersion", DeviceUtil.getPlatformVersion(udid));
+        capabilities.setCapability("app", app.getAbsolutePath());
+        capabilities.setCapability("appPackage", this.apkInfo.getPackageName());
+        capabilities.setCapability("appActivity", this.apkInfo.getLaunchableActivity());
+        capabilities.setCapability("udid",this.udid);
+        capabilities.setCapability("unicodeKeyboard","true");
+        capabilities.setCapability("resetKeyboard","true");
+        capabilities.setCapability("noSign", "true");
+//        capabilities.setCapability("automationName", "UiAutomator2");
+        boolean success = false;
+        int index = 0;
+        while (! success && index <= Consts.INIT_DRIVER_TIMES) {
+            try {
+                driver = new AndroidDriver<>(new URL("http://127.0.0.1:" + port + "/wd/hub"), capabilities);
+                success = true;
+            } catch (MalformedURLException e1) {
+                e1.printStackTrace();
+                index++;
+            } catch (UnreachableBrowserException e) {
+                PrintUtil.printErr("Device " + this.udid + ", restart appium", TAG, this.udid);
+                startServer();
+                index++;
+            } catch (WebDriverException e) {
+                PrintUtil.printErr("init android driver  web driverException " + this.udid + e.getMessage(), TAG, this.udid);
+                index++;
+            }
+        }
+        if (driver != null) {
+            PrintUtil.print("the driver init successfully " + this.udid, TAG, this.udid);
+        } else {
+            PrintUtil.print("the driver init wrongly " + this.udid, TAG, this.udid);
+            throw new AppiumDriverInitException("Init appium driver failed!");
+        }
+    }
+
+    private void startServer() {
+        AppiumManager manager = AppiumManager.getInstance();
+        String appiumLogsPath = AddressUtil.getAppiumLogPath(traceId, udid);
+        String exceptionLogsPath = AddressUtil.getExceptionPath(traceId, udid);
+        manager.setupAppium(this.udid, appiumLogsPath, exceptionLogsPath);
+        this.port = manager.checkDevicePort(this.udid);
+        PrintUtil.print("Setup Appium " + this.udid + " " + this.port, TAG, this.udid);
+    }
+}