What are Mobile Element locators in Appium?

In any form of automation testing, we need to identify the objects and they are identified by locators. We have discussed various locators in WebDriver as well; we used them to identify web elements in web GUI testing. Now we are discussing locators to identify mobile element; so we need mobile elements of hybrid, native and web app to perform automation action on them.

Recommended: How to inspect mobile elements with UiAutomatorViewer tool?

What is Mobile Element?

Everything we see on any mobile app is a Mobile element. It could be anything like text, search box, text area, date picker, image, time, video, animation, app icon, etc. Every mobile element has some hierarchy, if it is a mobile web app then it will have HTML DOM hierarchy. Similarly, the hybrid and native app will have a different hierarchy. We define a strategy to locate test those elements and for that, we should be aware of various locators in Appium.

Mobile Element Locators

In Appium based mobile testing, there is a WebDriver like an interface referred to as AppiumDriver which is used to create an instance of the class of the respective devices. Mobile elements locator is the target element of the mobile application where we send various WebDriver or AppiumDriver commands, or simply Selenium commands.

There are various types of locators in Appium, it depends on the mind-set of the automation developer to choose the right one so that mobile elements could easily be located. We will discuss those locators in the next subsection of this post.

What are different mobile locators in Appium to find elements?

There are mainly five types of locators in use for identifying mobile elements in Appium test automation. Those are listed as below:

  • Id
  • Name
  • Accessibility id
  • Class Name
  • XPath

You would be familiar with most of the locators as you used them in your Selenium tests. Let’s discuss each of the locators above and the technique to fetch them, but before that let’s understand findElement() and findElements() method.

findElement() & findElements() in Appium

In Web Element, findElement() method used to identify single element, whereas, findElements() method identifies a List of web elements. Similar concept work behind these two methods here as well, but, instead of Web Element, we use Mobile Element.

Let’s locate different mobile elements of the calculator application. We discussed inspecting elements through UiAutomatorViewer tool in the previous article, so here as well we are using the same tool to inspect mobile objects.

Locating element by Id

We have to click on the object of numeric 7 of the calculator. We are going to click through Id so attribute resource-id used for locating element by id.

findElementById Mobile Element

Command will be:

driver.findElementById("com.android.bbkcalculator:id/digit7").click();

Locating element by Accessibility Id

Again content-desc attribute referred to as the Accessibility id of any mobile element. See image below:

findElementByAccessibilityId Mobile Element

Command will be:

driver.findElementByAccessibilityId("7").click();

Locating element by Class name

In some application, Class might contain a list of the mobile element, hence in such case, we need to call List interface. Well, in our case of calculator application it’s only a single element. See image below.

findElementByClassName Mobile Element

Command will be:

driver.findElementByClassName("android.widget.ImageButton").click();

In case, if it contains List of the mobile element then the command would be:

List<MobileElement> list = driver.findElementsByClassName("android.widget.ImageButton");

Locating element by Name

Like WebDriver, we can use name locator here as well. Value of name locator referred by text attribute.

findElementByName Mobile Element

Command will be:

driver.findElementByName("ViewAll").click();

Locating element by XPath

XPath is the mother of all locators. – Avinash 😉

If you are not able to act with any of the locators mentioned above then the final and permanent solution is given by XPath. I have described XPath in a well-detailed manner. You should go through below two exclusive posts on dynamic XPath. We will separately discuss XPath for Appium in a blog post.

findElementByXPath Mobile Element

Command will be:

driver.findElementByXPath("//*[@resource-id='com.flipkart.android:id/tv_card_view_holder']").click();

This was all about identifying the mobile element with various locators. All these are the basic appium tutorials. Further, we will work on Appium framework and other mobile testing strategies.

Join Inviul fb group

Leave a Reply