借鉴了: https://github.com/RobotiumTech/robotium/wiki/Downloads 下面的Android studio 的example。总结了如下配置。
1.引入robotium-solo-5.5.4.jar库,可以从工程new module添加jar库。
2.在application下面建立一个androidTest/java目录 新建一个类NotePadTest,类的内容如下,NotesList是你要测试的类,用于传入ActivityInstrumentationTestCase2的构造函数。
package com.example.android.notepad;//
com.example.android.notepad包名要命名好,才不会出错。
import com.robotium.solo.Solo;
import android.test.ActivityInstrumentationTestCase2;
public class NotePadTest extends ActivityInstrumentationTestCase2<NotesList> {
private static final String NOTE_1 = "Note 1";
private static final String NOTE_2 = "Note 2";
private Solo solo;
public NotePadTest() {
super(NotesList.class);
}
@Override
public void setUp() throws Exception {
//setUp() is run before a test case is started.
//This is where the solo object is created.
solo = new Solo(getInstrumentation());
getActivity();
}
@Override
public void tearDown() throws Exception {
//tearDown() is run after a test case has finished.
//finishOpenedActivities() will finish all the activities that have been opened during the test execution.
solo.finishOpenedActivities();
}
public void testAddNote() throws Exception {
//Unlock the lock screen
solo.unlockScreen();
//Click on action menu item add
solo.clickOnView(solo.getView(com.example.android.notepad.R.id.menu_add));
//Assert that NoteEditor activity is opened
solo.assertCurrentActivity("Expected NoteEditor Activity", NoteEditor.class);
//In text field 0, enter Note 1
solo.enterText(0, NOTE_1);
//Click on action menu item Save
solo.clickOnView(solo.getView(com.example.android.notepad.R.id.menu_save));
//Click on action menu item Add
solo.clickOnView(solo.getView(com.example.android.notepad.R.id.menu_add));
//In text field 0, type Note 2
solo.typeText(0, NOTE_2);
//Click on action menu item Save
solo.clickOnView(solo.getView(com.example.android.notepad.R.id.menu_save));
//Takes a screenshot and saves it in "/sdcard/Robotium-Screenshots/".
solo.takeScreenshot();
//Search for Note 1 and Note 2
boolean notesFound = solo.searchText(NOTE_1) && solo.searchText(NOTE_2);
//To clean up after the test case
deleteNotes();
//Assert that Note 1 & Note 2 are found
assertTrue("Note 1 and/or Note 2 are not found", notesFound);
assertTrue(solo.waitForText("honey zhuzhu call me!!!"));
}
}
3. application 模块的build.gradle的配置
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.0.1'
androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.5.4'
}
老的需要配置:
android {
compileSdkVersion 23
buildToolsVersion '23.0.2'
defaultConfig {
minSdkVersion 14
targetSdkVersion 21
}
sourceSets {
main {
dirs.each { dir ->
java.srcDirs "src/${dir}/java"
res.srcDirs "src/${dir}/res"
}
}
androidTest.setRoot('src/androidTest')
androidTest.java.srcDirs = ['src/androidTest/java']
}
}
4. 点击application 右键Run All Test
参考文档将robotium-solo-5.5.4-javadoc.jar解压即可。
参考文档:
https://github.com/robotiumtech/robotium
https://www.youtube.com/watch?v=mTF2k5NlFds
https://github.com/RobotiumTech/robotium/wiki/Downloads
http://wxynxyo.iteye.com/blog/2250039
http://anirudh24seven.github.io/devlog/2015/02/13/robotium-android-studio.html