From 1b255378abcf7a2244e6be42b80adaf1955364e2 Mon Sep 17 00:00:00 2001 From: bootstraponline Date: Tue, 13 May 2014 12:03:16 -0400 Subject: [PATCH] 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! --- .../android/bootstrap/SocketServer.java | 1 - .../handler/DumpWindowHierarchy.java | 38 +++++++++++-------- .../android/bootstrap/handler/Find.java | 4 ++ .../bootstrap/utils/NotImportantViews.java | 6 ++- 4 files changed, 31 insertions(+), 18 deletions(-) diff --git a/lib/devices/android/bootstrap/src/io/appium/android/bootstrap/SocketServer.java b/lib/devices/android/bootstrap/src/io/appium/android/bootstrap/SocketServer.java index df448bd2e..78ce3ce95 100644 --- a/lib/devices/android/bootstrap/src/io/appium/android/bootstrap/SocketServer.java +++ b/lib/devices/android/bootstrap/src/io/appium/android/bootstrap/SocketServer.java @@ -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 { 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 efb77240a..3ea6fd8ec 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 @@ -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()); } } \ No newline at end of file diff --git a/lib/devices/android/bootstrap/src/io/appium/android/bootstrap/handler/Find.java b/lib/devices/android/bootstrap/src/io/appium/android/bootstrap/handler/Find.java index b3fcd8d8a..42844353c 100644 --- a/lib/devices/android/bootstrap/src/io/appium/android/bootstrap/handler/Find.java +++ b/lib/devices/android/bootstrap/src/io/appium/android/bootstrap/handler/Find.java @@ -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 { diff --git a/lib/devices/android/bootstrap/src/io/appium/android/bootstrap/utils/NotImportantViews.java b/lib/devices/android/bootstrap/src/io/appium/android/bootstrap/utils/NotImportantViews.java index 3dba0a9d6..35800017c 100644 --- a/lib/devices/android/bootstrap/src/io/appium/android/bootstrap/utils/NotImportantViews.java +++ b/lib/devices/android/bootstrap/src/io/appium/android/bootstrap/utils/NotImportantViews.java @@ -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; + } } } } \ No newline at end of file