jar*_*etz 6 testing android ui-automation robotium
我在我的应用程序中自动测试流程,我在那里安装了设备管理员.要激活大多数设备上的设备管理员(我们假设我没有一些企业API可以让我像三星提供的那样),系统会向用户显示弹出窗口,然后用户必须单击"激活"按钮.
我正在使用Robotium和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);
// 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();
}
}
Run Code Online (Sandbox Code Playgroud)
运行此测试时,系统弹出"激活"设备管理员处于活动状态,我只想单击屏幕.为了解决这个问题的目的,我已经将100,100硬编码为点击位置,但实际上我会点击屏幕的右下角,这样我就可以点击按钮了.
我没有在屏幕上发生任何点击事件.有任何人对此有经验吗?有什么选择可以做我想做的事吗?根据我的理解,很少有工具可以做到这一点.
谢谢.
更新setSource为正确答案
添加
终于搞清楚了.我将MotionEvents与我点击按钮时调度的两个事件进行了比较,唯一的区别是源.所以,我在两个motionEvents上设置了源码并且它有效.
....
motionDown.setSource(InputDevice.SOURCE_TOUCHSCREEN);
....
motionUp.setSource(InputDevice.SOURCE_TOUCHSCREEN);
Run Code Online (Sandbox Code Playgroud)
这是该方法的完整版本
//=========================================================================
//== 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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3745 次 |
| 最近记录: |