Finding elements using locators in Appium


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.

UI_A_LocateElement

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.

Locate_ID
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 ‘+’.

Locate_Name

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:
 driver.findElement(By.className(“android.widget.Button”)).click();

Locate_ClassName

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:
List<MobileElement>
plusButton=driver.findElements(By.className(“android.widget.Button”));
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)
plusButton.get(16).click();

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:
driver.findElement(By.AccessibilityId(AccessibilityId value));

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

Locate_AccessibilityId

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)”);
assertEquals(“android.widget.FrameLayout”, element.getTagName());

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
UiSelector().description(\”equals\”)”);

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’
driver.findElement(By.xpath(“//android.widget.Button[@text=’5′]”))

XpathWithTextIn above ‘android.widget.Button’ is a classname and text value is ‘5’

b. Xpath using ‘contains’
driver.findElement(By.xpath(“//android.widget.Button[contains(@resource-id,’digit5′)]”));

Xpath_ResourceID_TextIn 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.

Advertisements

3 thoughts on “Finding elements using locators in Appium

  1. bhaskar August 24, 2016 / 8:19 pm

    awsome

    Like

  2. Siddhesh Sahane February 6, 2017 / 10:44 am

    Hi, I would like to know how to create an xpath with smart way like using parent and sibling for android element…
    Could you please help?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s