问题
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