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

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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值