本人在学习UiAutomator的时候,遇到添加收获地址的测试用例,这里的地址的地区是一级一级选择的。所以写了一个随机选择的方法。分享出来,供大家参考。
public void addAdress() throws UiObjectNotFoundException {
login();
waitForResourceIdAndClick("com.gaotu100.superclass:id/view_shared_tabbar_me");//点击我的
waitForResourceIdAndClick("com.gaotu100.superclass:id/mylexuefragment_userinfo_container");//点击进入个人中心
waitForTextAndClick("收货地址");
sleep(1000);
waitForTextAndClick("添加");
writeTextByResourceId("com.gaotu100.superclass:id/order_address_name", "收货人01");//输入收货人
writeTextByResourceId("com.gaotu100.superclass:id/order_address_mobile", "13120454219");//输入手机号
waitForResourceIdAndClick("com.gaotu100.superclass:id/order_address_city_right_arrow");//点击选择地区
randomClickOpiton();//随机选择省
randomClickOpiton();//随机选择市
randomClickOpiton();//随机选择县
writeTextByResourceId("com.gaotu100.superclass:id/order_address_detail", "详细地址01");
waitForTextAndClick("保存");
String name = getTextByResourceId("com.gaotu100.superclass:id/order_address_name");//获取收货人
String mobile = getTextByResourceId("com.gaotu100.superclass:id/order_address_mobile");//获取手机号
String adress = getTextByResourceId("com.gaotu100.superclass:id/order_address");//获取详细地址
assertEquals("收货地址收货人姓名错误!", "收货人01", name);
assertEquals("收货地址收货人手机号错误!", "13120454219", mobile);
assertTrue(adress.contains("详细地址01"));
waitForResourceIdAndClick("com.gaotu100.superclass:id/order_address_right_arrow");//点击修改收货地址
waitForTextAndClick("删除");
waitForResourceIdAndClick("com.gaotu100.superclass:id/customer_dialog_ok");//点击确定
}其中randomClickOpiton()方法如下:
public void randomClickOpiton() throws UiObjectNotFoundException {
int num = getUiObjectByClassName("android.widget.ListView").getChildCount();
int i = new Random().nextInt(num);
getUiObjectByResourceIdIntance("com.gaotu100.superclass:id/simpleitemview_left_text", i).clickAndWaitForNewWindow();
}