0
votes

I am struggling with a navigation drawer on a native Android app.

So there are 11 elements in the drawer, 9 are visible, for the rest you have to scroll down.

Appium Inspector view

those elements contain 2 child elements, first - imageView, second - textView element.

Appium Inspector view

what I want to do, is create a method to iterate through the visible drawer elements in search for the element with a specific name and then tap on it, if the elements is invisible, I will scroll the drawer down/up and then repeat the iteration.

So I need a for loop to iterate through textView elements, locate the one I need using a text attribute and then click on the parent element of this textView.

However I'm struggling with properly locating the elements using xpath and Appium can not seem to find them.

Can anyone help me to understand how to specify xpath dynamically so I can iterate through the elements?

Thank you.

1

1 Answers

0
votes

You could try something like I have below. This uses the PageObject model, along with Selenium and Appium. It takes advantage of the uiAutomator, rather than Xpath.

public class PageObjectXYZ {

    private AndroidDriver driver;

    @AndroidFindBy(uiAutomator = "new UiSelector().className("android.widget.TextView")")
    private List<MobileElements> someTextViews;

    public PageObjectXYZ(AndroidDriver driver) {
        this.driver = driver;
        PageFactory.initElements(new AppiumFieldDecorator(driver), this);
    }

    public void clickTheOneIWant(String option){
        for(MobileElement me: someTextViews){
            if(me.getAttribute("text").equals(option)){
                this.driver.tap(1, me, 1000);
                break;
            }
        }
    }
}

If you just want the quick and dirty using xpath, then something like(some assembly may be required):

String x = "//RecyclerView/RelativeLayout/RelativeLayout/TextView";
String y = "//RecyclerView/RelativeLayout/RelativeLayout";

Android driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), desiredCapabilities);

Arraylist<MobileElements> arrayTextViews = driver.findElementsByXpath(x);
Arraylist<MobileElements> arrayParentElements = driver.findElementsByXpath(y);

for(int i = 0; i < arrayTextViews.size(); i++){
    if(arrayTextViews.get(i).getText().equals("text I want")){
        arrayParentElements.get(i).click();
    }
}