Android 自动化测试

一、基本概念

       自动化测试就是模拟人工测试,例如点击按钮,下滑,点击Menu,退出,这一系列的动作都由代码实现。这样的优点主要是可以反复执行很多次,来观察程序是否出现异常。


二、开源框架

       主要是robotium,网站是http://code.google.com/p/robotium/

         中文网站http://www.robotium.cn/


三、搭建测试环境,并运行第一个自动化测试代码流程,参考:

       http://www.robotium.cn/archives/210


四、模仿上下左右滑动,参考

       http://blog.softteco.com/2011/02/touch-hold-swipe-release-gesture.html


五、模拟下滑,代码:

private void scrollDown() {
		Instrumentation inst = getInstrumentation();
		long downTime = SystemClock.uptimeMillis();
		// event time MUST be retrieved only by this way!
		long eventTime = SystemClock.uptimeMillis();

		// Just init your variables, or create your own coords logic :)
		float xStart = 200;
		float yStart = 900;
		// simulating thick finger touch
		float x0 = 200;
		float y0 = 900;
		float x9 = 200;
		float y9 = 100;
		// release finger, logically to use coords from last movent
		float x10 =200;
		float y10 = 100;

		try {
			// sending event - finger touched the screen
			MotionEvent event = MotionEvent.obtain(downTime, eventTime,
					MotionEvent.ACTION_DOWN, xStart, yStart, 0);
			inst.sendPointerSync(event);
			// sending events - finger is moving over the screen
			eventTime = SystemClock.uptimeMillis();
			event = MotionEvent.obtain(downTime, eventTime,
					MotionEvent.ACTION_MOVE, x0, y0, 0);
			inst.sendPointerSync(event);
			event = MotionEvent.obtain(downTime, eventTime,
					MotionEvent.ACTION_MOVE, x9, y9, 0);
			inst.sendPointerSync(event);
			// release finger, gesture is finished
			event = MotionEvent.obtain(downTime, eventTime,
					MotionEvent.ACTION_UP, x10, y10, 0);
			inst.sendPointerSync(event);
			

		} catch (Exception ignored) {
			//Log.d("gcy111", "run");
		}
		
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值