执行自动化测试时我们都必须先开启 App 后才开始才进行测试,开启 App 的动作我们可以透过 adb(Android Debug Bridge) 指令来帮我们完成,指令如下所示:
    

adb shell am start -n [PACKAGE-NAME]/[ACTIVITY-NAME] # adb shell am start -n <com.package.name>/<com.package.name.ActivityName>


其中 PACKAGE-NAME 参数的部分,我们可以通过以下指令来得知 App 的 package name。
   
adb shell pm list packages -f


接著 ACTIVITY-NAME 参数则可通过以下指令来查看,它就可以列出所有正在执行 App 的 Activity 名称。

adb shell dumpsys activity

包名和Activity类的类名一定要输入正确,但是要注意只能列出正在执行的App 的 Activity 名称,如果你的app没有在执行是看不到的。我们可以进入AndroidManifest.xml文件进行查看,还有一个办法可以通过安装aapt来获取app信息。还可以用re-sign工具(在最后的弹出信息框会显示出包名和Activity类名)。如下:

aapt dump badging com.google.android.apps.maps-1.apk      # aapt dump badging <path-to-apk>

下面我们来看下adb shell am命令的帮助

usage: am [subcommand] [options] 


     start an Activity: am start [-D] [-W] <INTENT> 

         -D: enable debugging 

         -W: wait for launch to complete 


     start a Service: am startservice <INTENT> 


     send a broadcast Intent: am broadcast <INTENT> 


     start an Instrumentation: am instrument [flags] <COMPONENT> 

         -r: print raw results (otherwise decode REPORT_KEY_STREAMRESULT) 

         -e <NAME> <VALUE>: set argument <NAME> to <VALUE> 

         -p <FILE>: write profiling data to <FILE> 

         -w: wait for instrumentation to finish before returning 


     start profiling: am profile <PROCESS> start <FILE> 

     stop profiling: am profile <PROCESS> stop 


     <INTENT> specifications include these flags: 

         [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>] 

         [-c <CATEGORY> [-c <CATEGORY>] ...] 

         [-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...] 

         [--esn <EXTRA_KEY> ...] 

         [--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...] 

         [-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...] 

         [-n <COMPONENT>] [-f <FLAGS>] 

         [--grant-read-uri-permission] [--grant-write-uri-permission] 

         [--debug-log-resolution] 

         [--activity-brought-to-front] [--activity-clear-top] 

         [--activity-clear-when-task-reset] [--activity-exclude-from-recents] 

         [--activity-launched-from-history] [--activity-multiple-task] 

         [--activity-no-animation] [--activity-no-history] 

         [--activity-no-user-action] [--activity-previous-is-top] 

         [--activity-reorder-to-front] [--activity-reset-task-if-needed] 

         [--activity-single-top] 

         [--receiver-registered-only] [--receiver-replace-pending] 
        [<URI>]

adb停止 应用程序


cn.com.conversant.swiftsync.android


cn.com.conversant.swiftsync.android为包名。
比如关闭浏览器进程:adb shell am kill-all com.android.browser