As shown in the previous article, we can identify elements of Native & Hybrid apps using UIAutomatorviewer. There are few locators used to locate elements for native and hybird apps. Okay let’s look into it. Before that launch your emulator or connect real device. If you don’t know how, check my article. We’ll locate elements of Calculator app. Now Launch UIAutomatorviewer and capture the screenshot of calculator app.
Now, let’s see different locators supported by the Appium driver.
- Finding elements by ID
- Finding elements by name
- Finding elements by className
- Finding elements by AccessibilityId
- Finding elements by AndroidUIAutomator
- Finding elements by Xpath
All the syntax was same as in Selenium. No need to worry. We will see each and every locator here.
1. Finding elements by ID (resource-id)
Using this ID we can locate an element. Let’s try to find the digit 5 from the calculator app using UI Automator. Just Click on number ‘5’ in UIAutomator.
Node details of that element will be displayed. In those details get the value of ‘resource-id’. And pass the value in the syntax.
Now coming to syntax: driver.findElement(By.id(“resource-id Value”));
As of now for digit ‘5’, driver.findElement(By.id(“com.android.calculator2:id/digit5”)).click();
2. Finding elements by name (text)
For this the syntax would be findElement(By.name(text of 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 ‘+’.
The syntax would be: driver.findElement(By.name(“+”)).click();
3. Finding elements by className (class)
We can locate an element using the className as well. This is how the syntax looks like driver.findElement(By.className(class value));
In the below image, you can see that class name was ‘android.widget.Button’. So the syntax would be:
Do you think it works? No! Because all the buttons has the same class name. Now what we suppose to do. In order to click on ‘+’ button using class name, we need to store all the elements having that className and store it in a list. The syntax would be like:
Here, the ‘+’ button has an index value 16, so we have to pass an index value of 16 to take perform action(Just count the buttons in rows)
4. Finding elements by AccessibilityId
Accessibility ID usually have text associated with element. As per Appium Docs, accessibility id: a string corresponding to a recursive element search using the Id/Name that the native Accessibility options utilize. This is how the syntax for AccessibilityId looks:
As of now, let’s check with ‘+’ button again. Click on the + sign from UIAutomator. Now under node Details, you will get the ‘content-desc’ as plus. Take a look at below image
Now the syntax would be like: driver. findElementByAccessibilityId(“plus”);
5. Finding elements by AndroidUIAutomator & UISelector
AndroidUIAutomator is a very powerful locator to find an element. It uses UIselector to find an element. Syntax looks like: driver.findElementByAndroidUIAutomator(“String”);
If you would like to get a tag name of an element then
WebElement element = driver.findElementByAndroidUIAutomator(“new UiSelector().index(0)”);
Here I have passed UISelector as an argument. UiSelector has many more methods in-built. You can verify whether element is scrollable, clickable etc..
To pick ‘content-desc‘ as equals,then syntax looks like
WebElement test=driver. findElementBy.AndroidUIAutomator(“new
6. Finding elements by Xpath
Just like in Selenium, we can use Xpath to locate an element. We can also customize xpath using axis. Though xpath works bit slower compared to ID, this helps us in locating exact element. Syntax would be like:
driver.findElement(By.xpath(xpath of element));
I know this is simple syntax. Now, let’s see few customized xpaths to locate an element
a. XPath using class name and ‘text’
In above ‘android.widget.Button’ is a classname and text value is ‘5’
b. Xpath using ‘contains’
In above command we are using contains to locate an element having ‘android.widget.Button’ class name and having ‘resource-id’ = ‘digit5’
c. Xpath using ‘contains’ and ‘and’ to club locator values
driver.findElement(By.xpath(“//android.widget.Button[contains(@resource-id,’digit5′) and @text=’5′]”));
In above command, we are locating an element having class name ‘android.widget.Button’ which contains ID ‘resource-id’ and having ‘text’
We can customize xpath in many ways. Here I’m not posting all about it. You can find few basics of it here. Also you can surf in google to know more about xpath customization. Now you’re good with basic part of Appium. Let’s dive into coding part.