Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion docs/Advanced-By.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
67 changes: 66 additions & 1 deletion src/main/java/io/appium/java_client/AppiumBy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
* <a href="https://appium.github.io/appium-uiautomator2-driver/latest/guides/migrating-from-uiscrollable/">
* Migrating from UiScrollable and UiSelector</a>.
*/
@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 <a href="https://appium.github.io/appium-uiautomator2-driver/latest/guides/android-class-chain/">
* the documentation</a> 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 <a href="https://appium.github.io/appium-uiautomator2-driver/latest/guides/android-class-chain/#predicate-string-strategy">
* the documentation</a> 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.
*
Expand Down Expand Up @@ -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.
Expand All @@ -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).
*/
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/io/appium/java_client/pagefactory/AndroidBy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Loading
Loading