天天看點

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

         經過一位朋友的提示說,現在高版本的方法已經變了,我就按照他的提示,上網搜了一下需要的類,測試通過後,特寫此補充篇總結一下。

上次使用的是android sdk tools路徑下的lib裡面的4個包:ddmlib.jar,guavalib.jar,monkeyrunner.jar,sdklib.jar.而更新後的版本需要添加另外一個包就是:chimpchat.jar,monkerunner.jar這個包倒不是必須的了。另外,低版本中是用AdbMonkeyDevice實作IMonkeyDevice,高版本中沒有這兩個類了,用的AdbChimpDevice和IchimpDevice。

       下面還是用以前的測試類,進行稍微改變一下,就可以看出兩者的不同:

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問題的朋友們,不要忘了源碼這個最好的資源。

本文轉自 風泊海上 51CTO部落格,原文連結:http://blog.51cto.com/fengbohaishang/1071155

繼續閱讀