MonkeyRunner_Monkeyrunner里使用Java做为脚本语言(1)

来源:互联网 发布:显示屏超频软件 编辑:程序博客网 时间:2024/06/05 02:28
monkeyrunner官网和很多地方都是使用的python做为脚本语言的,但是实际上monkeyrunner是支持Java做为脚本语言的,下面是对在monkeyrunner中使用Java的一些尝试,已经全部使用过是可行的,需要引入的jar包包括: 
ddmlib.jar;guavalib.jar;sdklib.jar和monkeyrunner.jar 
Java代码  收藏代码
  1. package com.test  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Collection;  
  5. import java.util.HashMap;  
  6.   
  7. import com.aliyun.smoking.monkeyrunner.extend.ImageProcess;  
  8. import com.android.monkeyrunner.adb.AdbBackend;  
  9. import com.android.monkeyrunner.core.IMonkeyDevice;  
  10. import com.android.monkeyrunner.core.IMonkeyImage;  
  11. import com.android.monkeyrunner.core.TouchPressType;  
  12.   
  13. /** 
  14.  * this class provide java code to call monkeyrunner.jar to execute test case 
  15.  *  
  16.  * @author cx 
  17.  *  
  18.  */  
  19. public class RunnerProxy {  
  20.     private IMonkeyDevice device;  
  21.     private AdbBackend adb;  
  22.     private static RunnerProxy instance;  
  23.   
  24.     public static RunnerProxy getInstance() {  
  25.         if (instance == null) {  
  26.             instance = new RunnerProxy();  
  27.         }  
  28.         return instance;  
  29.     }  
  30.   
  31.     private RunnerProxy() {  
  32.         adb = new AdbBackend();  
  33.     }  
  34.   
  35.     /** 
  36.      * this function will connect to a device, emulator or phone 
  37.      */  
  38.     public void connect() {  
  39.         assert (adb != null);  
  40.         device = adb.waitForConnection();  
  41.     }  
  42.   
  43.     /** 
  44.      * this function clear device connect 
  45.      */  
  46.     public void dispose() {  
  47.         if (device != null) {  
  48.             device.dispose();  
  49.             device = null;  
  50.         }  
  51.     }  
  52.   
  53.     private String imageDir;  
  54.   
  55.     public void setImageDir(String imageDir) {  
  56.         this.imageDir = imageDir;  
  57.     }  
  58.   
  59.     public String getImageDir() {  
  60.         return imageDir;  
  61.     }  
  62.   
  63.     private String logDir;  
  64.   
  65.     public void setLogDir(String logDir) {  
  66.         this.logDir = logDir;  
  67.     }  
  68.   
  69.     public String getLogDir() {  
  70.         return logDir;  
  71.     }  
  72.   
  73.     /** 
  74.      * this function finish touch operation 
  75.      *  
  76.      * @param x 
  77.      *            : x coordinate 
  78.      * @param y 
  79.      *            : y coordinate 
  80.      */  
  81.     public void touch(int x, int y) {  
  82.         assert (device != null);  
  83.         device.shell("sendevent /dev/input/event6 3 48 1");  
  84.         device.shell("sendevent /dev/input/event6 3 53 " + x);  
  85.         device.shell("sendevent /dev/input/event6 3 54 " + y);  
  86.         device.shell("sendevent /dev/input/event6 0 2 0");  
  87.         device.shell("sendevent /dev/input/event6 0 0 0");  
  88.         device.shell("sendevent /dev/input/event6 3 48 0");  
  89.         device.shell("sendevent /dev/input/event6 0 2 0");  
  90.         device.shell("sendevent /dev/input/event6 0 0 0");  
  91.     }  
  92.   
  93.     /** 
  94.      * this function finish long touch operation 
  95.      *  
  96.      * @param x 
  97.      *            : x coordinate 
  98.      * @param y 
  99.      *            : y coordinate 
  100.      */  
  101.     public void longTouch(int x, int y) {  
  102.         assert (device != null);  
  103.         device.drag(x, y, x, y, 102);  
  104.     }  
  105.   
  106.     /** 
  107.      * this function finish drag from one point to another point 
  108.      *  
  109.      * @param x 
  110.      *            : x coordinate of start point 
  111.      * @param y 
  112.      *            : y coordinate of start point 
  113.      * @param endX 
  114.      *            : x coordinate of end point 
  115.      * @param endY 
  116.      *            : Y coordinate of end point 
  117.      *  
  118.      */  
  119.     public void drag(int x, int y, int endX, int endY) {  
  120.         assert (device != null);  
  121.         device.drag(x, y, endX, endY, 102);  
  122.     }  
  123.   
  124.     /** 
  125.      * this function finish type a text to view operation 
  126.      *  
  127.      * @param value 
  128.      *            : text to type in 
  129.      */  
  130.     public void type(String value) {  
  131.         assert (device != null);  
  132.         device.type(value);  
  133.     }  
  134.   
  135.     /** 
  136.      * this function finish click a key operation 
  137.      *  
  138.      * @param keyCode 
  139.      *            : key code 
  140.      */  
  141.     public void press(String keyCode) {  
  142.         assert (device != null);  
  143.         device.press(keyCode, TouchPressType.DOWN_AND_UP);  
  144.     }  
  145.   
  146.     /** 
  147.      * this function finish start an activity operation 
  148.      *  
  149.      * @param component 
  150.      *            : activity what to start 
  151.      */  
  152.     public void startActivity(String component) {  
  153.         assert (device != null);  
  154.         String action = "android.intent.action.MAIN";  
  155.         Collection<String> categories = new ArrayList<String>();  
  156.         categories.add("android.intent.category.LAUNCHER");  
  157.         device.startActivity(null, action, nullnull, categories,  
  158.                 new HashMap<String, Object>(), component, 0);  
  159.     }  
  160.   
原创粉丝点击