How to Inspect or Locate Element using UIAutomatorViewer in Appium: Tutorial
Garima Tiwari
Posted on April 22, 2022
Appium is widely used for automated mobile app testing. It helps execute automated tests on native and hybrid apps for faster results. To fulfill the growing demand for apps that serve, delight, and retain users, the need for automated app testing continues to increase. That is why Appium is widely used to perform automation testing, which helps development and testing professionals deliver high-end apps within tight deadlines.
For any application, User Interface (UI) plays a major role. It is the UI elements that the end-user interacts with. Thus, it is very important to test UI elements individually. These elements can be located using GUI (Graphic User Interface) tools like UIAutomatorViewer, and then tested with desired scenarios involving user interactions.
This article will discuss different ways to inspect or locate UI elements using UIAutomatorViewer in Appium.
Before we begin, let’s look at different ways used to define a UI Element.
Ways of defining a UI Element
UI Elements can be defined by ID, ClassName, Name, Accessibility ID, XPath.
To understand how to inspect or locate a UI element with UIAutomatorViewer, this article will use the example of the Android Calculator. The aim is to locate the element that is the + button with the help of UIAutomator View, and Click on it.
Setting up UIAutomatorViewer
Before creating the script to inspect the desired element, let’s look at setting up UIAutomatorViewer.
Prerequisites for setting up UIAutomatorViewer
- Install Appium
- Install Android Studio and SDK Tools
- Install Java and set up the environment variables
- Install Eclipse IDE for Java
- Connect the Android device where the test application is to be tested. The system must have Appium Setup using USB
Once the Appium Server is launched
Open UIAutomatorViewer
This can be done by either of the following methods:
- entering uiautomatorviewer in the command prompt
- opening uiautomatorviewer.bat file in the Android installation folder with the following command: Android >> Android-SDK >> Tools >> UIAutomatorViewer.bat
Locating Element using UIAutomatorViewer
This article will explore a test case demonstrating how to locate the + button on the Android Calculator App using Java and click it.
the UIAutomatorViewer is opened, open the target app that has to be tested – Android Calculator in this example.
Click on the Device Screenshot Icon to display the screen of the Android device in the window. It looks like the screenshot below.
UIAutomatorViewer Window with Android Calculator App
Move the cursor on the target element that has to be located – the + button.
Note down the values mentioned in the Node Detail Tab of the UIAutomatorViewer window.
Note the values of Text, ResourceID, Class, Package, and Content Desc. These values will be used when writing the test script to locate the element.
Script to Identify the + Button in the Android Calculator App
- Find the + Button using ID and click it
driver.findElement(By.id("com.android.calculator2:id/plus")).click();
Or
driver.findElementById("com.android.calculator2:id/plus").click();
- Find the + Button using Accessibility ID Property and click it
driver.findElementByAccessibilityId("plus").click();
- Find the + Button using XPath and click it
driver.findElement(By.xpath("//android.widget.Button[@content-desc = 'plus']")).click();
Or
driver.findElement(By.xpath("//*[@content-desc = 'plus']")).click();
- Find the + Button using ClassName **and **findElements() method and click it
List<MobileElement> elements = driver.findElements(By.className("android.widget.Button"));
for(MobileElement element : elements) {
if(element.getAttribute("contentDescription").equals("plus")) {
element.click();
break;
}
}
Conclusion
As demonstrated, inspecting a desired element from the target Android App under test is easy enough using UIAutomatorViewer in Appium. This can be used to test the functionality of UI elements while performing Android App Automation testing.
Posted on April 22, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.