How to use UiSelector with Appium in java

Hi ,

I want to use the uiselector & uiscrollable capability with appium in java

Do i need to import the uiautomator.jar or how can i use it?

Please give a brief example

1 Like

Here is a UiSelector example.

Thanks,that was really helpful,
can there be intergration as such we could use all the functions defined in uiautomatorviewer.jar ?

You can use a bunch of the uiautomator methods, everything on UiSelector plus some others. If there are specific methods that you’d like to be supported, I suggest opening an enhancement request on github.

Thanks again for the info… if you can also guide me on how can i use getChild on the same.

For eg if the parent is defined like - MobileElement el4 = (MobileElement) driver.findElementByAndroidUIAutomator(“resourceId(“com.makemytrip:id/login_activity_login_btn”)”);

How i can define the child under them , i tried using UISelector.getclass ("").index;

Can you guide me with an example?

Here is a childSelector example.

Hi,

I have a list view in which there are many relative layouts and in each textview exists ,i want to find a particular text and click on it . how can i go about it ?

WebElement el = driver.elementsByAndroidUIAutomator(“new UiSelector().className(android.widget.ListView).childSelector(new UiSelector().className(android.widget.LinearLayout).clickable(true))”);

is this correct as i am not able to work through this and to get to that text in it( Dynamic text)

You could try textContains.

The syntax that i am trying to use gives an error in itself and the text is dynamic so cannot hard code it.

Can you provide an example in the ApiDemos app with the example or any other way where it is used in Java,

If you don’t know how to use UiSelector via uiautomator then appium doesn’t make it any easier. I’m pretty sure the childSelector would work. You could also find it by instance. There are some docs here. Other than that, I think it’s up to you.

i certainly know how to use it with the correct syntax by creating UiObjects, only thing is with the Appium driver i find it a bit different in terms of writing the code as …my test fails while using the same here ,so asked for an example!

An example with UiAutomator

UiObject PatInfo = new UiObject(new UiSelector().className(“android.widget.RelativeLayout”).index(0).instance(0));
UiObject Enteries = PatInfo.getChild(new UiSelector().className(“android.widget.LinearLayout”).index(1));

  UiObject PatName = Enteries.getChild(new UiSelector().className("android.widget.TextView").index(0).instance(0));
  UiObject Patage = Enteries.getChild(new UiSelector().className("android.widget.TextView").index(1).instance(0));
  UiObject Patsex = Enteries.getChild(new UiSelector().className("android.widget.TextView").index(2).instance(0));
  UiObject PatRoom = Enteries.getChild(new UiSelector().className("android.widget.TextView").index(0).instance(1));
  UiObject PatRoomLoc = Enteries.getChild(new UiSelector().className("android.widget.TextView").index(1).instance(1));
  UiObject PatBed = Enteries.getChild(new UiSelector().className("android.widget.TextView").index(2).instance(1));
  UiObject PatAvatar = PatInfo.getChild(new UiSelector().className("android.widget.ImageView").index(0));

This doesn’t work?

driver.elementsByAndroidUIAutomator("new UiSelector().className(\"android.widget.RelativeLayout\").index(0).instance(0).childSelector(new UiSelector().className(\"android.widget.LinearLayout\").index(1));");

I think, only difference with using AndroidUIAutomator api through Appium is - passing “UiSelector” object as a String parameter. You should use escape characters correctly for double quotes.

One example can be found @ Getting selected text for a combobox in Android

this Worked perfectly thanks !!

How Can i get the child count for say a parent class has - 3 chillds with the same Class at different index and from the parent i would like to take the Count having the childs with the same className at different indexes?

I would start by implementing this in Java and then port it to the appium syntax. Other than implicit UiObject, it’s similar. Also you can send multiple UiSelectors in a single request separated by ;

1 Like

Great , please post here once that is done :slight_smile:

Here the sample code to use UISelector:

driver.findElementsByAndroidUIAutomator(“new UiSelector().resourceId(“com.demo”)”)
driver.findElementsByAndroidUIAutomator(“new UiSelector().text(“OK”)”)

Test Automation

I have following scenario

I am using python client and locating using android uiautomator strategy.

I have tried a lot but still no breakthrough.

I have to click on Image button relative particular textview.

Can you help me with locator for the same?

I have tried UiSelector as well.

Please reply…

I have tried fromParent() and childSelector().

I have tried something like this:

uiautomator=.resourceId(“com.XYZ.pqrs:id/imgBtn_notes”).fromParent(.childSelector(.childSelector(.className(“android.widget.TextView”).text(“ABC DEF”))))

But it happens to select .className(“android.widget.TextView”).text(“ABC DEF”) and click on it.

I want .resourceId(“com.XYZ.pqrs:id/imgBtn_notes”) to be clicked