码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2014-09-23 22:32:55      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:自动化测试   android   monkey runner   aapt   安装卸载   

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)如何安装和卸载一个应用

标签:自动化测试   android   monkey runner   aapt   安装卸载   

原文地址:http://blog.csdn.net/vshuang/article/details/39502405

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!