In the first article:
Comparison of controls obtained by hierarchyviewer and uiautomatorviewer in android automated testing (1)
I said that an error will be reported when uiautomatorviewer obtains the dynamic interface. The error is as follows:
Today, when I learned uiautomator and used dumpWindowHierarchy () to obtain control information, I found that this method can obtain dynamic interface information. This makes up for the defect of uiautomatorviewer. Next we will post the process to you.
The predecessors wrote a lot about how to compile the information of java engineering files. I will give you the connections I learned.
Http://blog.csdn.net/robinzhou/article/details/8447058
I will focus on it. Java code:
package com.ibm.test;import java.io.File;import com.android.uiautomator.core.UiDevice;import com.android.uiautomator.core.UiObjectNotFoundException;import com.android.uiautomator.testrunner.UiAutomatorTestCase;public class MyUIauto extends UiAutomatorTestCase {private static final String TAG = "MyUIauto";public void testPressHome() throws UiObjectNotFoundException {File file = new File("/data/local/tmp/local/tmp/qian.xml");UiDevice uiDevice = getUiDevice();//uiDevice.pressHome();String realPath = "qian.xml";uiDevice.dumpWindowHierarchy(realPath);}}
I have read the dumpWindowHierarchy method source code:
public void [More ...] dumpWindowHierarchy(String fileName) { AccessibilityNodeInfo root = getAutomatorBridge().getQueryController().getAccessibilityRootNode(); if(root != null) { AccessibilityNodeInfoDumper.dumpWindowToFile( root, new File(new File(Environment.getDataDirectory(), "local/tmp"), fileName)); } }
The default directory on the official website is/data/local/tmp, but the local/tmp directory is added from the source code. The merged directory is the/data/local/tmp directory. This is also the cause of the error caused by creating a directory at first. It always reports a file-free error in/data/local/tmp. Later, I found the source code. Source code Website:
Http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.2_r1/com/android/uiautomator/core/UiDevice.java#UiDevice.dumpWindowHierarchy%28java.lang.String%29
After compiling the java project, use the ant command to generate the jar package, push the jar package to the device, and open the stopwatch. Run the script to get the xml file and capture the local file. View the xml file as follows:
Therefore, this method is the best way to obtain controls.