Discard NotImportantViews only for XPath & DumpWindow

When setCompressedLayoutHeirarchy is enabled, views not marked as important to accessibility are discarded. This must be enabled for XPath and page source to work. If it's left enabled then it's not possible to find unimportant elements via other locator strategies.

If discard is toggled back to false at the end of an XPath request it will break XPath. To safely set discard to false, the code waits for the next find request that doesn't use XPath.

Thanks @paymand for figuring out how to make this work!
This commit is contained in:
bootstraponline
2014-05-13 12:03:16 -04:00
parent c7a99f0354
commit 1b255378ab
4 changed files with 31 additions and 18 deletions

View File

@@ -44,7 +44,6 @@ class SocketServer {
* @throws SocketServerException
*/
public SocketServer(final int port) throws SocketServerException {
NotImportantViews.discard(true); // must be set for API 18+
keepListening = true;
executor = new AndroidCommandExecutor();
try {

View File

@@ -3,6 +3,7 @@ package io.appium.android.bootstrap.handler;
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;
@@ -21,22 +22,11 @@ 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 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);
/*
* @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) {
public static boolean dump() {
dumpFolder.mkdirs();
if (dumpFile.exists()) {
@@ -56,6 +46,22 @@ public class DumpWindowHierarchy extends CommandHandler {
}
}
return getSuccessResult(dumpFile.exists());
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());
}
}

View File

@@ -15,6 +15,7 @@ import io.appium.android.bootstrap.exceptions.InvalidStrategyException;
import io.appium.android.bootstrap.exceptions.UiSelectorSyntaxException;
import io.appium.android.bootstrap.exceptions.UnallowedTagNameException;
import io.appium.android.bootstrap.selector.Strategy;
import io.appium.android.bootstrap.utils.NotImportantViews;
import io.appium.android.bootstrap.utils.UiSelectorParser;
import java.util.ArrayList;
@@ -187,7 +188,10 @@ public class Find extends CommandHandler {
+ " with the contextId: " + contextId);
if (strategy == Strategy.INDEX_PATHS) {
NotImportantViews.discard(true);
return findElementsByIndexPaths(text, multiple);
} else {
NotImportantViews.discard(false);
}
try {

View File

@@ -8,10 +8,14 @@ public abstract class NotImportantViews {
// setCompressedLayoutHeirarchy doesn't exist on API <= 17
// http://developer.android.com/reference/android/accessibilityservice/AccessibilityServiceInfo.html#FLAG_INCLUDE_NOT_IMPORTANT_VIEWS
private static boolean canDiscard = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2;
private static boolean lastDiscard = false;
public static void discard(boolean discard) {
if (canDiscard) {
UiDevice.getInstance().setCompressedLayoutHeirarchy(discard);
if (discard != lastDiscard) {
UiDevice.getInstance().setCompressedLayoutHeirarchy(discard);
lastDiscard = discard;
}
}
}
}