diff --git a/lib/devices/android/android-controller.js b/lib/devices/android/android-controller.js index ebdcf5517..e0321840a 100644 --- a/lib/devices/android/android-controller.js +++ b/lib/devices/android/android-controller.js @@ -380,6 +380,14 @@ androidController.getPageSource = function (cb) { xml = fs.readFileSync(xmlFile, 'utf8'); fs.unlinkSync(xmlFile); } + + // xml file may not exist or it could be empty. + if (xml === '') { + var error = "dumpWindowHierarchy failed"; + logger.error(error); + return cb(error); + } + try { xml = _updateSourceXMLNodeNames(xml); } catch (e) { diff --git a/lib/devices/android/bootstrap/src/io/appium/android/bootstrap/handler/DumpWindowHierarchy.java b/lib/devices/android/bootstrap/src/io/appium/android/bootstrap/handler/DumpWindowHierarchy.java index 3ea6fd8ec..69a32aaca 100644 --- a/lib/devices/android/bootstrap/src/io/appium/android/bootstrap/handler/DumpWindowHierarchy.java +++ b/lib/devices/android/bootstrap/src/io/appium/android/bootstrap/handler/DumpWindowHierarchy.java @@ -1,5 +1,7 @@ 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; @@ -7,11 +9,6 @@ import io.appium.android.bootstrap.utils.NotImportantViews; import java.io.File; -import android.os.Environment; -import android.os.SystemClock; - -import com.android.uiautomator.core.UiDevice; - /** * This handler is used to dumpWindowHierarchy. * https://android.googlesource.com/ @@ -22,28 +19,28 @@ 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 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 final File dumpFile = new File(dumpFolder, dumpFileName); + + private static void deleteDumpFile() { + if (dumpFile.exists()) { + dumpFile.delete(); + } + } public static boolean dump() { dumpFolder.mkdirs(); - if (dumpFile.exists()) { - dumpFile.delete(); - } + deleteDumpFile(); - UiDevice.getInstance().dumpWindowHierarchy(dumpFileName); - - if (!dumpFile.exists()) { - for (int count = 0; count < 30; count++) { - SystemClock.sleep(1000); - UiDevice.getInstance().dumpWindowHierarchy(dumpFileName); - - if (dumpFile.exists()) { - break; - } - } + 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();