UIAutomator - How to click the menu item

穿精又带淫゛_ 提交于 2020-01-15 06:11:42

问题


I have automated go to setting in android phone using UI Automator and i clicked the menu using "getUiDevice().pressMenu();" and its opened menu with 3 sub menu item and i want to click the second menu by using name or index or id , please help how to click the sub menu in Android UIAutomator ?


回答1:


This overflow menu icon ImageView with the three dots has no id ...

but one can get it by it's description:

UiObject2 menuButton = this.mDevice.findObject(By.desc("More options"));

/* open the menu */
if(menuButton != null && menuButton.isClickable()) {
    menuButton.click();
    menuButton.recycle();
} else {
    Assert.fail();
}

Then one can click menu items by their index:

ArrayList<UiObject2> menu = (ArrayList<UiObject2>) this.mDevice.findObject(By.clazz("android.widget.ListView")).getChildren();

/* click the menu item at index 0 */
UiObject2 menu_action = menu.get(0);
if (menu_action != null && menu_action.isClickable()) {
    menu_action.click();
    menu_action.recycle();
} else {
    Assert.fail();
}



回答2:


Use clazz method is working

 UiObject2 item = mDevice.findObject(By.clazz("android.widget.ListView"));



回答3:


You can use 'uiautomatorviewer' tool and view the text/description/resource id/index of your menu items displayed there.

To use uiautomatorviewer tool :

[1] Connect your device.
[2] Open command prompt and type 'uiautomatorviewer'.
[3] Open the particular screen of whose elements you want to view (in this case - your  menu items).
[4] Press the green 'Device Screenshot' button  next to file open in top left.

Hover on the screenshot to view the text/description/resource id/index of the elements of the screen and you can use whatever is being displayed there.



来源:https://stackoverflow.com/questions/21721400/uiautomator-how-to-click-the-menu-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!