Merge pull request #2657 from bootstraponline/npe_fix

Fix DumpWindowHierarchy NullPointerException
This commit is contained in:
Jonah
2014-05-23 13:27:14 -07:00
2 changed files with 26 additions and 21 deletions

View File

@@ -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) {

View File

@@ -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();