21 May 2024 Leave a comment Tech-Help
When working in environments that restrict the use of graphical tools, extracting layout attributes such as id, position, and text via ADB commands becomes essential. This guide will walk you through the process of obtaining the current layout attributes using ADB, similar to what you would achieve with tools like uiautomatorviewer.
Using ADB to Dump UI Hierarchy
The uiautomator
tool in ADB can be utilized to create an XML dump of the current UI hierarchy. This can be particularly useful for debugging and automated testing purposes.
Steps to Dump UI Hierarchy
- Open your terminal or command prompt.
- Run the following command to dump the UI hierarchy to a default location on the device:
- The output will indicate the location where the XML file is stored, usually on the external storage:
- To pull the XML file to your local machine, use the following command:
adb shell uiautomator dump
UI hierarchy dumped to: /sdcard/window_dump.xml
adb pull /sdcard/window_dump.xml ./window_dump.xml
Combining Dump and Pull into a Single Command
You can streamline the process by combining the dumping and pulling steps into a single command. This can be done by dumping directly to the standard output:
adb exec-out uiautomator dump /dev/tty
This command will print the entire UI hierarchy to the console, allowing you to capture it directly.
Alternative Tools for UI Dumping
In addition to the built-in uiautomator tool, there are other tools available that provide more features and flexibility.
AndroidViewClient/culebra
AndroidViewClient’s dump
tool offers a comprehensive way to extract and manipulate the UI hierarchy. It provides various options to control the output, making it a powerful alternative.
Basic Usage
dump [OPTION]... [serialno]
Running the dump
command without any options will print the tree of views along with some basic properties such as IDs.
Obtaining View Positions
To get the positions of the views, use the following option:
dump -x
This will include the coordinates of each view in the output, providing detailed information about their layout on the screen.
Extracting Layout Information in JSON Format
With newer versions of AndroidViewClient, you can also obtain the view hierarchy dump in JSON format, which can be more convenient for processing and integration with other tools.
dump -ah emulator-5554 | jq
This command will output the hierarchy in JSON format, which can be further manipulated using tools like jq
.
Practical Example
Let’s consider a practical example where we need to get the layout information of a specific button on the screen. First, we dump the UI hierarchy:
adb exec-out uiautomator dump /dev/tty
Next, we locate the bounds of the target control in the XML output:
<node index="0" text="" resource-id="com.example.app:id/button" class="android.widget.Button" bounds="[100,200][300,400]">
We can calculate the center position of the button using the bounds values and simulate a tap:
adb shell input tap 200 300
Automating the Process with Repeato
While these manual steps are effective, automating the process can significantly enhance productivity and reduce errors. Repeato, a no-code test automation tool for iOS and Android, offers robust solutions for creating, running, and maintaining automated tests for your apps.
Repeato’s integration with ADB allows you to execute ADB commands via script steps, enabling precise control over timing and sequence. This feature is particularly useful for tasks that involve extracting and interacting with layout information.
For more details on how to leverage Repeato for your automated testing needs, visit our documentation.
By harnessing the power of ADB and tools like Repeato, you can streamline your testing process and ensure your applications perform optimally across various scenarios.