首先,你尝试过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视频
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
Assert.assertEquals(solo.getEditText(..id..).toString(), "Text");
,但它不起作用。我还尝试了https://dev59.com/sn7aa4cB1Zd3GeqPminO#22969743。 - Nagy Vilmos