How to use Locators in Appium for Android

 Here we will get to know how to capture elements to perform desired action on Mobile App/ Web 

Elements Locator:

 An Element Locator is basically the address by which the framework (Appium, in this article) identified a UI element on the mobile app. Each app screen carries multiple UI elements, which means that each of them needs to be identified with a unique address or parameter. Usually, finding the Element Locator is easy, but sometimes the tester may have to explore further to detect a unique address.


The below snapshot describes how to find the Textview element for any android application (in Java).

As we have multiple element locators as listed below:

  • Accessibility ID
  • Class name
  • ID
  • Name
  • XPath
  • Image (Recently Introduced)
  • Android Ui Automator (UiAutomator2 only)

Appium Inspector for capturing locators: find the screenshot for reference


Accessibility ID

This is the best preferred locator strategy in Appium. Always use this one if you can.

It is a Cross-platform locator strategy as this works in a similar way on iOS and Android which makes your code more reusable.

iOS: If the accessibility id property(attribute) value is set at development time (by the app developers) then you can inspect it using the Appium Inspector(Android & iOS) or UiAutomatorViewer(Android). When Accessibility Id property value is not defined by developer, it is by default equals to the Name of that UI Element.

AndroidAccessibility Id property is equals to content-desc property(attribute) on Android.

WebElement chromeButtonElement = driver.findElementByAccessibilityId(“buttonStartWebviewCD”);
MobileElement mobileElement = (MobileElement)chromeButtonElement;

Class Name

Finding an element using Class Name is generic and it does not guarantee to find the unique element because many elements have the same class name driver.findElementsByClassName(“android.widget.ImageButton);

   ID

In Mobile Application Automation id is are in form of Native context, it is not similar to Selenium WebDriver’s CSS id.
id are also cross-platform locator strategy similar like accessibility id.

iOS: It will find elements by name and label attribute but before that Appium will try to search for a accessibility id that will match with the given id string.

driver.findElementById("IntegerA");

Name

iOS & Android: It’s the Name of the element on both platforms. This isn’t used as often as accessibility id and id strategies are mostly used.

In below snippet you can find the Name attribute using:
MobileElement element = driver.findElementByName("IntegerA");

XPaths in Appium

This locator strategy analyzes the XML structure of the app and locates the element with respect to the other elements.

The XPath is originally designed to allow for the navigation of XML data with the purpose of locating unique UI elements.

XPath selectors are not cross-platform

  • By using Appium Inspector for inspection of the Application XML structure then Appium will directly give you the XPath without any additional effort.

MobileElement textButton = driver.findElementByXPath("//XCUIElementTypeButton[@name="ComputeSumButton"]");

Image


Appium supports Image Comparison as a locator strategy which is using the OpenCV library in the backend.

The strings which are being used by this locator strategy are Base64-encoded image files.

So you need to convert image files into Base64-encoded image files first and you need to pass that String into the locator. String base64Image = //Code which will to convert Image file to Base-64 String

WebElement element = driver.findElementByImage(base64Image);


Android UiAutomator (UiAutomator2 only)

This is an Android Platform specific locator strategy.

This is rarely used to find the element locators as it requires to have prior knowledge of the UiSelector API (and of course it’s Android only).

It’s performance is slightly better than XPath.
AndroidUIAutomator("new UiSelector().text(\"7\").className(\"android.widget.TextView\")"));

Inspect using Android UI Automator Viewer

It is available under android sdk to capture locators find the image below for reference:



Xpath combinations:

Xpath with Class Name and Resource Id

Syntax: by.xpath("//classname[@resource-id='resource id here']");

Xpath with Class Name and Text

by.xpath("//classname[@text='text here']"); Example: By.xpath("//android.widget.TextView[@text='Hyderabad']");

Xpath with Class Name and Content Desk

Example: By.xpath("//android.widget.ImageButton[@content-desc='Open navigation drawer']");

Xpath using index with class and resource-Id:

Example: By.xpath("(//android.widget.ImageView[@resource-id='com.Clairvoyant.FernsAndPetals:id/ivProductImage'])[1]");


1 comment:

  1. For MI phones to automate/skip install via usb toast message try :

    Settings -> Additional Settings -> Developer options -> Turn off Miui Optimizations -> Restart

    ReplyDelete

Scroll iOS application using Appium

When creating test scripts for iOS native app we need to perform scroll to perform any other action on particular element  Here we will get ...