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.
Refer: Appium doc for elements
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.
Android: Accessibility 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"]");
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);
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\")"));
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:
For MI phones to automate/skip install via usb toast message try :
ReplyDeleteSettings -> Additional Settings -> Developer options -> Turn off Miui Optimizations -> Restart