Android自动化测试中实现长按并拖动
作者:网络转载 发布时间:[ 2013/8/26 13:54:31 ] 推荐标签:
因此要想完成长按并拖动的操作,只要在ACTION_DOWN之后,停留一段时间即可模拟长按操作。
/**
* 实现将一个视图拖动到另一个视图所在的位置
* @param viewFrom 起始View
* @param viewTo 终点View
* @throws Exception
*/
public void clickLongAndDrag(View viewFrom,View viewTo) throws Exception {
//获得视图View中手机屏幕上的x、y坐标
final int[] location = new int[2];
final int[] location2 = new int[2];
viewFrom.getLocationOnScreen(location);
viewTo.getLocationOnScreen(location2);
float xStart=location[0];
float yStart=location[1];
float xStop=location2[0];
float yStop=location2[1];
Log.i(TAG, "xStart:"+String.valueOf(xStart));
Log.i(TAG, "yStart:"+String.valueOf(yStart));
Log.i(TAG, "xStop:"+String.valueOf(xStop));
Log.i(TAG, "yStop:"+String.valueOf(yStop));
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
try{
MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, xStart+10f, yStart+10f, 0);
inst.sendPointerSync(event);
//event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, xStart+10f+1.0f, yStart+10f+1.0f, 0);
//inst.sendPointerSync(event);
//Thread.sleep(1000);
//延迟一秒,模拟长按操作
eventTime = SystemClock.uptimeMillis() + 1000;
//xStop加了10点坐标,获得的View坐标需根据应用实际情况稍做一点调整
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, xStop+10f, yStop+50f, 0);
inst.sendPointerSync(event);
eventTime = SystemClock.uptimeMillis() + 1000;
//又再小小移动了一次,不这么做的话可以无法激活被测应用状态,导致View移动后又回复到原来位置
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, xStop+10f, yStop+10f, 0);
inst.sendPointerSync(event);
eventTime = SystemClock.uptimeMillis() + 1000;
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, xStop+10f, yStop+10f, 0);
inst.sendPointerSync(event);
}catch (Exception ignored) {
// Handle exceptions if necessary
}
}
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11