如何使用Android UiAutomation.injectInputEvent注入click事件

时间:2014-04-18 17:41:40

标签: android testing ui-automation robotium

我在我安装设备管理员的应用程序中自动测试流程。要激活大多数设备上的设备管理员(我们假设我没有一些企业API可以让我像三星提供的那样),系统会向用户显示弹出窗口,然后用户必须单击“激活”按钮。

我正在使用Robotium和Android JUnit来推动我的测试。在正常的测试案例中,人们只能与测试中的应用程序和流程进行交互,而不能与出现的任何系统活动进行交互。

UiAutomation声称允许您通过利用Accessibility Framework与其他应用程序进行互动,然后允许其inject arbitrary input events

所以 - 这就是我要做的事情:

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);

        // This line was added in to give a complete solution
        motionDown.setSource(InputDevice.SOURCE_TOUCHSCREEN);

        automation.injectInputEvent(motionDown, true)
        MotionEvent motionUp = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), KeyEvent.ACTION_UP,
                100, 100, 0);

        // This line was added in to give a complete solution
        motionUp.setSource(InputDevice.SOURCE_TOUCHSCREEN);

        automation.injectInputEvent(motionUp, true)
        motionUp.recycle();
        motionDown.recycle();
     }

 }

运行此测试时,系统弹出“激活”设备管理员处于活动状态,我只想单击屏幕。为了解决这个问题的目的,我已经将100,100硬编码为点击位置,但实际上我会点击屏幕的右下角,这样我就可以点击按钮了。

我没有在屏幕上发生任何点击事件。有任何人对此有经验吗?有什么选择可以做我想做的事吗?根据我的理解,很少有工具可以做到这一点。

感谢。

更新 为正确答案添加了setSource

1 个答案:

答案 0 :(得分:7)

终于弄明白了。我将MotionEvents与我点击按钮时调度的两个事件进行了比较,唯一的区别就是源。所以,我在两个motionEvents上设置了源码并且它有效。

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

这是方法的完整版本

//=========================================================================
//==                        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();
}