Android自動化測試之使用java調用monkeyrunner(補充篇)

來源:互聯網
上載者:User

    上一篇使用java調用monkeyrunner(http://fengbohaishang.blog.51cto.com/5106297/1065647)中遺留了一個問題,就是上次用的是低版本的4個包解決的問題,使用高版本的jar包怎麼調用monkeyrunner呢?
         經過一位朋友的提示說,現在高版本的方法已經變了,我就按照他的提示,上網搜了一下需要的類,測試通過後,特寫此補充篇總結一下。
上次使用的是android sdk tools路徑下的lib裡面的4個包:ddmlib.jar,guavalib.jar,monkeyrunner.jar,sdklib.jar.而更新後的版本需要添加另外一個包就是:chimpchat.jar,monkerunner.jar這個包倒不是必須的了。另外,低版本中是用AdbMonkeyDevice實現IMonkeyDevice,高版本中沒有這兩個類了,用的AdbChimpDevice和IchimpDevice。
而通過查看AdbChimpDevice   www.2cto.com(http://code.google.com/p/aster/source/browse/src/com/android/chimpchat/adb/AdbChimpDevice.java?r=967f7f8cd6249c69e00c6de7ff1b55bd0f51d311)和IchimpDevice(http://code.google.com/p/aster/source/browse/src/com/android/chimpchat/core/IChimpDevice.java?r=967f7f8cd6249c69e00c6de7ff1b55bd0f51d311)這兩個類在官方的源碼,就不難發現,AdbChimpDevice實現了IchimpDevice這個介面,不過串連方法還是通過AdbBackend,通過adb方式串連模擬器,或者真機。只是Device的父類發生了變化。
       下面還是用以前的測試類別,進行稍微改變一下,就可以看出兩者的不同:
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.HashMap; 
import com.android.chimpchat.adb.AdbBackend; 
import com.android.chimpchat.adb.AdbChimpDevice; 
public class TestNewMonkeyrunner { 
 
    /** 
     * @param args 
     */ 
     //這裡有變化 
    private static AdbChimpDevice device; 
    private static AdbBackend adb; 
         
    public static void main(String[] args) { 
        // TODO Auto-generated method stub 
     
         if (adb==null){  
             adb = new AdbBackend();  
//      參數分別為自己定義的等待連線時間和裝置id  
 //這裡需要注意一下adb的類型 
             device = (AdbChimpDevice) adb.waitForConnection(8000,"MSM8225QRD5"); 
         }  
         //添加啟動許可權 
            String action = "android.intent.action.MAIN";    
            Collection<String> categories = new ArrayList<String>();    
            categories.add("android.intent.category.LAUNCHER"); 
    //              啟動要測試的主介面 
            device.startActivity(null, action, null, null, categories,    
            new HashMap<String, Object>(),"cn.com.fetion/.android.ui.activities.StartActivity", 0);  
     //           點擊某一個座標 
     //touch方法略有變化           
device.touch(202,258,com.android.chimpchat.core.TouchPressType.DOWN_AND_UP);          
    }        
         
    } 
        從上面可以看出,高版本與低版本的變化,並不是很多。只要串連上裝置,一些需要用到的操作方法,自己可以去源碼裡面看,也可以自己重寫一些常用的方法。
        源碼裡的注釋是非常詳細,比如IchimpDevice介面類中的startActivity方法:
void startActivity(@Nullable String uri, @Nullable String action, 
            @Nullable String data, @Nullable String mimeType, 
            Collection<String> categories, Map<String, Object> extras, @Nullable String component, 
            int flags); 
 
    /** 
     * Send a broadcast intent to the device. 
     * 
     * @param uri the URI for the Intent 
     * @param action the action for the Intent 
     * @param data the data URI for the Intent 
     * @param mimeType the mime type for the Intent 
     * @param categories the category names for the Intent 
     * @param extras the extras to add to the Intent 
     * @param component the component of the Intent 
     * @param flags the flags for the Intent 
     */ 
       該方法裡對重要參數解釋的都很清楚。所以,建議正在研究java調用monkeyrunner問題的朋友們,不要忘了源碼這個最好的資源。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.