Appium : How to select a checkbox from a native Android application

0
down vote
favorite
I want to select a checkbox from a list of fruit names .

Eg .

1.Apple [:heavy_check_mark:]

2.Mango [:heavy_check_mark:]

3.Banana [:heavy_check_mark:]

This entire one row is in one relative layout , then text view and checkbox .

Relative layout[Parent]

  1. //android.widget.TextView[@text=‘Mango’]
  2. //android.widget.CheckBox[@text=‘liked’]

Now i have written function for selecting any fruit by clicking on checkbox , but my xpath is not returning any element.

public static void likeFruit(String fruitName){
driver.findElement(By.xpath("//android.widget.TextView[@text=’"+fruitName+"’]/following-sibling::android.widget.CheckBox[@text=‘liked’]")).click();
}

I have tried below xpath as well , nothing is working .Am i doing anything wrong?

1.//android.widget.TextView[@text=’"+fruitName+"’]/…/android.widget.CheckBox[@text=‘liked’] 2.//android.widget.TextView[@text=’"+fruitName+"’]/following-sibling::*[1]

Can you give a try of removing these thing?

Why ? This works fine .

If I use only //android.widget.TextView[@text=’"+fruitName+"’] element is found which taps on fruit name , not on the checkbox.

Am trying to click on checkbox not the fruit string .

So, is it working now?

No , it still shows NoSuchELement exception . My xpath is correct ?
1.//android.widget.TextView[@text=’"+fruitName+"’]/…/android.widget.CheckBox[@text=‘liked’] 2.//android.widget.TextView[@text=’"+fruitName+"’]/following-sibling::*[1]

I think it should be better if you can have a UIAutomator screenshot and people can take a look.

Can you change ‘"+fruitName+"’ to “’”+fruitName+"’"

Gray circle is the checkbox

Ui Automator screenshot , blurred some sensitive data.

Can you get content-desc instead of text() for your element identification,

Try using below xpath, its working for me.

//android.widget.TextView[@content-desc=‘abc’]/following-sibling::android.widget.CheckBox