uiautomator:中文输入问题

上一篇 / 下一篇  2014-07-16 14:56:26 / 个人分类:android

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


Uiautomator Unicode Input Helper

(Japanese Version is here.)

Uiautomator Unicode Input Helper enables you to input any Unicode (including non-ASCII) characters by using Android uiautomator.

Uiautomator has UiObject.setText(String) API to set text in an editable field. The API works well if the text consists of only the ASCII character, but can not input any non-ASCII characters, such as Japanese. This limitation has prevented developers of non-English applications from using uiautomator for testing.

Uiautomator Unicode Input Helper solves it by providing an IME, which converts encoded text using only ASCII characters into Unicode text. Modified UTF-7 (RFC 3501) is used for the encoding scheme.

Getting Started

Precaution

Utf7Ime, which is installed by the following instructions, prevents you from inputting any text via software keyboard. Do not use Utf7Ime as a human interface. It is intended for only test automation.

To avoid your confusion, change the default IME to your favorite one after using it:

  1. Show home screen by tapping home key.
  2. Launch Settings.
  3. Open Language & input.
  4. Tap Default belonging to KEYBOARD & INPUT METHODS category.
  5. Select your favorite IME.

Prerequisites

Install Utf7Ime and change the default IME

  • Import Utf7Ime/ directory into your Eclipse workspace. You can import by File -> Import... -> Existing Android Code Into Workspace.
  • Build Utf7Ime project and install it on your Android-powered device (or emulator) for UI testing.
  • Launch Settings App in the device.
  • Open Language & input.
  • Switch on the UTF7 IME for UI Testing checkbox belonging to KEYBOARD & INPUT METHODS category.
  • Tap Default belonging to KEYBOARD & INPUT METHODS category.
  • Change the default IME to Utf7Ime by selecting UTF7 IME for UI Testing.

Prepare your Uiautomator tests

Prepare (or create) your test that runs in the uiautomator framework. If you have not written a test for uiautomator yet, you can see detailed instruction here.

Copy the helper library to your Uiautomator project

Copy helper-library/src/* to <your uiaotomator project>/src/.
Note that a uiautomator project does not recognize any JAR libraries in libs directory.

Usage

Apply Utf7ImeHelper.e() to a string used for UiObject.setText(). Any Unicode (including non-ASCII) characters are accepted.

importjp.jun_nama.test.utf7ime.helper.Utf7ImeHelper;....// gets UiObject which refers to editable textUiObjecteditText=...;// inputs German umlaut characterseditText.setText(Utf7ImeHelper.e("Danke schön"));// inputs Japanese characterseditText.setText(Utf7ImeHelper.e("ありがとう"));....

Note that you can set a string to UiObject.setText() directly as long as the string consists of only the ASCII characters except &.

....UiObjecteditText=...;// inputs ASCII text not containing '&'.// Utf7ImeHelper.e() is not needed.editText.setText("Thank you very much.");// inputs ASCII text containing '&'.// You must use Utf7ImeHelper.e().editText.setText(Utf7ImeHelper.e("fish & chips"));....

Sample

You can see a sample project which resides in UiAutomatorInputSample/ directory. This sample code simulates the following instructions:

  • Press on the HOME button.
  • Launch the "Google" app.
  • Input Japanese sentences into Google search box.
    Result Screenshot

How to run

  • Change the Locale of your testing device to English (United States).
  • Set ANDROID_HOME environment variable properly.
  • Connect your testing device.
  • Change directories to the root of the sample project.
  • Build and run by typing the following commands (ant is required):

    ant clean build install
    adb shell uiautomator runtest UiAutomatorInputSample.jar \
        -c jp.jun_nama.test.utf7ime.sample.UiAutomatorInputTest

The sample code is tested under Nexus7 (2013) based on Android 4.4.

It is not run properly for Android emulators based on Android 4.3 or earlier because the class name of Google search box is different. Rewrite search criteria in the sample code so that it can be run under such a device or emulator.

Note that such restrictions are applied to only the sample code. Utf7Ime and the helper library are run properly under devices supporting uiautomator.

我在尝试时,遇到的问题是编码的问题,汉字输入的是乱码,然后改为UTF-8就ok了,正常的显示输入的汉字或者是汉字加英文。


总结一下步骤:
1:从github上下载Utf7Ime,运行生成apk,安装到手机或者是模拟器,并设置其为默认语言
2:Copy helper-library/src/* to <your uiaotomator project>/src/
notice:要保证你的项目使用的是UTF-8编码
3:创建build.xml文件
android create uitest-project -n *** -t 6 -p e:\workspace\***
4: 编译生成jar
ant build
5:push & run jar
adb push <jar文件路径> data/local/tmp
adb shell uiautomator runtest <jar文件名> -c <工程中的类名,包含包名>



TAG:

 

评分:0

我来说两句

Open Toolbar