百度传课:http://www.chuanke.com/v1983382-106000-218428.html
一、基于控件
控件:
1、 Spinner
2、 TabHost
3、 Galley
4、 ProgressBar
5、 DatePaicker
6、 CheckBox
7、 RadioButton
8、 ToggleButton
9、 EditTest
二、Robotium原理
1、什么是Robotium
Robotium是一款国外的Android自动化测试框架,主要针对Android平台的应用进行黑盒自动化测试,它提供了模拟各种手势操作(点击、长按、滑动等)、查找和断言机制的API,能够对各种控件进行操作。Robotium结合Android官方提供的测试框架达到对应用程序进行自动化的测试。另外,Robotium 4.0版本已经支持对WebView的操作。Robotium 对Activity,Dialog,Toast,Menu 都是支持的。2014年1月,已经开发了基于eclipse插件的录制器。
2、代码分析
A、Click类
public void clickOnScreen(float x, float y, View view) {
boolean successfull = false;
int retry = 0;
SecurityException ex = null;
while(!successfull && retry < 10) {
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
MotionEvent event = MotionEvent.obtain(downTime, eventTime,
MotionEvent.ACTION_DOWN, x, y, 0);
MotionEvent event2 = MotionEvent.obtain(downTime, eventTime,
MotionEvent.ACTION_UP, x, y, 0);
try{
inst.sendPointerSync(event);
inst.sendPointerSync(event2);
successfull = true;
}catch(SecurityException e){
ex = e;
dialogUtils.hideSoftKeyboard(null, false, true);
sleeper.sleep(MINI_WAIT);
retry++;
View identicalView = viewFetcher.getIdenticalView(view);
if(identicalView != null){
float[] xyToClick = getClickCoordinates(identicalView);
x = xyToClick[0];
y = xyToClick[1];
}
}
}
if(!successfull) {
Assert.fail("Click at ("+x+", "+y+") can not be completed! ("+(ex != null ? ex.getClass().getName()+": "+ex.getMessage() : "null")+")");
}
}
MotionEvent.obtain:
重点看这两个方法:
static MotionEvent |
obtain(long downTime, long eventTime, int action, int pointerCount, PointerProperties[] pointerProperties, PointerCoords[] pointerCoords, int metaState, int buttonState, float xPrecision, float yPrecision, int deviceId, int edgeFlags, int source, int flags) Create a new MotionEvent, filling in all of the basic values that define the motion. |
static MotionEvent |
obtain(long downTime, long eventTime, int action, float x, float y, float pressure, float size, int metaState, float xPrecision, float yPrecision, int deviceId, int edgeFlags) Create a new MotionEvent, filling in all of the basic values that define the motion. |
B、Solo类
面试会问:控件层叠了,怎么搜索到控件是什么。
//将所有的东西得到后放入ArrayList里面,再遍历出来
public <T extends View> boolean searchFor(View view) {
ArrayList<View> views = viewFetcher.getAllViews(true);
for(View v : views){
if(v.equals(view)){
return true;
}
}
return false;
}
C、Search类—阿里高级测试面试题
D、WebView、RobotiumWebClient和WebChromeClient—阿里高级测试面试题2
WebChromeClient used to get information onweb elements by injections of JavaScript.
WebChromeClient在web控件上通过注入js脚本来获取信息。
public void enableJavascriptAndSetRobotiumWebClient(List<WebView> webViews, WebChromeClient originalWebChromeClient){
this.originalWebChromeClient = originalWebChromeClient;
for(final WebView webView : webViews){
if(webView != null){
inst.runOnMainSync(new Runnable() {
public void run() {
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(robotiumWebClient);
}
});
}
}
}