《Android4.3引入的UiAutomation新框架官方简介》
《UIAutomator源码分析之启动和运行》
《UiAutomator源码分析之UiAutomatorBridge框架》
《UiAutomator源码分析之注入事件》
《UiAutomator源码分析之获取控件信息》
从上面的一系列文章可以看到UiAutomator运用UiAutomation框架进行UI自动化测试是做了很多工作,进行了很多高层的封装来方便用户使用的。而Robotium仅仅是引入了获取UiAutomation的实例这个api来暴露给用户使用,一个方面,当然没有高层的封装提供了很多自由,但是也是这些自由让你想快速开发脚本无所适从!Robotium现阶段(5.2.1)对比UiAutomator或者Appium在使用UiAutomation来测试UI就好比,Robotium相当于一个原始社会的人自由的披着件兽皮两手空空的在原始森林中自由游猎,碰到猛兽可以自由的选择工具随意组装来进行猎杀,但很有可能工具没有组装好怪兽却先把你给吃了;UiAutomator相当于一个现代的人全副武装AK47的在原始森林根据GPS定位如履薄冰的向目标猎物靠近来猎杀猎物。两者都是使用最终由化学元素组成的工具来猎杀猎物,但早已高层封装好的ak47和你临时抱佛脚去凭空组建个弓从效率上又怎么能比呢。所以这里我怀疑Robotium可能就提供这个接口就算了,不会再做上层的封装,因为UiAutomator已经做了,人家UiAutomator是google自家的,什么时候又改动人家最清楚,你怎么跟得住人家呢?况且它从Instrumentation的不可跨进程到提供了一个跨进程的突破口,也给了确实需要跨进程调用的用户的一个突破口,不提供太多的封装还能美其名曰“自由”了。注意,仅仅是我自己的猜想了,如果Robotium往后真对UiAutomation做高成封装的话就当我发神经得了。
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package come.example.android.notepad.test;
import android.test.ActivityInstrumentationTestCase2;
import com.example.android.notepad.NotesList;
import com.example.android.notepad.NoteEditor;
import com.example.android.notepad.NotesList;
import com.example.android.notepad.R;
import android.app.Activity;
import android.app.Instrumentation;
import android.app.Instrumentation.ActivityMonitor;
import android.content.Intent;
import android.os.SystemClock;
import android.test.InstrumentationTestCase;
import android.view.KeyEvent;
import android.widget.TextView;
/**
* Make sure that the main launcher activity opens up properly, which will be
* verified by {@link #testActivityTestCaseSetUpProperly}.
*/
public class NotePadTest extends ActivityInstrumentationTestCase2<NotesList> {
NotesList mActivity = null;
/**
* Creates an {@link ActivityInstrumentationTestCase2} for the {@link NotesList} activity.
*/
public NotePadTest() {
super(NotesList.class);
}
//private static Instrumentation instrumentation = new Instrumentation();
@Override
protected void setUp() throws Exception {
super.setUp();
//Start the NotesList activity by instrument
Intent intent = new Intent();
intent.setClassName("com.example.android.notepad", NotesList.class.getName());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Instrumentation inst = getInstrumentation();
mActivity = (NotesList) inst.startActivitySync(intent);
}
@Override
protected void tearDown() {
mActivity.finish();
try {
super.tearDown();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Verifies that the activity under test can be launched.
*/
/*
public void testActivityTestCaseSetUpProperly() {
assertNotNull("activity should be launched successfully", getActivity());
}
*/
public void testActivity() throws Exception {
//Add activity monitor to check whether the NoteEditor activitys ready
ActivityMonitor am = getInstrumentation().addMonitor(NoteEditor.class.getName(), null, false);
//Evoke the system menu and press on the menu entry "Add note";
getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
getInstrumentation().invokeMenuActionSync(mActivity, R.id.menu_add, 0);
//Direct to the NoteEditor activity
Activity noteEditorActivity = getInstrumentation().waitForMonitorWithTimeout(am, 60000);
assertEquals(NoteEditor.class,noteEditorActivity.getClass());
SystemClock.sleep(3000);
//assertEquals(true, getInstrumentation().checkMonitorHit(am, 1));
TextView noteEditor = (TextView) noteEditorActivity.findViewById(R.id.note);
//Get the text directly, DONT need to runOnMainSync at all!!!
String text = noteEditor.getText().toString();
assertEquals(text,"");
//runOnMainSync to change the text
getInstrumentation().runOnMainSync(new PerformSetText(noteEditor,"Note1"));
//inject events to change the text
getInstrumentation().sendCharacterSync(KeyEvent.KEYCODE_1);
getInstrumentation().sendCharacterSync(KeyEvent.KEYCODE_2);
getInstrumentation().sendCharacterSync(KeyEvent.KEYCODE_P);
getInstrumentation().sendStringSync("gotohell");
//getInstrumentation().callActivityOnPause(noteEditorActivity);
Thread.sleep(5000);
//getInstrumentation().callActivityOnResume(noteEditorActivity);
//Save the new created note
getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
getInstrumentation().invokeMenuActionSync(noteEditorActivity, R.id.menu_save, 0);
}
private class PerformSetText implements Runnable {
TextView tv;
String txt;
public PerformSetText(TextView t,String text) {
tv = t;
txt = text;
}
public void run() {
tv.setText(txt);
}
}
}
trueTechArticleRobotium源码分析之Instrumentation进阶,robotium 在分析Robotium的运行原理之前,我们有必要先搞清楚Instrumentation的一些相关知识点,因为Robotium就...
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/ruanjian/article-26961-11.html
过于激动没好处
你老婆也行
也是醉