我在我的应用程序中自动测试一个流,在那里我安装了一个设备管理员。为了在大多数设备上激活设备管理员(假设这里我没有企业API让我像三星提供的那样),系统会向用户显示一个弹出窗口,用户随后必须单击“激活”按钮。
我使用罗布托和Android JUnit来驱动我的测试。在一个正常的测试用例中,一个人只能与被测试的应用程序和进程进行交互,而不能与任何系统活动进行交互。
UiAutomation声称允许您通过利用无障碍框架与其他应用程序交互,然后允许一个应用程序与注入任意输入事件交互。
所以-我想做的是:
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
发布于 2014-04-18 21:55:28
终于弄明白了。我将我的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();
}
https://stackoverflow.com/questions/23159265
复制相似问题