UiAutomator:如何使用uiautomator实现粘贴动作?

问题描述 投票:0回答:3

我需要在EditText中输入非ASCII字符(如中文字符),我发现UiObject.setText()方法无法完成这项工作。

所以我得到一个方法:将字符复制到剪贴板,然后将其粘贴到EditText。现在我已经实现了copy work,但不知道如何使用代码实现粘贴操作。

我在网上搜索,发现使用热键可以实现paste动作:“menu”+“v”

所以我去了UiDevice api,找到一个方法:按下Keycode(),但它只能按一次键码。

有谁知道如何使用uiautomator同时按“菜单”和“v”或有一些原始代码来实现这一目标?

非常感谢!

android uiautomator android-uiautomator uidevice
3个回答
0
投票

你也有pressMenu()方法。 see here

我想你也可以用你复制的文本setText。


0
投票

我已经解决了这个问题。见下面的代码:

public class MyTest  extends TestCase{

    /**
    * Paste text to an EditText feild which is currentlly get focused.
    *
    * @param: text the text(Non-ASCII) you want to paste into EditText feild. 
    */
    IClipboard clipboard = IClipboard.Stub.asInterface(ServiceManager.getService(Context.CLIPBOARD_SERVICE));
    IInputManager iInputManager = IInputManager.Stub.asInterface(ServiceManager.getService(Context.INPUT_SERVICE)); 
    private void pastText(String text) throws UiObjectNotFoundException{
        try {
            //copy the text to clipboard.
            clipboard.setPrimaryClip(ClipData.newPlainText("NonASCII", text), text);   

            //inject event: press Menu + V         
            iInputManager.injectInputEvent(
                new KeyEvent(android.os.SystemClock.uptimeMillis(), 
                             android.os.SystemClock.uptimeMillis(), 
                             KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU, 0),1); 
            iInputManager.injectInputEvent(
                new KeyEvent(android.os.SystemClock.uptimeMillis(), 
                             android.os.SystemClock.uptimeMillis(), 
                             KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_V, 0),1);              
            iInputManager.injectInputEvent(
                new KeyEvent(android.os.SystemClock.uptimeMillis(), 
                             android.os.SystemClock.uptimeMillis(), 
                             KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MENU, 0),1);
            iInputManager.injectInputEvent(
                new KeyEvent(android.os.SystemClock.uptimeMillis(), 
                             android.os.SystemClock.uptimeMillis(), 
                             KeyEvent.ACTION_UP, KeyEvent.KEYCODE_V, 0),1);              

            //After "Menu"+"V" pressed, A "Menu" will show if exist in current Activicy.
            //Then press menu again, to make it down just for bug fixing.
            sleep(300);
            iInputManager.injectInputEvent(
                new KeyEvent(android.os.SystemClock.uptimeMillis(), 
                             android.os.SystemClock.uptimeMillis(), 
                             KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU, 0),1); 
            iInputManager.injectInputEvent(
                new KeyEvent(android.os.SystemClock.uptimeMillis(), 
                             android.os.SystemClock.uptimeMillis(), 
                             KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MENU, 0),1);

        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

}

谢谢。


0
投票

这对我有用:

UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).pressKeyCode(KeyEvent.KEYCODE_V, KeyEvent.META_CTRL_MASK)

© www.soinside.com 2019 - 2024. All rights reserved.