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