本人最近在学习UiAutomator的时候,写一个聊天室的用例,刚好练习一下断言的应用。分享出来,供大家参考。
//班级聊天测试
public void charroomSendAndSwitchSwitch() throws UiObjectNotFoundException {
login();
waitForTextAndClick("聊天");
waitForTextAndClick("班级群聊");
String text = "我是测试!"+getNow();
writeText(text);
waitForTextAndClick("发送");
assertTrue(getUiObjectByText(text).exists());
//验证切换老师按钮
waitForResourceIdAndClick("com.gaotu100.superclass:id/chatroom_header_filter_msg_image");//点击切换老师按钮
assertFalse(getUiObjectByText(text).exists());
waitForResourceIdAndClick("com.gaotu100.superclass:id/chatroom_header_filter_msg_image");//点击切换老师按钮
assertTrue(getUiObjectByText(text).exists());
}
中间有一个切换老师的按钮,点击之后就只能看到老师的发言。不能看到自己的发言了。下面是login()方法。
//登录学生app
public void login() throws UiObjectNotFoundException {
waitForResourceIdAndClick("com.gaotu100.superclass:id/view_shared_tabbar_me");//点击我的
if (!getUiObjectByText("我的班课").exists()) {
pressTimes(KeyEvent.KEYCODE_FORWARD_DEL, 12);//清除已有帐号
getUiObjectByResourceId("com.gaotu100.superclass:id/login_username").setText("1320454218");//输入帐号
getUiObjectByResourceId("com.gaotu100.superclass:id/login_password").setText("qqqqqq");//输入密码
getUiObjectByResourceId("com.gaotu100.superclass:id/login_button").clickAndWaitForNewWindow();//点击登录
}
}