uiautomator click on a ImageButton with no text or content-desc

徘徊边缘 提交于 2019-12-25 05:34:28

问题


On 4.2.2 emulator, in Now Playing screen from Music, we have the prev, play and next ImageButtons. Unfortunately these 3 buttons have no text or contet-desc values.

I want to click on one of them and I think the answer lies in something like:

  UiObject relLayout = new UiObject(new UiSelector()
  .className("android.widget.LinearLayout").index(1));
  relLayout.click();

But there are several LinearLayout in this screen and I don't know how to select a specific LinearLayout. The code snipped above will press on shuffle as it is also a LinearLayout that contains 3 image buttons(library, shuffle and repeat).

The only thing we know about our LinearLayout is that it is the child, with index 5, of another LinearLayout which is the child, with index 0, of a FrameLayout which is the child, with index 0, of a LinearLayout which is the child, with index 0, of a FrameLayout.

The question is how can we tell UiSelector to look for a certain LinearLayout?

Thanks,

Alex


回答1:


In situations like this, I click by bounds. This is can cause problems cause when you change device sizes, the bounds will change. So I get the screen height so you know which device your using, then click by bounds. You can find your bounds via uiautomatorviewer

 int height = UiDevice.getInstance().getDisplayHeight();

if(height == THE_SIZE_EXPECTED){
  UiDevice.getInstance().click(int_x, int_y);
 } else if (height == ANOTHER_SCREEN_HEIGHT_OPTION){ 
   UiDevice.getInstance().click(int_x, int_y);
 } 

Hope this helps



来源:https://stackoverflow.com/questions/20519905/uiautomator-click-on-a-imagebutton-with-no-text-or-content-desc

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