Robotium是一款国外的Android自动化测试框架,主要针对Android平台的应用进行黑盒自动化测试,它提供了模拟各种手势操作(点击、长按、滑动等)、查找和断言机制的API,能够对各种控件进行操作。Robotium结合Android官方提供的测试框架达到对应用程序进行自动化的测试。
- Download "robotium-solo.jar"
- Adds library
- Create test class in androidTest package
- Run your test class
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity>{
private Solo solo;
public MainActivityTest() {
super(MainActivity.class);
}
@Override
protected void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
//Unlock the lock screen
solo.unlockScreen();
//Hides the soft keyboard
solo.hideSoftKeyboard();
solo.clearEditText((EditText)solo.getView(R.id.username));
solo.clearEditText((EditText)solo.getView(R.id.password));
}
@Override
protected void tearDown() throws Exception {
solo.finishOpenedActivities();
}
public void testUsernameEmpty() throws Exception {
solo.clickOnView(solo.getView(R.id.regist));
boolean actual = solo.searchText(StringUtils.EMPTY_TOAST_USERNAME);
assertTrue(StringUtils.USERNAME_EMPTY_TEST, actual);
}
}
- Easy to use
- Tests are easy to read
- Doesn't require access to source code. Can test a APK
- Can identify elements easily (with caveat...more on this later)
- Can fall back on default Android framework
- Great support
- Not all views and objects are currently supported e.g. SlidingDrawer
- Slower compared to unit testing
- Single class containing all methods, Selenium 1 style. This is going to get messy
Robotium in Android Studio
Robotium API
User Guide Android Studio
ANDROID自动化测试 robotium