Question

I am writing an Appium test on an Android device. I worked a lot with Selenium WebDriver, I was hoping to have a similar experience. However, XPath is giving me headaches. Here's the component hierarchy:

android.widget.GridView

--android.widget.RelativeLayout
------android.widget.RelativeLayout
------android.widget.RelativeLayout
----------android.widget.TextView     <-   text here is "Test1", resourceId=qa.com.text:id/bookTitle

--android.widget.RelativeLayout
------android.widget.RelativeLayout
------android.widget.RelativeLayout
----------android.widget.TextView     <-   text here is "Test2", resourceId=qa.com.text:id/bookTitle

--android.widget.RelativeLayout
------android.widget.RelativeLayout
------android.widget.RelativeLayout
----------android.widget.TextView     <-   text here is "Test3", resourceId=qa.com.text:id/bookTitle

--android.widget.RelativeLayout
------android.widget.RelativeLayout
------android.widget.RelativeLayout
----------android.widget.TextView     <-   text here is "Test4", resourceId=qa.com.text:id/bookTitle

I wanted to have a method which returns me the 4 elements children of the GridView. Here's my code:

public List<WebElement> getElementsFromGrid() {
   return driver.findElementsByXPath("//android.widget.GridView/android.widget.RelativeLayout");
}

Test:

for (final WebElement s : kno.getBooksInCourse()) {
 final MobileElement m = (MobileElement) s;
System.err.println(m.findElementByAndroidUIAutomator("resourceId(\"qa.com.text:id/bookTitle\")").getText());
}

I was expected to see: Test1, Test2, Test3, Test4, but I am getting: Test1, Test1, Test2, Test4. What am I doing wrong? Can I use another finding method? Using the classname does not help me, because it gives me ALL the RelativeLayout elements, not only the next in hierarchy. I would use a UISelector, but I don't know how com.

Using Appium 1.2 and Appium Java client 1.5

Was it helpful?

Solution 2

It was actually a bug in Appium, in regards to compressed hierarchy. Fixed in Appium 1.3

OTHER TIPS

You can use the findElementById("qa.com.text:id/bookTitle") but I have doubts that the problem is generated from the finder.

Can you use findElementsById("qa.com.text:id/bookTitle") and print the list. If the problem persists there might be a problem regarding refresh. Sometimes xpath just goes nuts becuz the page is not reloaded and the prior page is still in memory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top