WEB开发网
开发学院手机开发Android 开发 android自动化测试中实现长按并拖动 阅读

android自动化测试中实现长按并拖动

 2013-08-19 21:33:11 来源:WEB开发网   
核心提示: android应用自动化过程中,会遇见需要长按并拖动的场景,android自动化测试中实现长按并拖动,例如类似UC浏览器中,长按某个导航中的图标,模拟长按操作eventTime = SystemClock.uptimeMillis() + 1000;//xStop加了10点坐标,获得的View坐标需根据应用实际情

  android应用自动化过程中,会遇见需要长按并拖动的场景,例如类似UC浏览器中,长按某个导航中的图标,使其处于可移动状态,然后再将其移动到另一个地方,与其它导航图标换个位置,在robotium中有个drag(float fromX, float toX, float fromY, float toY,int stepCount)方法,但由于drag没有长按这个步骤,因此不能使应用处于可移动的状态,是没法完成这样的常见而简单的操作的。
drag方法源码实现如下:

	public void drag(float fromX, float toX, float fromY, float toY,
			int stepCount) {
		long downTime = SystemClock.uptimeMillis();
		long eventTime = SystemClock.uptimeMillis();
		float y = fromY;
		float x = fromX;
		float yStep = (toY - fromY) / stepCount;
		float xStep = (toX - fromX) / stepCount;
		MotionEvent event = MotionEvent.obtain(downTime, eventTime,MotionEvent.ACTION_DOWN, fromX, fromY, 0);
		try {
			inst.sendPointerSync(event);
		} catch (SecurityException ignored) {}
		for (int i = 0; i < stepCount; ++i) {
			y += yStep;
			x += xStep;
			eventTime = SystemClock.uptimeMillis();
			event = MotionEvent.obtain(downTime, eventTime,MotionEvent.ACTION_MOVE, x, y, 0);
			try {
				inst.sendPointerSync(event);
			} catch (SecurityException ignored) {}
		}
		eventTime = SystemClock.uptimeMillis();
		event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP,toX, toY, 0);
		try {
			inst.sendPointerSync(event);
		} catch (SecurityException ignored) {}
	}

可以看出其实是通过MotionEvent的ACTION_DOWN模拟屏幕按下操作,ACTION_MOVE模拟手势在屏幕上滑动,ACTION_UP模拟手势离开屏幕,从而完成整个拖动过程,而且其实robotium中的各种点击类方法也都是通过模拟不同的手势完成的。
因此要想完成长按并拖动的操作,只要在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
			}
	}

Tags:android 自动化 实现

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接