【问题标题】:How to inject click event with Android UiAutomation.injectInputEvent如何使用 Android UiAutomation.injectInputEvent 注入点击事件
【发布时间】:2014-06-03 06:24:19
【问题描述】:

我正在安装设备管理员的应用中自动测试流程。要在大多数设备上激活设备管理员(假设在这里我没有一些企业 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);

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

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

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

我没有在屏幕上看到任何点击事件。有任何人对此有经验吗?有没有其他方法可以做我想做的事?据我了解,很少有工具可以做到这一点。

谢谢。

更新 为正确答案添加setSource

【问题讨论】:

  • 我认为您应该取出您编辑的 setSource() 调用。原始问题应该说明问题,其中答案显示解决方案。事实上,比较两者会导致混淆,因为它们都有效。

标签: android testing ui-automation robotium


【解决方案1】:

终于想通了。我将 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();
}

【讨论】:

  • 我喜欢你的 javadoc 和 cmets
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-30
  • 2019-06-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多