[www.infoshare.cc]【uiautomator】输入中文(输入法安装+测试代码)

来源:http://www.infoshare.cc

备注

1、Utf7Ime官网下载需要增加修改部分文件,因此附上地址:http://download.csdn.net/detail/victoria_vicky/8799851,可直接导入Eclipse使用

2、src下载地址:http://download.csdn.net/detail/victoria_vicky/8799973

3、将来可能用到的

一、安装输入法

1、下载Utf7Ime.rar,解压缩到本地文件夹

2、导入到Eclipse:File->Import->General->Existing Projects into Workspace->Next->Select Root directory->Browse选择之前Utf7Ime解压地址->Finish

3、Eclipse:右击Utf7Ime工程->Run As->Android Application,安装到本地手机

4、手机设置(以小米2S为例):设置->语言和输入法->键盘和输入法->将默认修改为UTF7 IME for UI Testing

二、测试工程中加入对应代码

1、将src里面的所有文件拷贝到测试项目src下,并将其导入到具体测试项目中,如下图所示

2、以下为本人使用写的小例子

package com.beixun.test;
import java.io.IOException;
import jp.jun_nama.test.utf7ime.helper.Utf7ImeHelper;
import android.os.RemoteException;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;

public class test extends UiAutomatorTestCase {
    
    //input userName and Password,then login the APP
    public void test() throws InterruptedException,  RemoteException, UiObjectNotFoundException{
        FindAndOpenApp();    
        loginAPP(Utf7ImeHelper.e("你好"),Utf7ImeHelper.e("111111"));        
    }
    //open beixun APP
    private void FindAndOpenApp() throws UiObjectNotFoundException, InterruptedException, RemoteException{
        UiDevice device=getUiDevice();
        
        if(!device.isScreenOn()){
            //wake up screen
            device.wakeUp();
            device.isScreenOn();
            assertTrue("screen can not wake up",device.isScreenOn());
            //unlock screen
            getUiDevice().swipe(532, 1870, 555, 402,5);
        }
        
        //press on Home button
        getUiDevice().pressHome();
        
        //open beixun app
        try {
            Runtime.getRuntime().exec("am start -n com.luyankeji.beixun/.LoginActivity");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }        
    }
    //login beixun APP
    private void loginAPP(String UserName,String Password) throws UiObjectNotFoundException, InterruptedException{
        //input userName
        UiObject etUserName=new UiObject(new UiSelector().className("android.widget.EditText").text("请输入账号或手机号"));
        assertEquals("请输入账号或手机号",etUserName.getText());
        etUserName.click();
        etUserName.clearTextField();
        etUserName.setText(UserName);
        Thread.sleep(1000);
        
        //input password
        UiObject ll4=new UiObject(new UiSelector().className("android.widget.LinearLayout").index(3));
        UiObject etPassword=ll4.getChild(new UiSelector().className("android.widget.EditText"));
        assertTrue("etPassword can not been found",etPassword.exists());
        etPassword.click();
        etPassword.clearTextField();
        etPassword.setText(Password);
        Thread.sleep(1000);
                
        //click on login button
        UiObject btLoginButton=new UiObject(new UiSelector().className("android.widget.Button").text("登陆"));
        assertEquals("登陆", btLoginButton.getText());
        btLoginButton.clickAndWaitForNewWindow();
        Thread.sleep(2000);            
        System.out.println("Login beixunAPP success");
    }    
}

3、补充下原理知识

1、在Uiautomator中,**.setText("**"),通常用来填充文本,但是setText("")只能接受ASCII码;

2、utf7ime可以将输入的Unicode编码的字符串转换成ASCII码,setText接受ASCII码再通过utf7ime这个输入法转换成Unicode码输出;

3、Unicode是国际组织制定的可以容纳世界上所有文字和符号的字符编码方案

原文地址:https://www.cnblogs.com/wonderful0714/p/4571941.html