如何注入click事件与Android UiAutomation.injectInputEvent [英] How to inject click event with Android UiAutomation.injectInputEvent

查看:3046
本文介绍了如何注入click事件与Android UiAutomation.injectInputEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的自动化我的应用程序流的测试,我安装一个设备管理员。要激活大多数设备的设备管理员(让我们假设我在这里别有一番事业的API,可以让我做这像什么三星提供),系统会显示一个弹出窗口,让那么谁点击激活按钮的用户。

I'm automating the testing of a flow in my app where I install a device administrator. To activate a device administrator on most devices (let's assume here I don't have some enterprise API that lets me do this like what Samsung offers) the system displays a popup to the user who then has to click the "Activate" button.

我使用 Robotium 和Android JUnit来推动我的测试。在正常的测试情况下,可以只与被测应用程序和过程,而不是说拿出任何系统活动进行交互。

I'm using Robotium and Android JUnit to drive my tests. In a normal testing case one can only interact with the app and process under test and not any system activities that come up.

借助 UiAutomation 声称,让您与其他应用程序通过互动借力辅助框架,然后允许一个的注入任意输入事件的。

The UiAutomation claims to allow you to interact with other applications by leveraging the Accessibility Framework, and then allowing one to inject arbitrary input events.

所以 - 这里就是我想要做的:

So - here's what I'm trying to do:

public class AbcTests extends ActivityInstrumentationTestCase2<AbcActivity> {

    private Solo mSolo

    @Override
    public void setUp() {
        mSolo = new Solo(getInstrumentation(), getActivity());

    }

    ...

    public void testAbc(){

        final UiAutomation automation = getInstrumentation().getUiAutomation();         

        MotionEvent motionDown = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), KeyEvent.ACTION_DOWN,
                100,  100, 0);
        motionDown.setSource(InputDevice.SOURCE_TOUCHSCREEN);
        automation.injectInputEvent(motionDown, true)
        MotionEvent motionUp = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), KeyEvent.ACTION_UP,
                100, 100, 0);
        motionUp.setSource(InputDevice.SOURCE_TOUCHSCREEN);
        automation.injectInputEvent(motionUp, true)
        motionUp.recycle();
        motionDown.recycle();
     }

 }

在这个测试运行的系统弹出激活设备管理员处于活动状态,我想只​​要按一下屏幕上。我已经很难$ C $光盘100,100为点击次数这一问题的目的位置,但实际上我将点击屏幕右下角这样我就可以按下按钮。

When this test is run the System popup to "Activate" the device administrator is active, and I want to just click on the screen. I've hardcoded in 100,100 as the position for clicks for the purposes of this question but realistically I'll click in the bottom right corner of the screen so I can hit the button.

我不明白在屏幕上发生的任何点击事件。有没有人有这样的经历?是否有其它方法做我想做的事?从我的理解也有极少数的工具,做到这一点。

I do not get any click events occurring on the screen. Does anyone have experience with this? Are there any alternatives to do what I want to do? From my understanding there are very few tools that do this.

感谢。

更新 新增的SetSource 为正确答案

推荐答案

终于想通了这一点。我比我的MotionEvents两个事件得到调度,当我点击一个按钮,唯一的区别是源。所以,我设置的两个motionEvents源和它的工作。

Finally figured this out. I compared my MotionEvents to the two events that get dispatched when I clicked on a button and the only difference was the source. So, I set the source on the two motionEvents and it worked.

....
motionDown.setSource(InputDevice.SOURCE_TOUCHSCREEN);
....
motionUp.setSource(InputDevice.SOURCE_TOUCHSCREEN);

这是一个完整版本的方法

And here's a full version of the method

//=========================================================================
//==                        Utility Methods                             ===
//=========================================================================
/**
 * Helper method injects a click event at a point on the active screen via the UiAutomation object.
 * @param x the x position on the screen to inject the click event
 * @param y the y position on the screen to inject the click event
 * @param automation a UiAutomation object rtreived through the current Instrumentation
 */
static void injectClickEvent(float x, float y, UiAutomation automation){
    //A MotionEvent is a type of InputEvent.  
    //The event time must be the current uptime.
    final long eventTime = SystemClock.uptimeMillis();

    //A typical click event triggered by a user click on the touchscreen creates two MotionEvents,
    //first one with the action KeyEvent.ACTION_DOWN and the 2nd with the action KeyEvent.ACTION_UP
    MotionEvent motionDown = MotionEvent.obtain(eventTime, eventTime, KeyEvent.ACTION_DOWN,
            x,  y, 0); 
    //We must set the source of the MotionEvent or the click doesn't work.
    motionDown.setSource(InputDevice.SOURCE_TOUCHSCREEN);
    automation.injectInputEvent(motionDown, true);
    MotionEvent motionUp = MotionEvent.obtain(eventTime, eventTime, KeyEvent.ACTION_UP,
            x, y, 0);
    motionUp.setSource(InputDevice.SOURCE_TOUCHSCREEN);
    automation.injectInputEvent(motionUp, true);
    //Recycle our events back to the system pool.
    motionUp.recycle();
    motionDown.recycle();
}

这篇关于如何注入click事件与Android UiAutomation.injectInputEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