首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Android UiAutomation.injectInputEvent注入单击事件

如何使用Android UiAutomation.injectInputEvent注入单击事件
EN

Stack Overflow用户
提问于 2014-04-18 17:41:40
回答 1查看 4.9K关注 0票数 6

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

我使用罗布托和Android JUnit来驱动我的测试。在一个正常的测试用例中,一个人只能与被测试的应用程序和进程进行交互,而不能与任何系统活动进行交互。

UiAutomation声称允许您通过利用无障碍框架与其他应用程序交互,然后允许一个应用程序与注入任意输入事件交互。

所以-我想做的是:

代码语言:javascript
运行
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-18 21:55:28

终于弄明白了。我将我的MotionEvents与当我单击按钮时被分派的两个事件进行比较,唯一的区别是源。因此,我在两个motionEvents上设置了源代码,它起了作用。

代码语言:javascript
运行
复制
....
motionDown.setSource(InputDevice.SOURCE_TOUCHSCREEN);
....
motionUp.setSource(InputDevice.SOURCE_TOUCHSCREEN);

下面是这个方法的完整版本

代码语言:javascript
运行
复制
//=========================================================================
//==                        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();
}
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23159265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档