Android 自動化測試(1)如何安裝和卸載一個應用

來源:互聯網
上載者:User

標籤:自動化測試   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)如何安裝和卸載一個應用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.