# DumpWindowHierarchy
~~~
package io.appium.android.bootstrap.handler;
import android.os.Environment;
import com.android.uiautomator.core.UiDevice;
import io.appium.android.bootstrap.AndroidCommand;
import io.appium.android.bootstrap.AndroidCommandResult;
import io.appium.android.bootstrap.CommandHandler;
import io.appium.android.bootstrap.utils.NotImportantViews;
import java.io.File;
/**
* This handler is used to dumpWindowHierarchy.
* https://android.googlesource.com/
* platform/frameworks/testing/+/master/uiautomator
* /library/core-src/com/android/uiautomator/core/UiDevice.java
*/
@SuppressWarnings("ResultOfMethodCallIgnored")
public class DumpWindowHierarchy extends CommandHandler {
// Note that
// "new File(new File(Environment.getDataDirectory(), "local/tmp"), fileName)"
// is directly from the UiDevice.java source code.
private static final File dumpFolder = new File(Environment.getDataDirectory(), "local/tmp");
private static final String dumpFileName = "dump.xml";
private static final File dumpFile = new File(dumpFolder, dumpFileName);
private static void deleteDumpFile() {
if (dumpFile.exists()) {
dumpFile.delete();
}
}
public static boolean dump() {
dumpFolder.mkdirs();
deleteDumpFile();
try {
// dumpWindowHierarchy often has a NullPointerException
UiDevice.getInstance().dumpWindowHierarchy(dumpFileName);
} catch (Exception e) {
e.printStackTrace();
// If there's an error then the dumpfile may exist and be empty.
deleteDumpFile();
}
return dumpFile.exists();
}
/*
* @param command The {@link AndroidCommand} used for this handler.
*
* @return {@link AndroidCommandResult}
*
* @throws JSONException
*
* @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android.
* bootstrap.AndroidCommand)
*/
@Override
public AndroidCommandResult execute(final AndroidCommand command) {
NotImportantViews.discard(true);
return getSuccessResult(dump());
}
}
~~~
這個方法可能在某些機器上執行到不成功而讓很多人對這個方法不甚了解,而我做了很長事件的功能遍歷工具的開發,專門研究過這個方法,看看我之前寫的文章就理解它是做什么的啦:
[dumpWindowHierarchy](http://blog.csdn.net/itfootball/article/details/dumpWindowHierarchy)
它是獲取當前手機界面所有控件信息,然后把樹形結構保存在/data/local/tmp的目錄下的dump.xml文件中,所以我們看見上面類的定義有很多關于路徑、文件的字符串,就是這個原因,appium的這個DumpWindowHierarchy首先根據api的不同設置是否禁止布局壓縮。如果api為18以上的,那么就啟動布局壓縮,這對我們獲取有用的控件信息是很有作用的。然后執行dump()方法,dump方法會先創建目錄,當然該目錄一般都會存在,無需創建。然后刪除dump.xml文件,因為要創建新的,必須刪除舊的,以免獲取不到控件信息的時候,dump.xml里仍然有就信息返回。然后調用
~~~
UiDevice.getInstance().dumpWindowHierarchy(dumpFileName);
~~~
將控件樹信息保存在里文件中。
(android官網終于可以上了!)
- 前言
- appium框架之bootstrap
- bootstrap之Click事件
- bootstrap之WaitForIdle&&Clear
- bootstrap之Orientation
- bootstrap之Swipe
- bootstrap之Flick
- bootstrap之Drag
- bootstrap之Pinch
- bootstrap之鼠標操作
- bootstrap之文本框的操作
- bootstrap之GetName&&GetAttribute&&GetDeviceSize&&GetSize&&GetLocation&&GetDataDir
- bootstrap之ScrollTo
- bootstrap之Wake&&PressBack&&TakeScreenshot&&OpenNotification
- bootstrap之PressKeyCode&&LongPressKeyCode
- bootstrap之DumpWindowHierarchy
- bootstrap之UpdateStrings
- bootstrap之MultiPointerGesture