新年伊始,干货不断
新的一年里,heyniu祝大家心想事成、身体健康、阖家欢乐,还有鸡年大吉吧[手动滑稽]
众所周知,Robotium的2大痛处 >> 重签名、跨应用
重签名: 在我看来这个不是大问题,找研发要签名,然后签名我们的测试应用,就可以保持与被测应用签名的一致性。
跨应用:这个在之前是比较蛋疼的,网上的方案也是大多不适用。今天我就为这个给大家分享一下我的方案。
灵感来源
抢红包应用层出不穷,到底它们是怎样抢到微信的红包呢?带着这个疑问,查阅了一下资料,发现它们是通过Android的辅助功能来实现的,于是我的方案是UiAutomation + Accessibility
重视Robotium
2大痛处都解决了,那么优势就来啦,比Appium更快的速度,且与被测应用共享数据 ,这是我选择它的原因。
原理浅析
Robotium基于Instrumentation的二次封装,然而UiAutomation 也能通过instrumentation.getUiAutomation()
拿到。顺带提一下Uiautomator也是基于UiAutomation 的封装。
UiAutomation 跨应用操作三大利器:
setOnAccessibilityEventListener()
开启Accessibility
executeShellCommand()
执行shell命令(权限比Runtime.getRuntime().exec()
高,相当于adb shell)
injectInputEvent()
注入事件,比如点击。
Accessibility查找控件的2种方式:
findAccessibilityNodeInfosByViewId()
通过完整的资源查找
findAccessibilityNodeInfosByText()
通过文本查找
以上具体的方法查阅API描述即可。
举一反三
目前已实现跨应用点击、输入文本、点击通知栏、拍照、授权、QQ登录等等,其他跨应用场景也是类似。只要掌握原理浅析列出的几个核心方法,我相信跨应用处理已经不是问题了,这里只是抛砖引玉。
场景实例
点击
/**
* Work across application boundaries for click.
* @param x the x coordinate
* @param y the y coordinate
*/
public void acrossForClick(float x, float y){
MotionEvent motionDown = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), KeyEvent.ACTION_DOWN,
x, y, 0);
motionDown.setSource(InputDevice.SOURCE_TOUCHSCREEN);
uiAutomation.injectInputEvent(motionDown, true);
MotionEvent motionUp = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), KeyEvent.ACTION_UP,
x, y, 0);
motionUp.setSource(InputDevice.SOURCE_TOUCHSCREEN);
uiAutomation.injectInputEvent(motionUp, true);
motionUp.recycle();
motionDown.recycle();
}
拍照
/**
* Work across application boundaries for camera.
* @param viewId The fully qualified resource name of the view id to find. e.g: com.sec.android.app.camera:id/okay
*/
public void acrossForCamera(String viewId){
Log.d(LOG_TAG, "acrossForCamera()");
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) return;
UiAutomation uiAutomation = instrumentation.getUiAutomation();
uiAutomation.setOnAccessibilityEventListener(new UiAutomation.OnAccessibilityEventListener() {
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) {
if (viewId.contains(event.getPackageName())) {
if (event.getSource() != null) {
List infoList = event.getSource().findAccessibilityNodeInfosByViewId(viewId);
if (infoList == null || infoList.isEmpty()) return;
performClick(infoList.get(0));
}
}
}
}
});
sleep(2000);
uiAutomation.executeShellCommand("input keyevent 27");
}
授权(核心代码)
/**
* Requests permissions to be granted to this application.
*/
public void requestPermissions(){
if (Build.VERSION.SDK_INT >= 23) {
String[] permissions = checkPermissions();
if (permissions == null || permissions.length == 0) return;
final String manufacturer = getManufacturer();
ActivityCompat.requestPermissions((Activity) context, permissions, 10000);
UiAutomation uiAutomation = instrumentation.getUiAutomation();
uiAutomation.setOnAccessibilityEventListener(new UiAutomation.OnAccessibilityEventListener() {
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
android.util.Log.d(LOG_TAG, "UiAutomation: " + event.toString());
if (manufacturer.toLowerCase().contains("mi")) {
handlePermissions(event, PACKAGE_INSTALLER_XIAOMI, PERMISSION_ALLOW_ID_XIAOMI);
} else handlePermissions(event, PACKAGE_INSTALLER, PERMISSION_ALLOW_ID);
}
});
}
}
QQ登录
/**
* Work across application boundaries for QQ login.
*/
public void acrossForQQLogin(String account, String password){
boolean installed = isInstalled(QQ);
if (!installed) {
Log.w(LOG_TAG, "QQ is not installed.");
return;
}
uiAutomation.setOnAccessibilityEventListener(new UiAutomation.OnAccessibilityEventListener() {
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
Log.d(LOG_TAG, "Event: " + event.toString());
if (event.getEventType() == TYPE_WINDOW_STATE_CHANGED && QQ.contains(event.getPackageName())){
handleQQLogin(event, account, password);
handleAuthorization(event);
}
}
});
}
private void handleAuthorization(AccessibilityEvent event) {
if (event.getClassName().toString().contains("com.tencent.open.agent.AuthorityActivity")) {
Log.i(LOG_TAG, "QQ Login: " + event.toString());
sleep(2000);
AccessibilityNodeInfo nodeInfo = uiAutomation.getRootInActiveWindow();
IterationNode(nodeInfo, "android.widget.Button");
}
}
/**
* Loop through the view and click.
* @param nodeInfo node
* @param name class name, e.g: android.widget.Button
*/
private void IterationNode(AccessibilityNodeInfo nodeInfo, String name) {
for (int i = 0; i < nodeInfo.getChildCount(); i ++){
AccessibilityNodeInfo node = nodeInfo.getChild(i);
Log.i(LOG_TAG, "QQ Login nodeInfo.getChild(i): " + node.getClassName());
if(name.contains(node.getClassName())) {
// Click the authorization button.
performClick(node);
break;
} else IterationNode(node, name);
}
}
private void handleQQLogin(AccessibilityEvent event, String account, String password) {
if (event.getClassName().toString().contains("com.tencent.qqconnect.wtlogin.Login")) {
AccessibilityNodeInfo nodeInfo = uiAutomation.getRootInActiveWindow();
List infoList = nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mobileqq:id/account");
if (infoList == null || infoList.isEmpty()) return;
// Click the account edit text and enter text.
performClick(infoList.get(0));
acrossForEnterText(account);
infoList = nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mobileqq:id/password");
if (infoList == null || infoList.isEmpty()) return;
// Click the password edit text and enter text.
performClick(infoList.get(0));
acrossForEnterText(password);
sleep(4000);
IterationNode(nodeInfo, "android.widget.Button");
}
}
部分演示及完整代码
百度网盘:链接: http://pan.baidu.com/s/1mh6bdJ6 密码: ffnp