From 2485649171de7133e1c7c7a5c5c1cfbf32d9e00f Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Tue, 18 Jun 2024 18:29:19 +0200 Subject: [PATCH] chore: Expose standard xml source attributes in the corresponding step --- .../appium/uiautomator2/handler/Source.java | 20 ++----------------- .../appium/uiautomator2/utils/Attribute.java | 20 ++++++++++++++++--- .../utils/actions_scheduler/SourceStep.java | 5 ++--- 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/io/appium/uiautomator2/handler/Source.java b/app/src/main/java/io/appium/uiautomator2/handler/Source.java index e5dbbbba0..14926436c 100644 --- a/app/src/main/java/io/appium/uiautomator2/handler/Source.java +++ b/app/src/main/java/io/appium/uiautomator2/handler/Source.java @@ -16,34 +16,18 @@ package io.appium.uiautomator2.handler; -import java.util.HashSet; -import java.util.Set; - import io.appium.uiautomator2.core.AccessibilityNodeInfoDumper; import io.appium.uiautomator2.handler.request.SafeRequestHandler; import io.appium.uiautomator2.http.AppiumResponse; import io.appium.uiautomator2.http.IHttpRequest; -import io.appium.uiautomator2.utils.Attribute; import static io.appium.uiautomator2.utils.AXWindowHelpers.refreshAccessibilityCache; +import static io.appium.uiautomator2.utils.Attribute.xmlExposableAttributes; /** * Get page source. Return as string of XML doc */ public class Source extends SafeRequestHandler { - private static final Set includedAttributes = new HashSet<>(); - - private static synchronized Set getXmlSourceAttributes() { - if (includedAttributes.isEmpty()) { - for (Attribute attribute : Attribute.values()) { - if (attribute.isExposableToXml()) { - includedAttributes.add(attribute); - } - } - } - return includedAttributes; - } - public Source(String mappedUri) { super(mappedUri); } @@ -51,7 +35,7 @@ public Source(String mappedUri) { @Override protected AppiumResponse safeHandle(IHttpRequest request) { refreshAccessibilityCache(); - String xmlSource = new AccessibilityNodeInfoDumper(null, getXmlSourceAttributes()).dumpToXml(); + String xmlSource = new AccessibilityNodeInfoDumper(null, xmlExposableAttributes()).dumpToXml(); return new AppiumResponse(getSessionId(request), xmlSource); } } diff --git a/app/src/main/java/io/appium/uiautomator2/utils/Attribute.java b/app/src/main/java/io/appium/uiautomator2/utils/Attribute.java index f45a30cb5..6fde418c8 100644 --- a/app/src/main/java/io/appium/uiautomator2/utils/Attribute.java +++ b/app/src/main/java/io/appium/uiautomator2/utils/Attribute.java @@ -21,7 +21,9 @@ import androidx.annotation.Nullable; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; public enum Attribute { CHECKABLE(new String[]{"checkable"}), @@ -53,16 +55,18 @@ public enum Attribute { private final String[] aliases; // Defines if the attribute is visible to the user from getAttribute call - private boolean isExposable = true; + private final boolean isExposable; // Defines if the attribute is visible to the user in the xml tree/xpath search - private boolean isExposableToXml = true; + private final boolean isExposableToXml; Attribute(String[] aliases) { this.aliases = aliases; + this.isExposable = true; + this.isExposableToXml = true; } Attribute(String[] aliases, boolean isExposable, boolean isExposableToXml) { - this(aliases); + this.aliases = aliases; this.isExposable = isExposable; this.isExposableToXml = isExposableToXml; } @@ -96,6 +100,16 @@ public static String[] exposableAliases() { return result.toArray(new String[0]); } + public static Set xmlExposableAttributes() { + Set result = new HashSet<>(); + for (Attribute attribute : Attribute.values()) { + if (attribute.isExposableToXml) { + result.add(attribute); + } + } + return result; + } + @Nullable public static Attribute fromString(String alias) { if (alias == null) { diff --git a/app/src/main/java/io/appium/uiautomator2/utils/actions_scheduler/SourceStep.java b/app/src/main/java/io/appium/uiautomator2/utils/actions_scheduler/SourceStep.java index 97dfe36d6..be93c9731 100644 --- a/app/src/main/java/io/appium/uiautomator2/utils/actions_scheduler/SourceStep.java +++ b/app/src/main/java/io/appium/uiautomator2/utils/actions_scheduler/SourceStep.java @@ -17,8 +17,7 @@ package io.appium.uiautomator2.utils.actions_scheduler; import static io.appium.uiautomator2.utils.AXWindowHelpers.refreshAccessibilityCache; - -import java.util.Collections; +import static io.appium.uiautomator2.utils.Attribute.xmlExposableAttributes; import io.appium.uiautomator2.core.AccessibilityNodeInfoDumper; import io.appium.uiautomator2.model.api.scheduled.ScheduledActionStepModel; @@ -46,6 +45,6 @@ protected Object runInternalImplementation(String subtype) throws UnknownStepSub private String fetchXmlSource() { refreshAccessibilityCache(); - return new AccessibilityNodeInfoDumper(null, Collections.emptySet()).dumpToXml(); + return new AccessibilityNodeInfoDumper(null, xmlExposableAttributes()).dumpToXml(); } }