gpt4 book ai didi

android - 如何使用 Android UiAutomation.injectInputEvent 注入(inject)点击事件

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:50:39 25 4
gpt4 key购买 nike

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

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

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

}

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

我没有在屏幕上看到任何点击事件。这个事情谁有经验?有没有其他方法可以做我想做的事?据我所知,很少有工具可以做到这一点。

谢谢。

更新为正确答案添加了 setSource

最佳答案

终于明白了。我将我的 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();
}

关于android - 如何使用 Android UiAutomation.injectInputEvent 注入(inject)点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23159265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com