写case的时候,会存在一个问题,就是点击按钮跳转后,不确定跳转到的页面是哪个?这时候就需要一个这样的返回布尔值的方法,来判断某个元素是否存在,从而判断跳转到了哪个页面。
//判断text否存在
public static boolean judgmentText(String name) {
mDevice.wait(Until.findObject(By.text(name)), waitTime);
UiObject2 x = mDevice.findObject(By.text(name));
if (x != null) {
Log.e(TAG, "text“ " + name + " ”存在 ");
return true;
} else {
Log.e(TAG, "text“ " + name + " ”不存在 ");
return false;
}
}
//判断id否存在
public static boolean judgmentId(String name) {
mDevice.wait(Until.findObject(By.res(name)), waitTime);
UiObject2 x = mDevice.findObject(By.res(name));
if (x != null) {
Log.e(TAG, "text“ " + name + " ”存在 ");
return true;
} else {
Log.e(TAG, "text“ " + name + " ”不存在 ");
return false;
}
}
有了这两个方法,写case的时候就可以这样写了:
if (judgmentText("登录")) {
Log.e ("跳转到登录页面啦!")
} else if (judgmentText"注册") {
Log.e ("跳转到注册页面啦!")
}