How to Inspect Element using UIAutomatorViewer in Appium
By Garima Tiwari, Sr. Content Writer - December 23, 2022
UI plays a major role in customer engagement and retention. Having an interactive and smart UI delights users and helps retain them. Thus, it is very important to perform UI testing of the mobile application, testing 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. UIAutomatorViewer is an Android App Testing tool developed by Google that is used to locate UI elements in Android App for testing its functionalities.
Appium is widely used for automating mobile app testing. It helps execute automated UI 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. This article discusses different ways to inspect or locate UI elements in an Android App 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 in Appium.
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.
- Once 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.
- 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.
Run Appium Tests on Real Device Cloud for Free
Script to Identify the + Button in the Android Calculator App
1. 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();
2. Find the + Button using Accessibility ID Property and click it
driver.findElementByAccessibilityId("plus").click();
3. 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();
4. 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.
It is important to run the Appium automation tests on real Android devices. BrowserStack offers a real device cloud of thousands of real mobile devices installed with real operating systems for testing purposes. Simply Sign up for Free, choose the required device-OS combination, and start testing apps for free in real user conditions using BrowserStack App Automate.