yetong1985@126

Robotium5.3.1的新功能

robotium5.3.1已经发布了,下载地址为:https://dl.bintray.com/robotium/generic/robotium-solo-5.3.1.jar

帮助文档下载地址为:https://dl.bintray.com/robotium/generic/robotium-solo-5.3.1-javadoc.jar

增加了如下的功能:

1、setWiFiData  设置wifi开启或关闭

/**
* Sets if wifi data should be turned on or off. Requires android.permission.CHANGE_WIFI_STATE in the AndroidManifest.xml of the application under test.
* 设置WiFi开启或关闭, true为开启,false为关闭
*
* @param turnedOn true if mobile wifi is to be turned on and false if not
*/

public void setWiFiData(Boolean turnedOn){
systemUtils.setWiFiData(turnedOn);
}

/**
* Sets if wifi data should be turned on or off. Requires android.permission.CHANGE_WIFI_STATE in the AndroidManifest.xml of the application under test.
*
*
* @param turnedOn true if mobile wifi is to be turned on and false if not
*/

public void setWiFiData(Boolean turnedOn){
WifiManager wifiManager = (WifiManager)instrumentation.getTargetContext().getSystemService(Context.WIFI_SERVICE);
try{
wifiManager.setWifiEnabled(turnedOn);
}catch(Exception e){
e.printStackTrace();
}
}

2、setMobileData  设置数据网络开启或关闭

/**
* 设置数据网络开启或关闭,true为开启,false为关闭
* Sets if mobile data should be turned on or off. Requires android.permission.CHANGE_NETWORK_STATE in the AndroidManifest.xml of the application under test.
* NOTE: Setting it to false can kill the adb connection.
*
* @param turnedOn true if mobile data is to be turned on and false if not
*/

public void setMobileData(Boolean turnedOn){
systemUtils.setMobileData(turnedOn);
}

/**
* Sets if mobile data should be turned on or off. Requires android.permission.CHANGE_NETWORK_STATE in the AndroidManifest.xml of the application under test.
*
* @param turnedOn true if mobile data is to be turned on and false if not
*/

public void setMobileData(Boolean turnedOn){
ConnectivityManager dataManager=(ConnectivityManager)instrumentation.getTargetContext().getSystemService(Context.CONNECTIVITY_SERVICE);

Method dataClass = null;
try {
dataClass = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
dataClass.setAccessible(true);
dataClass.invoke(dataManager, turnedOn);
} catch (Exception e) {
e.printStackTrace();
}
}


这两个功能的添加,方便测试人员在脚本中控制网络的状态,要求就是要在测试工程的AndroidManifest.xml文件中添加android.permission.CHANGE_NETWORK_STATE权限。尤其是setMobileData这个功能,真的很赞,因为自从android2.3开始,google就不开放权限让开发者在代码中开启setMobileData了。在android2.2中可以这么实现数据网络的开启:

/**
* 打开数据网络连接
* @param context
* @throws ClassNotFoundException
*/
public void setDataConnect () throws ClassNotFoundException {
/*
* 调用TelephonyManager的隐藏API是先参考
* Framework的/base/telephony/java/com/android/internal/telephony/ITelephony.aidl,
* 然后自己实现一个ITelephony.aidl,最后在TelephonyManager中通过反射机制实例化自定义的ITelephony,
* 实例化之后就可以调用ITelephony里面的函数
*/
Method dataConnSwitchmethod = null;
Class telephonyManagerClass = null;
Class ITelephonyClass = null;
Object ITelephonyStub = null;
Method getITelephonyMethod = null;

// 获取系统的电话服务对象
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
try {
// 反射获取TelephonyManager类
telephonyManagerClass = Class.forName(telephonyManager.getClass().getName());
/*
* 反射获取TelephonyManager类中发私有方法 private Itelephony getITelephony()。该方法返回
* ITelephony.Stub.asInterface(ServiceManager
* .getService(Context.TELEPHONY_SERVICE))
*
* ITelephony.Stub对象
*/
getITelephonyMethod = telephonyManagerClass
.getDeclaredMethod("getITelephony");
// 设置私有方法可访问
getITelephonyMethod.setAccessible(true);
// 调用getITelephony()方法获取ITelephony的代理
ITelephonyStub = getITelephonyMethod.invoke(telephonyManager);
Log.i(TAG, "ITelephonyStub = " + ITelephonyStub.toString());

// 获取ITelephony类
ITelephonyClass = Class.forName(ITelephonyStub.getClass().getName());
// 获取 开启数据网络连接的方法enableDataConnectivity
dataConnSwitchmethod = ITelephonyClass
.getDeclaredMethod("enableDataConnectivity");
Log.i(TAG, "ITelephonyClass = " + ITelephonyClass.getName());
Log.i(TAG, "dataConnSwitchmethod = " + dataConnSwitchmethod.getName());

dataConnSwitchmethod.setAccessible(true);
dataConnSwitchmethod.invoke(ITelephonyStub, null);

Thread.sleep(60000);
if (telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED) {
Log.i(TAG, "数据网络已连接");
} else {
Log.i(TAG, "数据网络未连接");
}
} catch (NoSuchMethodException e) {
Log.i(TAG, "NoSuchMethodException");
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
Log.i(TAG, "IllegalAccessException" );
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
Log.i(TAG, "IllegalArgumentException" );
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
Log.i(TAG, "InvocationTargetException" + e.getTargetException());

// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
Log.i(TAG, "InterruptedException");
// TODO Auto-generated catch block
e.printStackTrace();
}
}


