首页 > 代码库 > Android 自动化测试(1)如何安装和卸载一个应用

Android 自动化测试(1)如何安装和卸载一个应用

1、android aapt
aapt 是android assert packaging tool的缩写,采用aapt可以查看apk的信息和列出apk包的内容


2、monkey runner 和chimpchat
The monkeyrunner tool provides an API for writing programs that control an Android device or emulator from outside of Android code. 
With monkeyrunner, you can write a Python program that installs an Android application or test package, runs it, 
sends keystrokes to it, takes screenshots of its user interface, and stores screenshots on the workstation. 
The monkeyrunner tool is primarily designed to test applications and devices at the functional/framework level and for running unit test suites, 
but you are free to use it for other purposes.
monkeyrunner 工具提供了一个从Android源码外部写程序控制一个Android设备或者模拟器的API。
你可以用monkeyrunner写一个Python程序,来装一个Android应用和测试包,运行它,发key事件,截屏,存在本地。
monkeyrunner工具的设计起源于在功能/框架层面来测试应用和设备,和跑单元测试的测试套件,
但是你也可以免费使用它用作其他用途。


3、使用java来语言来调用monkeyrunner api


import com.android.chimpchat.adb.AdbBackend;
import com.android.chimpchat.core.IChimpDevice;


public class MonkeyTest {
    public static void main(String[] args) {
        // sdk/platform-tools has to be in PATH env variable in order to find adb
        IChimpDevice device = new AdbBackend().waitForConnection();
        // Print Device Name
        System.out.println(device.getProperty("build.model"));
        // Take a snapshot and save to out.png
        device.takeSnapshot().writeToFile("out.png", null);
        device.dispose();
    }
}

The library dependencies are:
chimpchat.jar, common.jar, ddmlib.jar, guava-13.0.1.jar, sdklib.jar
They can all be found in the sdk/tools/lib subdirectory of the ADT bundle.


4、 完整的安装和卸载包流程,这样就完成了自动化测试的第一步

package com.robot.dream.test;


import com.android.chimpchat.adb.AdbBackend;
import com.android.chimpchat.core.IChimpDevice;
import com.sinaapp.msdxblog.apkUtil.entity.ApkInfo;
import com.sinaapp.msdxblog.apkUtil.utils.ApkUtil;


public class MonkeyTest {


    public static void main(String[] args) {


        String testApkPath = "D:\\apks_bak\\AndroidSensorBox.apk";
        System.out.println("start");
        IChimpDevice device = new AdbBackend().waitForConnection();


        System.out.println("monkey test connected");
        device.installPackage(testApkPath);
        System.out.println("install package success");


        try {
            Thread.sleep(3000);
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }


        try {
            ApkInfo apkInfo = new ApkUtil().getApkInfo(testApkPath);
            String pkgName = apkInfo.getPackageName();
            System.out.println("package name is "+pkgName);
            device.removePackage(pkgName);
            System.out.println("remove package success");
        } catch (Exception e) {
            e.printStackTrace();
        }


        // Print Device Name
        System.out.println(device.getProperty("build.model"));


        // Take a snapshot and save to out.png
        device.takeSnapshot().writeToFile("D:\\out1.png", "PNG");
        device.dispose();
    }
}


参考文献:
http://energykey.iteye.com/blog/1856173
http://developer.android.com/tools/help/monkeyrunner_concepts.html
http://stackoverflow.com/questions/6686085/how-can-i-make-a-java-app-using-the-monkeyrunner-api?rq=1


参考开源项目:
http://code.google.com/p/cfuture09-apkutil/


代码下载:
http://download.csdn.net/detail/vshuang/7964321

Android 自动化测试(1)如何安装和卸载一个应用