十五年测试老手,长期负责WEB\APP 项目测试,目前主要负责团队管理工作。

Android中的自动化测试

上一篇 / 下一篇  2011-09-04 12:57:15 / 个人分类:Android

文章来源
  • 文章来源:【转载】

1.用adb shell去启动测试程序,如:

H7Y|1G"y6cjX0
adb shell am start -n com.google.android.contacts/.ContactsActivity

am是一个脚本命令:51Testing软件测试网 |#G(`3H Vx:T5x*v2p1p

51Testing软件测试网)y'E e-d b)h pg

# Script. to start "am" on the device, which has a very rudimentary
# shell.
#
base=/system
export CLASSPATH=$base/framework/am.jar
exec app_process $base/bin com.android.commands.am.Am $*

9VHE@)h9S9Y Gu5B0

不加任何参数运行am,我们可以看帮助信息:51Testing软件测试网i}P"`g)\7{~ X#D

[root@localhost ~]# adb shell am
usage: am [start|broadcast|instrument|profile]
       am start -D INTENT
       am broadcast INTENT
       am instrument [-r] [-e <ARG_NAME> <ARG_VALUE>] [-p <PROF_FILE>]
                [-w] <:component>
       am profile <PROCESS> [start <PROF_FILE>|stop]
       INTENT is described with:
                [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
                [-c <CATEGORY> [-c <CATEGORY>] ...]
                [-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
                [--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
                [-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
                [-n <:component>] [-f <FLAGS>] [<URI>]

am 是在命令行启动android程序的一种方法,它是在cmds/am/src/com/android/commands/am/Am.java里实现的。51Testing软件测试网7mU]"y"Dk e Otms

在函数runInstrument实现了instrument 命令,主要做了下面的事:
Z;KK;c7o(x@W01.参数解析
~o1\g @ YE D0l;S02.创建一个InstrumentationWatcher对象,用来接收测试结果。
B\Z+vc6Q@03.调用IActivityManager的startInstrumentation运行测试程序。51Testing软件测试网3M)x/m| X|ER2i)`,XG:J
4.调用InstrumentationWatcher的waitForFinish函数等待测试结束。51Testing软件测试网8TD]yq.y

+?1l!f!Sm^4V0
private void runInstrument() {
...
        try {
            if (!mAm.startInstrumentation(cn, profileFile, 0, args, watcher)) {
                System.out.println("INSTRUMENTATION_FAILED: " +
                        cn.flattenToString());
                showUsage();
                return;
            }
        } catch (RemoteException e) {
        }
 
...
        if (watcher != null) {
            if (!watcher.waitForFinish()) {
                System.out.println("INSTRUMENTATION_ABORTED: System has crashed.");
            }
        }
...
}

uA5D/} _9L#w/DB-w0

startInstrumentation实际上是在services/java/com/android/server/am/ActivityManagerService.java里执行的,执行过程如下:
$~H8I(lw-g~01.调用PackageManager的getInstrumentationInfo获取InstrumentationInfo和ApplicationInfo。51Testing软件测试网~`'_QF E
2.uninstall原来的测试程序(如果前面运行了相同的测试程序,就会停止它)
4@j)c'Uw03.运行新的测试程序。

R)zPGB0]0

51Testing软件测试网7L}ZIp&j

public boolean startInstrumentation(ComponentName className,
            String profileFile, int flags, Bundle arguments,
            IInstrumentationWatcher watcher) {
...
      try {
                ii = mContext.getPackageManager().getInstrumentationInfo(
                    className, 0);
                ai = mContext.getPackageManager().getApplicationInfo(
                    ii.targetPackage, PackageManager.GET_SHARED_LIBRARY_FILES);
            } catch (PackageManager.NameNotFoundException e) {
...
            uninstallPackageLocked(ii.targetPackage, -1, true);
            ProcessRecord app = addAppLocked(ai);
...
     }

TAG: Android android 自动化测试

 

评分:0

我来说两句

Open Toolbar