robotium5.3.1直接为我们提供了setMobileData方法就可以直接开启数据网络,实在方便。顺便提一下,ConnectivityManager类中的setMobileDataEnable()方法在SDK中是没有的,如果想用需要把工程放到源码环境中编译,并且需要在Manifest文件中加入android:sharedUserId=android.uid.system属性。这里robotium应该是获取了系统级权限,才能打开数据网络,但robotium究竟是如何做到获取权限的,笔者尚不清楚。

         至于开启WiFi功能,即使没添加,我们自己去写代码,也是很简单的事情。在此,必须要说明的是,不要简单的以为在AndroidManifest.xml文件中添加了权限,就一定可以使用这两个功能了。笔者在上一篇《关于robitum自动化测试获取权限报错的问题 》中有讲到,测试工程所能够支配的权限依赖于被测工程所开通的权限。也就是说,如果被测功能没有开通修改网络状态的权限,那么这这两个新功能就无法使用了。


3、getView(Object tag)  获取具有指定标签的view

      getView(Object tag, int index)  获取满足指定标签的第index个view

/**
* Returns a View matching the specified tag.
*
* @param tag the tag of the {@link View} to return
* @return a {@link View} matching the specified id
* @see {@link View#getTag()}
*/

public View getView(Object tag){
return getView(tag, 0);
}

/**
* Returns a View matching the specified tag and index.
*
* @param tag the tag of the {@link View} to return
* @param index the index of the {@link View}. {@code 0} if only one is available
* @return a {@link View} matching the specified id and index
* @see {@link View#getTag()}
*/

public View getView(Object tag, int index){
View viewToReturn = getter.getView(tag, index);

if(viewToReturn == null) {
int match = index + 1;
if(match > 1){
Assert.fail(match + " Views with id: '" + tag + "' are not found!");
}
else {
Assert.fail("View with id: '" + tag + "' is not found!");
}
}
return viewToReturn;
}


这个Tag标签 是什么东东呢?Android的view的setTag和getTag方法,有时候一个view不仅仅是为了显示一些字符串、图片,还需要他们携带一些其他的数据以便对该view的识别或者其他操作。于是android 的设计者们就创造了setTag(Object)方法来存放一些数据和view绑定,可以理解为这个是view 的标签,通常用于listview的缓存convertView。


4、waitForView(Object tag)

      waitForView(Object tag, int minimumNumberOfMatches, int timeout)

      waitForView(Object tag, int minimumNumberOfMatches, int timeout, boolean scroll)

等待指定标签的view出现


另外,在robotium5.2.1中已经添加了解锁屏幕锁的方法  unlockScreen()

/**
* Unlocks the lock screen.
*/

public void unlockScreen(){
final Activity activity = activityUtils.getCurrentActivity(false);
instrumentation.runOnMainSync(new Runnable() {
@Override
public void run() {
if(activity != null){
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
}
}
});
}


评论

热度(1)