Inspecting elements using UIAutomatorViewer
Before starting writing test scripts, we need to identify elements using attributes such as id, name, class etc to perform actions. In order to identify elements, we use UIAutomatorViewer and Appium inspector to capture a snapshot of the current device screen which shows the hierarchy of the UI components arranged in the screen.
UI Automator Viewer is a GUI tool that comes with Android SDK which is used to inspect UI components of an Android Application and view the properties associated. We can find it in the Android SDK “tools” folder with the file name 'uiautomatorviewer.bat' on Windows.
How to work with UIAutomatorviewer?
There are several ways by which we can launch the uiautomatorviewer.
- From manually going to the path where uiautomatorviewer exists (android SDK folder) and run the batch file (for windows).
- Manually to the location via terminal and run the uiautomatorviewer sh file (for mac). Note: Execution permission should be given to the file.
- You can add alias to run directly via terminal. In Mac machine add it in bash_profile file.
How to access elements using UIAutomatorViewer?
- Connect device to the PC via USB cable.
- Ensure that you have enabled USB Debugging mode for the device connected.
- Run adb devices command in command prompt/terminal to check if device is detected and online/available.
- If the device is not detected then below error would be shown.
5. The UiAutomatorviewer will be shown below screen.
6. Open an application and click the device screenshot icon from right up corner.
7. After clicking on 'Device Screenshot', an image of the current screen will be loaded in the UIAutomatorViewer and we can use the mouse to click on any element and see the details of that element. You can also use the hierarchical tree in the top right to browse to a particular element.
In the above image, the left side of the uiautomatorviewer shows captured screenshot and the right side panel is divided into two parts:
Part 1 - Upper part shows the UI XML snapshot shows the hierarchy of UI components arranged in the screen layout
Part 2 - The ‘Node Detail’ at the right bottom shows details of that element based on the UI components selected.
In the above screen shot, text area ‘Enter your Name’ shows with a red outline and its details are shown in the bottom right section 'Node Detail'.
Note: UIAutomatorviewer works with Android native app elements only.
How to work with Appium inspector?
A default inspector available in standalone appium (not in the npm version) is called appium inspector. Which can be used to inspect android and iOS elements. It simply mirrors the screen of the device and we can interact with it and get the details of the elements. Lets see how we can launch and work with it.
- Launch appium for your application/programs menu.
- Click to search icon from right up corner.
3.Add correct desired capabilities as per your device/emulator and application. Then click on the start session button.