Whenever we perform automation on a mobile application or on a desktop/ mobile web browser, we basically perform 2 activities:
1) We locate the elements using various Locator techniques.
2) We perform some Action on the located element(s) like click, entering text, extracting text, drag and drop etc.

Since, Appium speaks the WebDriver protocol, it is able to support a subset of Selenium WebDriver Locator Strategies along with some additional Mobile JSON Wire Protocol Locator Strategies.

WebDriver Locator Strategies:

Class name (For Android this is the native UI class (not HTML class) e.g. android.widget.Button, for iOS this is the full name of the XCUI element and begins with XCUIElementType).

ID (For Android this is the native resource-id attribute and for iOS this is the name attribute)

XPath (Search of the app XML source document, representing a native UI hierarchy, using the given path)

JSON Wire Protocol Locator Strategies:

Accessibility ID
This is an unique element identifier – a string used for making a UI object accessible. For Android this is the content-desc attribute, for iOS this is the accessibility-id attribute.

Android UiAutomator (for UiAutomator2 only)
Sends the java code as a String to the Appium server using the Android UiAutomator selector API, usually the UiSelector class, which then executes in the application environment and sends back the element(s)

Additional Experimental Locator Strategies

Image
Locate an element by matching it with a Base64 encoded image file that is send to the Appium Server. If Appium can find a screen region matching the template, it will wrap up information about this region as a standard WebElement and send it back to the Appium client

Custom
String send to an element finding plugin registered via the customFindModules capability.

For more information on the Additional Experimental Locator Strategies, you can visit the Appium official webpage.

Let’s now see one by one how we can use the locator strategies to find the elements. To view the elements, you can use “uiautomatorviewer” batch file present inside the Android SDK tools bin folder (e.g. C:\Users\Sumon\AppData\Local\Android\Sdk\tools\bin). First ensure that your virtual device (emulator) is up and running. Then doubleclick on uiautomatorviewer file. Once the uiautomator opens up, click on the second icon from top to take screenshot from the device. You will be able to see all the elements. You can then click on each element to view its attributes and values.

1) CLASS NAME

To locate an element in Android using the class name, first we need to identify the classname which is the full name of the UiAutomator2 class e.g. android.view.View, android.widget.TextView, android.widget.Button etc. Also, remember that classname value will be the tagname while writing xpath expression (which we will see below). To locate an element using the classname we can use either of the following lines of code:

AndroidElement nextButton=driver.findElementByClassName("android.widget.Button");
AndroidElement nextButton=driver.findElement(By.className(“android.widget.Button”));

In most of the cases, multiple elements will be having the same classname and if we use findElement it will return the first matched element. To get all the elements and iterate over them, we need to use findElementsByClassName and findElements methods.

2) ID

“ID” is the unique native element identifier for mobile applications. For android, this is resource-id attribute and for iOS, this is the name attribute. To locate an element with resource-id we can use either of the below lines of code:

AndroidElement wifiCheckbox=androidDriver.findElementById("android:id/checkbox");
AndroidElement wifiCheckbox=androidDriver.findElement(By.id("android:id/checkbox"));

3) XPATH

XPath is an abstract representation of a path to an element. When we use xpath expression to find an element, it searches the application XML source containing the hierarchical relationship between the elements. Thus, it might take some time to find the elements. Hence, the above locating strategies should be preferred first before deciding to use Xpath. To use Xpath, we need to use class name as tagname, followed by the attribute(s) and attribute value(s).

AndroidElement advancedPreferencesText=androidDriver.findElementByXPath("//android.widget.TextView[@text=‘6. Advanced preferences']");
AndroidElement advancedPreferencesText=androidDriver.findElement(By.xpath("//android.widget.TextView[@text=’6. Advanced preferences']"));

Here, android.widget.TextView is the class name (tag name)
text is the text attribute

6. Advanced preferences is the text attribute value. If we want to consider searching the text attribute in all the elements we need to use * symbol like below:

AndroidElement advancedPreferencesText=androidDriver.findElementByXPath("//*[@text='6. Advanced preferences']");

4) ACCESSIBILITY ID

For Android, the content-desc is a unique element locator attribute that can be used to locate an element.

AndroidElement accessibilityServiceText=androidDriver.findElementByAccessibilityId("Accessibility Service");

5) UiAUTOMATOR

Using the UiAutomator API, we can send Java Code as a String to the Appium Server. The server will then execute the code in the Application environment using the UiAutomator2 driver for Android platform. The response will be the matched element or elements.

AndroidElement graphicsText=driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(text(\"Graphics\"))");

Credits:
http://appium.io/