Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the parent of a view using uiautomator?

I'm trying to identify the parent view of an ui element so I can navigate through the UI freely.

For example, in Settings app, I can find the view with the text "Bluetooth":

UiObject btView = new UiObject(new UiSelector().text("Bluetooth"));

Now, the part where I get stuck is this one: I want to navigate two levels up and start a new search for the on/off button that enables and disables bluetooth.

Note: I can get the button if I use the code below.

UiObject btButtonView = new UiObject(new UiSelector().className("android.widget.Switch").instance(1));

This searches for switch buttons and returns the second encounter. I want the search to be more precise and look for the button in the linear layout that contains the "Bluetooth" text.

UPDATE: This is the layout of the Settings app (the Bluetooth part that I need):

LinearLayout
    LinearLayout
        ImageView
    RelativeLayout
        TextView (with text = "Bluetooth")
    Switch ()
like image 841
Gabriel Porumb Avatar asked Jul 16 '13 15:07

Gabriel Porumb


People also ask

How do you inspect element with UIAutomator?

Q #12) How do you inspect an element in UIAutomator? Answer: Once you are done with the setup, open a command prompt, and enter the command UIAutomatorViewer. A window will be displayed on your PC. Connect the mobile to a PC and click on the Device screenshot (uiautomator dump) second icon at the top.

How do you use the UIAutomator in Appium?

If you go to android SDK>tools>UIAutomator. Make sure device is connected, tap on green button on top left side. This will take the screenshot of your current screen on the device. This way you can point mouse on the screen to detect elements.

What is UIAutomator viewer?

The uiautomatorviewer is a standard UI tool shipped as part of Android Studio for analyzing those UI components of your Android application. Using it you can inspect the UI, find hierarchies the same way you would use Appium Inspector and view different properties/attributes of those UI elements.


2 Answers

You need to find the UiObject two levels up first using the text. This can be done using the getChildByText() methods in UiCollection or UiScrollable. Then you can easily find the switch. For 'Settings' this code works on my device:

UiScrollable settingsList = new UiScrollable(new UiSelector().scrollable(true));
UiObject btItem = settingsList.getChildByText(new UiSelector().className(LinearLayout.class.getName()),"Bluetooth", true);

UiObject btSwitch = btItem.getChild(new UiSelector().className(android.widget.Switch.class.getName()));
btSwitch.click();
like image 109
Anders Avatar answered Oct 26 '22 03:10

Anders


Below code works for me.

//Getting the scrollable view

UiScrollable settingsList = new UiScrollable(new UiSelector().scrollable(true));

for (int i=0; i<=settingsList.getChildCount(new UiSelector ().className(LinearLayout.class.getName())); i++) {
//Looping through each linear layout view
UiObject linearLayout = settingsList.getChild(new UiSelector().className(LinearLayout.class.getName()).instance(i));

//Checking if linear layout have the text. If yes, get the switch, click and break out of the loop.
if (linearLayout.getChild(new UiSelector ().text("Bluetooth")).exists()) {
    UiObject btSwitch = linearLayout.getChild(new UiSelector().className(android.widget.Switch.class.getName()));
    btSwitch.click ();
    break;
    }
}
like image 33
karthick1616 Avatar answered Oct 26 '22 02:10

karthick1616