uiautomator中文输入解决方案

时间:2021-04-23 12:49:46
UiAutomator不支持中文输入。

通过设置中文输入法为默认,UiObject.setText("pinyin ") 的方式,可以实现中文输入,但是只能输入一些固定的词组。

github上发现了一个 utf7ime 的好东西,可以实现中文输入,英文输入,中英文混合输入。简单来说,支持输入任何unicode编码的字符。
原理是:UiObject.setText( String) 只能接受ASCII码,整个过程是输入的unicode编码的字符串decode成ASCIl码,setText接受这些ASCll码再通过utf7ime这个输入法encode成unicode编码的字符串输出。
前置条件:手机装入此输入法并将之设为默认输入法

简单说一说整个过程,

打包下载 https://github.com/sumio/uiautomator-unicode-input-helper
导入其中的Utf7Ime , 生成apk并安装设置成默认输入法
把 helper-library 里面的Utf7ImeHelper.java导入自己的公用方法库,用于把字符串decode成ASCII码
最后生成脚本是这样的:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import android.view.KeyEvent;

import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.core.UiWatcher;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
import com.beetstra.jutf7.Utf7ImeHelper;
/**
* android list
* 创建build文件 android create uitest-project -n RunnerDemo -t 2 -p E:\android_workspace\UiautomatorTest
* 编译生成jar 右击build.xml运行 Ant Build
* 导入jar文件adb push E:\android_workspace\UiautomatorTest\bin\RunnerDemo.jar /sdcard/
* 运行jar文件 adb shell uiautomator runtest <jar文件名> -c <工程中的类名,包含包名>
* adb shell uiautomator runtest /sdcard/filemanager.jar -c com.meizu.filemanager.test.FilemanagerSanityTestCase#test123
*
*
* id=UiAutomatorTestRunner
* test=testCreateContact
* class=com.meizu.test.TestCase
*
* [ro.build.inside.id]: [5.1-20150708021621]这个是prd版本是没有权限写的。的如果数字后面有个-i的话就是user版本的。
*
* 1、研究大小写
* 2、研究utf-7
*
* 注意:robotium框架里:exec()方法里面参数不能加adb shell 否则报错IOExpetion
*
* //如何匹配图标
*
* 1、在github上下载了utf7ime输入法的源码,编译了输入法apk到手机里,把utf7代码包放到项目中,调用里面的utf7imeHelper类里面的e()方法,它的原理是将Unicode码破译成ASCII码后传给UiObject.setText()方法,再由手机里的utf7ime将ASCII编译成Unicode码。
* 2、在统计文件夹下内容个数的时候,多了的.nomedia文件是为了跳过开机媒体扫描加快开机速度减少系统开销的,它不在目录下显示,所以在统计个数时判断一下忽略掉它。
* 3、进行swipe滑动的时候不能取这三个边界值(0,width,heigh),否则滑动无效。可以取(50,width-50,width/2+200,width/2-200)。速度可为10。
*
* 2015年7月9日
* @author niming
*/
public class TestCase extends UiAutomatorTestCase {

private UiDevice uiDevice;
private int width;
private int height;

@Override
protected void setUp() throws Exception {
super.setUp();
uiDevice = getUiDevice();
width = uiDevice.getDisplayWidth();
height = uiDevice.getDisplayHeight();
uiDevice.pressHome();
}

@Override
protected void runTest() throws Throwable {
try {
super.runTest();
System.out.println("true\ntrue\ntrue");
} catch (Throwable e) {
System.out.println("false\nfalse\nfalse");
throw new Throwable(e);
}
}

@Override
protected void tearDown() throws Exception {
super.tearDown();
}
new UiObject(new UiSelector().text(Utf7ImeHelper.e("设置"))).click(); 
下一步考虑如何将这个功能集成到脚本录制工具中去。

原文:http://testerhome.com/topics/408


MEIZU PRO7