首先,你試過Robotium?這很簡單,適用於本地混合應用程序。我經常使用它。與Maven,Gradle或Ant平滑集成,作爲持續集成的一部分運行測試。
import junit.framework.Assert;
public class EditorTest extends ActivityInstrumentationTestCase2<EditorActivity> {
private Solo solo;
public EditorTest() {
super(EditorActivity.class);
}
public void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
}
public void testPreferenceIsSaved() throws Exception {
solo.sendKey(Solo.MENU);
solo.clickOnText("More");
solo.clickOnText("Preferences");
solo.clickOnText("Edit File Extensions");
Assert.assertTrue(solo.searchText("rtf"));
solo.clickOnText("txt");
solo.clearEditText(2);
solo.enterText(2, "robotium");
solo.clickOnButton("Save");
solo.goBack();
solo.clickOnText("Edit File Extensions");
Assert.assertTrue(solo.searchText("application/robotium"));
}
@Override
public void tearDown() throws Exception {
solo.finishOpenedActivities();
}
}
二,Espresso。另一個容易與Gradle整合。官方Google IO video
onView(withId(R.id.my_view)) // withId(R.id.my_view) is a ViewMatcher
.perform(click()) // click() is a ViewAction
.check(matches(isDisplayed())); // matches(isDisplayed()) is a ViewAssertion
我建議你試試這個庫:https://github.com/mauriciotogneri/green-coffee 你只需要導入它,然後你就可以運行用小黃瓜你的測試。 – 2017-02-04 10:06:55