diff --git a/docs/Advanced-By.md b/docs/Advanced-By.md
index 96609f2a7..3fb3ee284 100644
--- a/docs/Advanced-By.md
+++ b/docs/Advanced-By.md
@@ -105,7 +105,36 @@ to learn more about the general concept.
@iOSXCUITFindBy(iOSClassChain = "**/XCUIElementTypeCell[`name == 'Foo'`][-1]")
```
-## Android's uiAutomator String
+## Android's Predicate String and Class Chain Queries
+
+Available when using [AndroidBy](AndroidBy) and [AndroidFindBy](AndroidFindBy) with
+[appium-uiautomator2-driver](https://github.com/appium/appium-uiautomator2-driver) 10.1.0 (class chain)
+and 10.2.0 (predicate string) or newer. They are evaluated directly on the accessibility tree, are about as fast as
+`UiSelector` and faster than XPath, and replace the deprecated [uiAutomator](#androids-uiautomator-string) strategy.
+Read the [Class Chain Locator Strategy](https://appium.github.io/appium-uiautomator2-driver/latest/guides/android-class-chain/)
+guide for the syntax.
+
+```java
+// A predicate string finds the elements that satisfy it:
+@AndroidFindBy(androidPredicate = "text == 'Hello World'")
+@AndroidFindBy(androidPredicate = "class == 'android.widget.Button' AND enabled == true")
+@AndroidFindBy(androidPredicate = "text CONTAINS[c] 'hello' OR content-desc BEGINSWITH 'menu'")
+
+// A class chain can also address the parent, the descendants and the position of the elements:
+@AndroidFindBy(androidClassChain = "**/android.widget.TextView[`text == 'Hello World'`]")
+@AndroidFindBy(androidClassChain = "**/android.widget.ListView/android.widget.TextView[2]")
+@AndroidFindBy(androidClassChain = "**/android.widget.LinearLayout[$text == 'Settings'$]")
+```
+
+The same locators are available as `AppiumBy.androidPredicateString` and `AppiumBy.androidClassChain`.
+
+## Android's uiAutomator String (deprecated)
+
+**Deprecated:** Google is going to remove `UiSelector` and `UiScrollable` from the UiAutomator framework, so
+this strategy is going to stop working. Use the predicate string and class chain queries above to find elements, and
+`mobile: scroll` or `mobile: scrollGesture` to scroll. See
+[Migrating from UiScrollable and UiSelector](https://appium.github.io/appium-uiautomator2-driver/latest/guides/migrating-from-uiscrollable/)
+for the equivalents.
Available when using [AndroidBy](AndroidBy) and [AndroidFindBy](AndroidFindBy) with
[appium-uiautomator2-server](https://github.com/appium/appium-uiautomator2-server). This
diff --git a/src/main/java/io/appium/java_client/AppiumBy.java b/src/main/java/io/appium/java_client/AppiumBy.java
index 0874769ce..4598d0dbf 100644
--- a/src/main/java/io/appium/java_client/AppiumBy.java
+++ b/src/main/java/io/appium/java_client/AppiumBy.java
@@ -101,11 +101,44 @@ public static By androidDataMatcher(final String dataMatcherString) {
*
* @param uiautomatorText is Android UIAutomator string
* @return an instance of {@link ByAndroidUIAutomator}
- */
+ * @deprecated Google is going to remove {@code UiSelector} and {@code UiScrollable} from the UiAutomator
+ * framework, so this strategy is going to stop working. Use {@link #androidPredicateString(String)} or
+ * {@link #androidClassChain(String)} to find elements, and {@code mobile: scroll} or
+ * {@code mobile: scrollGesture} to scroll. See
+ *
+ * Migrating from UiScrollable and UiSelector.
+ */
+ @Deprecated
public static By androidUIAutomator(final String uiautomatorText) {
return new ByAndroidUIAutomator(uiautomatorText);
}
+ /**
+ * This locator strategy is available in UiAutomator2 Driver mode since driver version 10.1.0.
+ *
+ * @param androidClassChainString is a valid class chain locator string.
+ * See
+ * the documentation for more details
+ * @return an instance of {@link AppiumBy.ByAndroidClassChain}
+ */
+ public static By androidClassChain(final String androidClassChainString) {
+ return new ByAndroidClassChain(androidClassChainString);
+ }
+
+ /**
+ * This locator strategy is available in UiAutomator2 Driver mode since driver version 10.2.0.
+ * It finds all elements below the search root that satisfy the predicate, for example
+ * {@code text == 'Gallery' AND clickable == true}.
+ *
+ * @param androidPredicateString is a valid predicate string.
+ * See
+ * the documentation for more details
+ * @return an instance of {@link AppiumBy.ByAndroidPredicateString}
+ */
+ public static By androidPredicateString(final String androidPredicateString) {
+ return new ByAndroidPredicateString(androidPredicateString);
+ }
+
/**
* This locator strategy is only available in Espresso Driver mode.
*
@@ -344,7 +377,11 @@ protected ByAndroidDataMatcher(String locatorString) {
/**
* Locates elements by an Android UIAutomator expression.
+ *
+ * @deprecated Google is going to remove {@code UiSelector} and {@code UiScrollable} from the UiAutomator
+ * framework. Use {@link ByAndroidPredicateString} or {@link ByAndroidClassChain} instead.
*/
+ @Deprecated
public static class ByAndroidUIAutomator extends AppiumBy implements Serializable {
/**
* Creates a new instance for the given locator.
@@ -356,6 +393,34 @@ public ByAndroidUIAutomator(String uiautomatorText) {
}
}
+ /**
+ * Locates elements by an Android class chain (UiAutomator2 driver only).
+ */
+ public static class ByAndroidClassChain extends AppiumBy implements Serializable {
+ /**
+ * Creates a new instance for the given locator.
+ *
+ * @param locatorString the locator string
+ */
+ protected ByAndroidClassChain(String locatorString) {
+ super("-android class chain", locatorString, "androidClassChain");
+ }
+ }
+
+ /**
+ * Locates elements by an Android predicate string (UiAutomator2 driver only).
+ */
+ public static class ByAndroidPredicateString extends AppiumBy implements Serializable {
+ /**
+ * Creates a new instance for the given locator.
+ *
+ * @param locatorString the locator string
+ */
+ protected ByAndroidPredicateString(String locatorString) {
+ super("-android predicate string", locatorString, "androidPredicateString");
+ }
+ }
+
/**
* Locates elements by an Espresso view matcher (Espresso driver only).
*/
diff --git a/src/main/java/io/appium/java_client/pagefactory/AndroidBy.java b/src/main/java/io/appium/java_client/pagefactory/AndroidBy.java
index b40acdd07..c8fc1bbf3 100644
--- a/src/main/java/io/appium/java_client/pagefactory/AndroidBy.java
+++ b/src/main/java/io/appium/java_client/pagefactory/AndroidBy.java
@@ -25,9 +25,28 @@
* Refer to https://developer.android.com/training/testing/ui-automator
*
* @return an Android UIAutomator string
+ * @deprecated Google is going to remove {@code UiSelector} and {@code UiScrollable} from the UiAutomator
+ * framework. Use {@link #androidPredicate()} or {@link #androidClassChain()} instead.
*/
+ @Deprecated
String uiAutomator() default "";
+ /**
+ * It is an Android class chain locator, available since appium-uiautomator2-driver 10.1.0.
+ * Refer to https://appium.github.io/appium-uiautomator2-driver/latest/guides/android-class-chain/
+ *
+ * @return an Android class chain string
+ */
+ String androidClassChain() default "";
+
+ /**
+ * It is an Android predicate string locator, available since appium-uiautomator2-driver 10.2.0.
+ * Refer to https://appium.github.io/appium-uiautomator2-driver/latest/guides/android-class-chain/#predicate-string-strategy
+ *
+ * @return an Android predicate string
+ */
+ String androidPredicate() default "";
+
/**
* It an UI automation accessibility Id which is a convenient to Android.
* About Android accessibility
diff --git a/src/main/java/io/appium/java_client/pagefactory/AndroidFindBy.java b/src/main/java/io/appium/java_client/pagefactory/AndroidFindBy.java
index f0ee7b7a9..a09bcfc31 100644
--- a/src/main/java/io/appium/java_client/pagefactory/AndroidFindBy.java
+++ b/src/main/java/io/appium/java_client/pagefactory/AndroidFindBy.java
@@ -40,9 +40,28 @@
* Refer to https://developer.android.com/training/testing/ui-automator
*
* @return an Android UIAutomator string
+ * @deprecated Google is going to remove {@code UiSelector} and {@code UiScrollable} from the UiAutomator
+ * framework. Use {@link #androidPredicate()} or {@link #androidClassChain()} instead.
*/
+ @Deprecated
String uiAutomator() default "";
+ /**
+ * It is an Android class chain locator, available since appium-uiautomator2-driver 10.1.0.
+ * Refer to https://appium.github.io/appium-uiautomator2-driver/latest/guides/android-class-chain/
+ *
+ * @return an Android class chain string
+ */
+ String androidClassChain() default "";
+
+ /**
+ * It is an Android predicate string locator, available since appium-uiautomator2-driver 10.2.0.
+ * Refer to https://appium.github.io/appium-uiautomator2-driver/latest/guides/android-class-chain/#predicate-string-strategy
+ *
+ * @return an Android predicate string
+ */
+ String androidPredicate() default "";
+
/**
* It an UI automation accessibility Id which is a convenient to Android.
* About Android accessibility
diff --git a/src/main/java/io/appium/java_client/pagefactory/bys/builder/Strategies.java b/src/main/java/io/appium/java_client/pagefactory/bys/builder/Strategies.java
index 590db8278..c387a2b1b 100644
--- a/src/main/java/io/appium/java_client/pagefactory/bys/builder/Strategies.java
+++ b/src/main/java/io/appium/java_client/pagefactory/bys/builder/Strategies.java
@@ -30,7 +30,9 @@
enum Strategies {
BYUIAUTOMATOR("uiAutomator") {
- @Override By getBy(Annotation annotation) {
+ @Override
+ @SuppressWarnings("deprecation")
+ By getBy(Annotation annotation) {
String value = getValue(annotation, this);
if (annotation.annotationType().equals(AndroidFindBy.class)
|| annotation.annotationType().equals(AndroidBy.class)) {
@@ -100,6 +102,18 @@ enum Strategies {
.androidViewMatcher(getValue(annotation, this));
}
},
+ BY_ANDROID_CLASS_CHAIN("androidClassChain") {
+ @Override By getBy(Annotation annotation) {
+ return AppiumBy
+ .androidClassChain(getValue(annotation, this));
+ }
+ },
+ BY_ANDROID_PREDICATE("androidPredicate") {
+ @Override By getBy(Annotation annotation) {
+ return AppiumBy
+ .androidPredicateString(getValue(annotation, this));
+ }
+ },
BY_NS_PREDICATE("iOSNsPredicate") {
@Override By getBy(Annotation annotation) {
return AppiumBy
diff --git a/src/test/java/io/appium/java_client/pagefactory_tests/ByFieldDecorationTest.java b/src/test/java/io/appium/java_client/pagefactory_tests/ByFieldDecorationTest.java
index 12b729c81..359d75516 100644
--- a/src/test/java/io/appium/java_client/pagefactory_tests/ByFieldDecorationTest.java
+++ b/src/test/java/io/appium/java_client/pagefactory_tests/ByFieldDecorationTest.java
@@ -201,6 +201,14 @@ public static class CrossPlatformByPage {
@iOSXCUITFindBy(accessibility = "appium_ios")
private AppiumBy somethingAppiumBy;
+ @AndroidFindBy(androidClassChain = "**/*[`text == 'chain_android'`]")
+ @iOSXCUITFindBy(iOSClassChain = "**/XCUIElementTypeButton")
+ private AppiumBy classChainAppiumBy;
+
+ @AndroidFindBy(androidPredicate = "text == 'predicate_android'")
+ @iOSXCUITFindBy(iOSNsPredicate = "name == 'predicate_ios'")
+ private AppiumBy predicateAppiumBy;
+
@FindBy(id = "web_only")
@AndroidFindBy(id = "native_android")
@iOSXCUITFindBy(accessibility = "native_ios")
@@ -321,6 +329,8 @@ public void decoratesByAndAppiumByFieldsAndSwitchesContextLazilyOnAndroid() {
assertInstanceOf(ContentMappedBy.class, page.cachedAndTimedBy);
assertEquals(AppiumBy.id("appium_android"), page.somethingAppiumBy);
+ assertEquals(AppiumBy.androidClassChain("**/*[`text == 'chain_android'`]"), page.classChainAppiumBy);
+ assertEquals(AppiumBy.androidPredicateString("text == 'predicate_android'"), page.predicateAppiumBy);
assertInstanceOf(ByChained.class, page.multiLocatorBy);
assertInstanceOf(ByAll.class, page.allPossibleBy);
assertInstanceOf(ByChained.class, page.findBysField);
diff --git a/src/test/java/io/appium/java_client/remote/DriverWireScenariosTest.java b/src/test/java/io/appium/java_client/remote/DriverWireScenariosTest.java
index a8542460b..a662e2866 100644
--- a/src/test/java/io/appium/java_client/remote/DriverWireScenariosTest.java
+++ b/src/test/java/io/appium/java_client/remote/DriverWireScenariosTest.java
@@ -234,6 +234,7 @@ private static final class Context {
for (var by : new By[]{By.id("a"), By.name("n"), By.className("c"), By.cssSelector(".x > y"),
By.xpath("//a[@b='c']"), By.linkText("l"), By.partialLinkText("p"), By.tagName("t"),
AppiumBy.accessibilityId("acc"), AppiumBy.androidUIAutomator("new UiSelector()"),
+ AppiumBy.androidClassChain("**/*[`text == 'a'`]"), AppiumBy.androidPredicateString("text == 'a'"),
AppiumBy.iOSClassChain("**/XCUIElementTypeButton"), AppiumBy.id("appium-id"),
AppiumBy.image("aGVsbG8="), AppiumBy.className("cn")}) {
SCENARIOS.put("findElement " + by, c -> c.driver.findElement(by));
diff --git a/src/test/resources/wire-golden/driver-calls.json b/src/test/resources/wire-golden/driver-calls.json
index e406a22b8..40530c38b 100644
--- a/src/test/resources/wire-golden/driver-calls.json
+++ b/src/test/resources/wire-golden/driver-calls.json
@@ -901,6 +901,164 @@
}
]
},
+ "findElement AppiumBy.androidClassChain: **/*[`text == 'a'`]": {
+ "result": "element:el1",
+ "requests": [
+ {
+ "method": "POST",
+ "uri": "/wd/hub/session/s1/element",
+ "headers": {
+ "content-type": "application/json; charset=utf-8",
+ "user-agent": "appium/x.y.z (selenium/x.y.z (java os))"
+ },
+ "body": {
+ "using": "-android class chain",
+ "value": "**/*[`text == 'a'`]"
+ },
+ "client": "https://appium.example.com:4723"
+ }
+ ]
+ },
+ "findElements AppiumBy.androidClassChain: **/*[`text == 'a'`]": {
+ "result": [
+ "element:el1",
+ "element:el2"
+ ],
+ "requests": [
+ {
+ "method": "POST",
+ "uri": "/wd/hub/session/s1/elements",
+ "headers": {
+ "content-type": "application/json; charset=utf-8",
+ "user-agent": "appium/x.y.z (selenium/x.y.z (java os))"
+ },
+ "body": {
+ "using": "-android class chain",
+ "value": "**/*[`text == 'a'`]"
+ },
+ "client": "https://appium.example.com:4723"
+ }
+ ]
+ },
+ "findChildElement AppiumBy.androidClassChain: **/*[`text == 'a'`]": {
+ "result": "element:el2",
+ "requests": [
+ {
+ "method": "POST",
+ "uri": "/wd/hub/session/s1/element/el1/element",
+ "headers": {
+ "content-type": "application/json; charset=utf-8",
+ "user-agent": "appium/x.y.z (selenium/x.y.z (java os))"
+ },
+ "body": {
+ "id": "el1",
+ "using": "-android class chain",
+ "value": "**/*[`text == 'a'`]"
+ },
+ "client": "https://appium.example.com:4723"
+ }
+ ]
+ },
+ "findChildElements AppiumBy.androidClassChain: **/*[`text == 'a'`]": {
+ "result": [
+ "element:el2"
+ ],
+ "requests": [
+ {
+ "method": "POST",
+ "uri": "/wd/hub/session/s1/element/el1/elements",
+ "headers": {
+ "content-type": "application/json; charset=utf-8",
+ "user-agent": "appium/x.y.z (selenium/x.y.z (java os))"
+ },
+ "body": {
+ "id": "el1",
+ "using": "-android class chain",
+ "value": "**/*[`text == 'a'`]"
+ },
+ "client": "https://appium.example.com:4723"
+ }
+ ]
+ },
+ "findElement AppiumBy.androidPredicateString: text == 'a'": {
+ "result": "element:el1",
+ "requests": [
+ {
+ "method": "POST",
+ "uri": "/wd/hub/session/s1/element",
+ "headers": {
+ "content-type": "application/json; charset=utf-8",
+ "user-agent": "appium/x.y.z (selenium/x.y.z (java os))"
+ },
+ "body": {
+ "using": "-android predicate string",
+ "value": "text == 'a'"
+ },
+ "client": "https://appium.example.com:4723"
+ }
+ ]
+ },
+ "findElements AppiumBy.androidPredicateString: text == 'a'": {
+ "result": [
+ "element:el1",
+ "element:el2"
+ ],
+ "requests": [
+ {
+ "method": "POST",
+ "uri": "/wd/hub/session/s1/elements",
+ "headers": {
+ "content-type": "application/json; charset=utf-8",
+ "user-agent": "appium/x.y.z (selenium/x.y.z (java os))"
+ },
+ "body": {
+ "using": "-android predicate string",
+ "value": "text == 'a'"
+ },
+ "client": "https://appium.example.com:4723"
+ }
+ ]
+ },
+ "findChildElement AppiumBy.androidPredicateString: text == 'a'": {
+ "result": "element:el2",
+ "requests": [
+ {
+ "method": "POST",
+ "uri": "/wd/hub/session/s1/element/el1/element",
+ "headers": {
+ "content-type": "application/json; charset=utf-8",
+ "user-agent": "appium/x.y.z (selenium/x.y.z (java os))"
+ },
+ "body": {
+ "id": "el1",
+ "using": "-android predicate string",
+ "value": "text == 'a'"
+ },
+ "client": "https://appium.example.com:4723"
+ }
+ ]
+ },
+ "findChildElements AppiumBy.androidPredicateString: text == 'a'": {
+ "result": [
+ "element:el2"
+ ],
+ "requests": [
+ {
+ "method": "POST",
+ "uri": "/wd/hub/session/s1/element/el1/elements",
+ "headers": {
+ "content-type": "application/json; charset=utf-8",
+ "user-agent": "appium/x.y.z (selenium/x.y.z (java os))"
+ },
+ "body": {
+ "id": "el1",
+ "using": "-android predicate string",
+ "value": "text == 'a'"
+ },
+ "client": "https://appium.example.com:4723"
+ }
+ ]
+ },
"findElement AppiumBy.iOSClassChain: **/XCUIElementTypeButton": {
"result": "element:el1",
"requests": [