配置Gradle
compile 'com.android.support:appcompat-v7:23.0.1'
androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.6.0'
androidTestCompile 'com.android.support.test:rules:0.4.1'
androidTestCompile 'junit:junit:4.12'
Simple case
这是一个简单的横竖屏切换case, 运行test1就能看到app自动切换横竖屏的效果了
@RunWith(AndroidJUnit4::class)
class WelcomeTest {
private lateinit var solo: Solo
@get:Rule
var activityTestRule = ActivityTestRule(WelcomeActivity::class.java)
@Before
fun initSolo() {
val config = Solo.Config()
config.commandLogging = true
config.commandLoggingTag = "BlackBoxTest"
solo = Solo(InstrumentationRegistry.getInstrumentation(), config, activityTestRule.activity)
}
@After
fun closeSolo() {
if (::solo.isInitialized) {
solo.sleep(2000)
}
}
@Test
fun test1() {
solo.unlockScreen()
solo.sleep(1000)
solo.setActivityOrientation(Solo.LANDSCAPE)
solo.sleep(1000)
solo.setActivityOrientation(Solo.PORTRAIT)
solo.sleep(1000)
}
}
Solo类
- 获取控件
- getView(int id)
- getView(int id, int index) //index表示控件是界面上第几个相同控件
- getView(String id) //xml中定义的id属性值
- 验证TextView文字
TextView textView = (TextView)solo.getView("hello");
assertEquals("hello world", textView.getText().toString());
- 输入文本
//先获取控件,
enterText(EditText editText, String text)
//index表示页面上的第几个输入框
enterText(int index, String text)
- 等待对话框关闭和打开
waitForDialogToClose()
waitForDialogToClose(long timeout)
waitForDialogToOpen()
waitForDialogToOpen(long timeout)
long timeout: 设置超时时间,单位为毫秒
- 验证Activity的加载
// 等待指定Activity的出现
waitForActivity(String)
waitForActivity(String, int)
waitForActivity(Class<? extends Activity)
waitForActivity(Class<? extends Activity, int)
String: Activity名称
int: 超时时间,默认为20000,单位为毫秒
- 其他延时操作
//休眠指定时间
sleep(int time)
//从当前界面搜索指定的文本
searchText(String text)
//等待指定Log的出现
waitForLogMessage(String logMessage)
//等待控件的出现
waitForView(int id)
//等待文本的出现
waitForText(String text)
//等待某种加载条件的达成
boolean waitForCondition (Condition condition, int timeout)
- 滚动滑动操作
//滑动到顶部
scrollTop()
//向上向下滚动屏幕
scrollUp()/scorllDown()
//滚动至ListView第line行
scrollListToLine(AbsListView absListView, int line)
//从其实x,y左边滑动至终点x,y坐标
drag(float fromX, float toX, float fromY, from toY)
- 其他操作
//截图,name为图片的参数,默认路径是/sdcard/Robotium-Screenshots/
takeScreenshot()
takeScreenshot(String name)
//截取某段时间内一个序列
takeScreenshotSequence(String name)
//关闭当前已打开的所有Activity
finishOpenedActivities()
//点击返回键
goBack()
//不断点击返回键直至返回到指定Activity
goBackToActivity(String name)
// 收起键盘
hideSoftKeyboard()
//设置Activity转屏方向
setActivityOrientation(int orientation)