Android 自动化功能测试神器ChimpChat(MonkeyRunner)
这几天在捣弄基于Android 远程控制和自动处理的一个解决方案,开始想的很复杂在手机上开发一个APK,然后安装进行远程控制,另一兄弟说很多东西需要处理,而且还需要Root,而且模拟器每次重启后Root不行了,反正很烦。
开始找到了MonkeyRunner,不得不说Google东西太庞大,资料太少,只好扒代码装JAD慢慢找(加上GFW这几天也特别卖力,google的相关网站一直上不去),最后扒出来了ChimpChat,哈哈,下面就是利用ChimpChat的java代码的“HelloWorld”:
public class DeviceService(){ private AdbBackend _adbBackend; private AndroidDebugBridge _bridge; public DeviceService(){ AndroidDebugBridge.addDeviceChangeListener(new DeviceChangeListener()); _adbBackend = new AdbBackend("/Users/apple/work/android-sdk-macosx/platform-tools/adb"); _bridge = AndroidDebugBridge.getBridge(); } class DeviceChangeListener implements IDeviceChangeListener{ @Override public void deviceChanged(IDevice device, int arg1) { System.out.println(String.format("%s changed %d",device.getSerialNumber(),arg1)); if(arg1==4){ IChimpDevice device = adbBackend.waitForConnection(30000, device.getSerialNumber()); //_chimpDevices.put( device.getSerialNumber(), device); device.drag(0, 0, 100, 100, 10, 9000L);//下拉状态条的 } } @Override public void deviceConnected(IDevice arg0) { System.out.println(String.format("%s 连接了",arg0.getSerialNumber())); } @Override public void deviceDisconnected(IDevice arg0) { System.out.println(String.format("%s 断开了",arg0.getSerialNumber())); //_chimpDevices.remove(arg0.getSerialNumber()); } } public static void main(String[] args){ new DeviceService(); } }
手机或者模拟器一定要开调试模式,其实说白了就是ADB的功能,adb 命令能做的这里全能做,可以作为Android功能测试,欢迎探讨。
IChimpDevice就是你要操作的每个设备,具体可以,估计也会访问不了,贴上接口代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.android.chimpchat.core; 2 3 import com.android.chimpchat.ChimpManager; 4 import com.android.chimpchat.hierarchyviewer.HierarchyViewer; 5 6 import java.util.Collection; 7 import java.util.Map; 8 9 import javax.annotation.Nullable; 10 11 /** 12 * ChimpDevice interface. 13 */ 14 public interface IChimpDevice { 15 /** 16 * Create a ChimpManager for talking to this device. 17 * 18 * @return the ChimpManager 19 */ 20 ChimpManager getManager(); 21 22 /** 23 * Dispose of any native resources this device may have taken hold of. 24 */ 25 void dispose(); 26 27 /** 28 * @return hierarchy viewer implementation for querying state of the view 29 * hierarchy. 30 */ 31 HierarchyViewer getHierarchyViewer(); 32 33 /** 34 * Take the current screen's snapshot. 35 * @return the snapshot image 36 */ 37 IChimpImage takeSnapshot(); 38 39 /** 40 * Reboot the device. 41 * 42 * @param into which bootloader to boot into. Null means default reboot. 43 */ 44 void reboot(@Nullable String into); 45 46 /** 47 * List properties of the device that we can inspect 48 * 49 * @return the list of property keys 50 */ 51 Collection<String> getPropertyList(); 52 53 /** 54 * Get device's property. 55 * 56 * @param key the property name 57 * @return the property value 58 */ 59 String getProperty(String key); 60 61 /** 62 * Get system property. 63 * 64 * @param key the name of the system property 65 * @return the property value 66 */ 67 String getSystemProperty(String key); 68 69 /** 70 * Perform a touch of the given type at (x,y). 71 * 72 * @param x the x coordinate 73 * @param y the y coordinate 74 * @param type the touch type 75 */ 76 void touch(int x, int y, TouchPressType type); 77 78 /** 79 * Perform a press of a given type using a given key. 80 * 81 * TODO: define standard key names in a separate class or enum 82 * 83 * @param keyName the name of the key to use 84 * @param type the type of press to perform 85 */ 86 void press(String keyName, TouchPressType type); 87 88 /** 89 * Perform a drag from one one location to another 90 * 91 * @param startx the x coordinate of the drag's starting point 92 * @param starty the y coordinate of the drag's starting point 93 * @param endx the x coordinate of the drag's end point 94 * @param endy the y coordinate of the drag's end point 95 * @param steps the number of steps to take when interpolating points 96 * @param ms the duration of the drag 97 */ 98 void drag(int startx, int starty, int endx, int endy, int steps, long ms); 99 100 /** 101 * Type a given string. 102 * 103 * @param string the string to type 104 */ 105 void type(String string); 106 107 /** 108 * Execute a shell command. 109 * 110 * @param cmd the command to execute 111 * @return the output of the command 112 */ 113 String shell(String cmd); 114 115 /** 116 * Push a file to device. 117 * 118 * @param localFilePath the local filepath 119 * @param remoteFilePath the remote filepath 120 * @return true if success 121 */ 122 boolean pushFile(String localFilePath, String remoteFilePath); 123 124 /** 125 * Pull a file from device. 126 * 127 * @param remoteFilePath the remote filepath 128 * @param localFilePath the local filepath 129 * @return true if success 130 */ 131 boolean pullFile(String remoteFilePath, String localFilePath); 132 133 /** 134 * Install a given package. 135 * 136 * @param path the path to the installation package 137 * @return true if success 138 */ 139 boolean installPackage(String path); 140 141 /** 142 * Uninstall a given package. 143 * 144 * @param packageName the name of the package 145 * @return true if success 146 */ 147 boolean removePackage(String packageName); 148 149 /** 150 * Start an activity. 151 * 152 * @param uri the URI for the Intent 153 * @param action the action for the Intent 154 * @param data the data URI for the Intent 155 * @param mimeType the mime type for the Intent 156 * @param categories the category names for the Intent 157 * @param extras the extras to add to the Intent 158 * @param component the component of the Intent 159 * @param flags the flags for the Intent 160 */ 161 void startActivity(@Nullable String uri, @Nullable String action, 162 @Nullable String data, @Nullable String mimeType, 163 Collection<String> categories, Map<String, Object> extras, @Nullable String component, 164 int flags); 165 166 /** 167 * Send a broadcast intent to the device. 168 * 169 * @param uri the URI for the Intent 170 * @param action the action for the Intent 171 * @param data the data URI for the Intent 172 * @param mimeType the mime type for the Intent 173 * @param categories the category names for the Intent 174 * @param extras the extras to add to the Intent 175 * @param component the component of the Intent 176 * @param flags the flags for the Intent 177 */ 178 void broadcastIntent(@Nullable String uri, @Nullable String action, 179 @Nullable String data, @Nullable String mimeType, 180 Collection<String> categories, Map<String, Object> extras, @Nullable String component, 181 int flags); 182 183 /** 184 * Run the specified package with instrumentation and return the output it 185 * generates. 186 * 187 * Use this to run a test package using InstrumentationTestRunner. 188 * 189 * @param packageName The class to run with instrumentation. The format is 190 * packageName/className. Use packageName to specify the Android package to 191 * run, and className to specify the class to run within that package. For 192 * test packages, this is usually testPackageName/InstrumentationTestRunner 193 * @param args a map of strings to objects containing the arguments to pass 194 * to this instrumentation. 195 * @return A map of strings to objects for the output from the package. 196 * For a test package, contains a single key-value pair: the key is 'stream' 197 * and the value is a string containing the test output. 198 */ 199 Map<String, Object> instrument(String packageName, 200 Map<String, Object> args); 201 202 /** 203 * Wake up the screen on the device. 204 */ 205 void wake();