Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 自動化測試

Android 自動化測試

編輯:關於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");
		}
		
}




  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved