本人在学习实践过程中,需要对app个人资料里面姓名和性别等进行验证修改是否成功,本来挺简单的一个事情结果弄了半天,原因就是app在这些地方设计了一些限制,字符长长度,本身UiAutomator的cleartexrfiled这个方法不太好用,导致了这些问题,还有一个,个人资料如果没有改动,保存按钮不可用,我想是随机设计性别然后再去验证。这个地方也阻碍了我一打会儿。之所以选择这两个,因为一个是选项一个是文字。如果不足,还请大神指点。
public void alterName(String text) throws UiObjectNotFoundException{//修改名字
getUiObjectByText("个人资料 > ").clickAndWaitForNewWindow();
String name = getUiObjextByResourceId("com.dianzhi.student:id/person_username_tv").getText();
getUiObjectByText("学生:").clickAndWaitForNewWindow();
pressTimes(KeyEvent.KEYCODE_DEL, 6);//删除6次,我们设定的最多6个字符
writeText(text);
getUiObjectByText("完成").clickAndWaitForNewWindow();
if (name.equals(text)) {
UiDevice.getInstance().pressBack();
}else{
getUiObjectByText("保存").clickAndWaitForNewWindow();
}
getUiObjectByText("个人资料 > ").clickAndWaitForNewWindow();
String newname = getUiObjextByResourceId("com.dianzhi.student:id/person_username_tv").getText();
assertEquals("修改名字失败!", text, newname);
outputNotable("修改名字成功!");
UiDevice.getInstance().pressBack();
}
以上是设置姓名的
public void setSexRandom() throws UiObjectNotFoundException {//随机设置性别
getUiObjectByText("个人资料 > ").clickAndWaitForNewWindow();
String text = getUiObjextByResourceId("com.dianzhi.student:id/person_sex_tv").getText();
getUiObjectByText("性别:").click();
int a = new Random().nextInt(160);
if (a<40 && a >= 0) {
getUiObjectByText("保密").click();
if (text == "保密"){
UiDevice.getInstance().pressBack();
}else{
getUiObjectByText("保存").clickAndWaitForNewWindow();
}
getUiObjectByText("个人资料 > ").clickAndWaitForNewWindow();
assertEquals("修改性别失败!", "保密", getUiObjextByResourceId("com.dianzhi.student:id/person_sex_tv").getText());
UiDevice.getInstance().pressBack();
}else if (a<80 && a >= 40) {
getUiObjectByText("男").click();
if (text == "男"){
UiDevice.getInstance().pressBack();
}else{
getUiObjectByText("保存").clickAndWaitForNewWindow();
}
getUiObjectByText("个人资料 > ").clickAndWaitForNewWindow();
assertEquals("修改性别失败!", "男", getUiObjextByResourceId("com.dianzhi.student:id/person_sex_tv").getText());
UiDevice.getInstance().pressBack();
}else if (a<120 && a >= 80) {
getUiObjectByText("女").click();
if (text == "女"){
UiDevice.getInstance().pressBack();
}else{
getUiObjectByText("保存").clickAndWaitForNewWindow();
}
getUiObjectByText("个人资料 > ").clickAndWaitForNewWindow();
assertEquals("修改性别失败!", "女", getUiObjextByResourceId("com.dianzhi.student:id/person_sex_tv").getText());
UiDevice.getInstance().pressBack();
} else {
getUiObjectByText("取消").click();
UiDevice.getInstance().pressBack();
getUiObjectByText("个人资料 > ").clickAndWaitForNewWindow();
assertEquals("修改性别失败!", text, getUiObjextByResourceId("com.dianzhi.student:id/person_sex_tv").getText());
UiDevice.getInstance().pressBack();
}
outputNotable("修改性别成功!");
}
以上是设置性别的。有点乱,看一个就行。