1
votes

Trying to Automatize Testing of an Android app. However, the elements don't have any IDs attached to them, only xpaths. These xpaths are dynamic and can change depending on the screen size and the elements location on the screen.

I have tried to use contains() function, but in some cases there is no common nominator. This also doesn't seem to be the most reliable way.

Example:

/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.view.ViewGroup/android.support.v4.widget.DrawerLayout/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup[2]/android.view.ViewGroup/android.widget.ScrollView/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup[2]/android.view.ViewGroup/android.view.ViewGroup[4]

changes to

/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.view.ViewGroup/android.support.v4.widget.DrawerLayout/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup[2]/android.view.ViewGroup/android.widget.ScrollView/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup[2]

when scrolling down on the screen.

What would be the best and most reliable solution? The same test script should run on multiple devices with varying screen sizes.

Is it even recommended to use xpaths in Appium or should I go to the developers and ask for unique IDs for each element?

1
Using Xpath is not recommended in Appium, Better you go for Unique ID, if its available!! You can try Appium Desktop, before approaching to developer to get the ID. - pankaj mishra

1 Answers

0
votes

using xpath is not recommended. You must ask your developer to put unique id for each element that you want to interact with.

The recommended way is to use

  • Find element by ID
  • Find element by Accessibility ID

You can ask developer to put unique cont-desc in the element if you want to use findElementByAccessibilityId().

MobileElemet element = driver.findElementByAccessibilityId("your cont-desc id")

or you can ask developer to put unique id in the the element if you want to use findElementById().

MobileElemet element = driver.findElementById("element's id")

you can verify if the app element contains id or cont-desc by using Appium Desktop Inspector or uiautomatorviewer