Appium Different Locator strategies | Find By Element using different locators in Appium
In Mobile (or Web) Automation Testing automating any scenario follows these 2 steps:
1) Find the UI element locators (uniquely).
2) Perform an action on that element.
So What is an Element Locator?
An Element Locator is nothing but an address that identifies a UI Element on a Mobile App (or Website). As there are many UI elements present on a single mobile application screen there can be a chance that same (generic)address can refer to more than one element. This means that we need to find a unique address for the element. As you will see, sometimes this is easy, and other times you have to do some further exploration to uniquely identify your UI element. The way in which you uniquely identify the element is called a locator strategy. Appium makes many different strategies available.
In Appium we have multiple locator strategies to find the
element. Lets discuss the different locator strategies:
- Accessibility ID
- Class Name
- ID(resource-id)
- Name
- Xpath
- Image(newly introduced)
- AndroidUIAutomator
1. Accessibility ID:
Read a unique identifier for a UI element. For XCUITest it is the
element's accessibility-id attribute. For Android it is the
element's content-desc attribute.
Syntax:
driver.findElementsByAccessibilityId(content-desc)
Example:
Lets take ‘DEL’ sign. To click on that button, we need a locator. So
click on 'DEL' button in UIAutomator. In node details you will ‘class’
having value ‘delete’.
driver.findElementsByAccessibilityId("delete")).click();
2. Class name(class) :
For IOS it is the full name of the XCUI element and begins with
XCUIElementType.
For Android it is the full name of the UIAutomator2 class (e.g.:
android.widget.TextView)
Syntax:
driver.findElement(By.className(class value));
Example:
Lets take ‘+’ sign. To click on that button, we need a locator. So
click on + button in UIAutomator. In node details you will ‘class’
having value ‘android.widget.Button’.
Lets take ‘+’ sign. To click on that button, we need a locator. So
click on + button in UIAutomator. In node details you will ‘class’
having value ‘android.widget.Button’.
driver.findElement(By.className("android.widget.Button")).click();
3. ID(resource-id): Native element identifier.
resource-id for android; name for iOS.
Node details of that element will be displayed. In those details get
the value of ‘resource-id’. And pass the value in the syntax.
Syntax:
driver.findElement(By.id(“resource-id Value”));
Example:
Using this ID we can locate an element. Let’s try to find the digit 1
from the calculator app using UI Automator. Just Click on number ‘1’
in UIAutomator. In node details you will ‘resource-id’ having value ‘com.android.calculator2:id/digit_1’.
4. Name(text): Name of element
Syntax:
findElement(By.name(text of the element));
Lets take ‘=’ sign. To click on that button, we need a locator. So
click on ‘=’ button in UIAutomator. In node details you
will ‘text’ having value ‘=’.
driver.findElement(By.name(“=”)).click();
5. XPath: Search the app XML source using xpath
Syntax:
driver.findElement(By.xpath(xpath of element));
driver.findElement(By.xpath(xpath of element));
a. XPath using class name and ‘text’
driver.findElement(By.xpath(“//android.widget.Button[@text=’1′]”))
In above ‘android.widget.Button’ is a classname and text value is
‘1’
b. Xpath using ‘contains’
driver.findElement(By.xpath(“//android.widget.Button[contains(@resource-id,’digit_1′)]”));
In above command we are using contains to locate an element having
‘android.widget.Button’ class name and having ‘resource-id’ =
‘digit_1’
c. Xpath using ‘contains’ and ‘and’ to club locator values:
driver.findElement(By.xpath(“//android.widget.Button[contains(@resource-id,’digit_1′) and@text=’1′]”));
In above command, we are locating an element having class name ‘android.widget.Button’ which contains ID ‘resource-id’ and having ‘text’
6. Image (Recently Introduced):
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.
Example:
WebElement element = driver.findElementByImage(base64Image);
7. AndroidUIAutomator :
AndroidUIAutomator is a very powerful locator to find an element. It uses UIselector to find an element.
AndroidUIAutomator is a very powerful locator to find an element. It uses UIselector to find an element.
Syntax:
driver.findElementByAndroidUIAutomator(“String”);
Example: If you would like to get a tag name of an element
then
WebElement element = driver.findElementByAndroidUIAutomator(“new UiSelector().index(0)”);
assertEquals(“android.widget.FrameLayout”, element.getTagName());
No comments:
Post a Comment
If any suggestions or issue, please provide