标签:
import android.os.SystemClock; import android.test.InstrumentationTestCase; import android.util.Log; public class Sampletest extends InstrumentationTestCase { private final static String TAG = "Sampletest"; private MainActivity activity = null; private Button btn = null; private TextView tv = null; //初始化设置,初始化页面对象与控件对象 @Override protected void setUp() { try { super.setUp(); } catch (Exception e) { e.printStackTrace(); } Intent intent = new Intent(); intent.setClassName("com.example.test", MainActivity.class.getName()); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); activity = (MainActivity) getInstrumentation().startActivitySync(intent); tv = (TextView) activity.findViewById(R.id.tv); btn = (Button) activity.findViewById(R.id.btn); } //垃圾清理与资源回收,测试完毕退出页面 @Override protected void tearDown() { activity.finish(); try { super.tearDown(); } catch (Exception e) { e.printStackTrace(); } } //活动功能测试,凡是public且以test打头的函数,均会被JUnit测试到 public void testActivity() throws Exception { Log.d(TAG, "test the Activity"); SystemClock.sleep(1000);//先sleep一会,这时的文字是hello for(int i=0; i<1000; i++) { //点击1000次 getInstrumentation().runOnMainSync(new PerformClick(btn)); } SystemClock.sleep(2000);//再sleep一会,这时看到的文字是Hello Android Log.d(TAG, tv.getText().toString()); assertEquals("Hello World", tv.getText().toString()); //注意,这里做了一个判断,判断当前tv标签控件的文字是不是Hello World,当然不是,所以这句判断是错的!!!! } private class PerformClick implements Runnable { Button btn; public PerformClick(Button button) { btn = button; } public void run() { btn.performClick(); } } }
import com.robotium.solo.Solo; import com.example.android.notepad.NotesList; import android.test.ActivityInstrumentationTestCase2; public class NotePadTest extends ActivityInstrumentationTestCase2<NotesList>{ private Solo solo; public NotePadTest() { super(NotesList.class); } @Override public void setUp() throws Exception { //初始化一个Solo对象 solo = new Solo(getInstrumentation(), getActivity()); } @Override public void tearDown() throws Exception { //测试结束要关闭所有打开的页面 solo.finishOpenedActivities(); } //测试添加记录 public void testAddNote() throws Exception { //解锁屏幕 solo.unlockScreen(); //点击菜单项“Add note” solo.clickOnMenuItem("Add note"); //声明NoteEditor这个页面已经打开 solo.assertCurrentActivity("Expected NoteEditor activity", "NoteEditor"); //往序号为0的编辑框输入“Note 1” solo.enterText(0, "Note 1"); //回到上一页 solo.goBack(); //点击菜单项“Add note” solo.clickOnMenuItem("Add note"); //往序号为0的编辑框输入“Note 2” //注意enterText和typeText的区别在于:typeText是模拟键盘输入, //所以typeText的字符串中如果包含“\n”会转为换行符,而enterText只会原样显示字符串“\n” solo.typeText(0, "Note 2"); //回到上一页 solo.goBack(); //捕捉屏幕截图并保存到默认路径:Environment.getExternalStorageDirectory()+"Robotium-Screenshots/". solo.takeScreenshot(); //检查是否找到“Note 1”和“Note 2” boolean notesFound = solo.searchText("Note 1") && solo.searchText("Note 2"); //如果找到则通过测试 assertTrue("Note 1 and/or Note 2 are not found", notesFound); } public void testEditNote() throws Exception { //点击列表视图的第二项,即进入到该记录的编辑页面 solo.clickInList(2); //隐藏软键盘 solo.hideSoftKeyboard(); //变更屏幕方向为水平方向 solo.setActivityOrientation(Solo.LANDSCAPE); //点击菜单项“Edit title” solo.clickOnMenuItem("Edit title"); //往序号为0的编辑框追加输入“ test” solo.enterText(0, " test"); //回到上一页 solo.goBack(); //设置屏幕方向为垂直方向 solo.setActivityOrientation(Solo.PORTRAIT); //使用通配符查找是否存在指定文本 boolean noteFound = solo.waitForText("(?i).*?note 1 test"); //如果找到则通过测试 assertTrue("Note 1 test is not found", noteFound); } public void testRemoveNote() throws Exception { //使用通配符查找是否存在包含test的文本 solo.clickOnText("(?i).*?test.*"); //点击菜单项“Delete” solo.clickOnMenuItem("Delete"); //查找是否存在文本“Note 1 test” boolean noteFound = solo.searchText("Note 1 test"); //如果没找到则通过测试 assertFalse("Note 1 Test is found", noteFound); //长按文本“Note 2” solo.clickLongOnText("Note 2"); //在弹出的上下文菜单中点击文本“Delete” solo.clickOnText("Delete"); //等待100毫秒后查找是否存在文本“Note 2” noteFound = solo.waitForText("Note 2", 1, 100); //如果没找到则通过测试 assertFalse("Note 2 is found", noteFound); } }
标签:
原文地址:http://blog.csdn.net/aqi00/article/details/51979928