Skip to content

Commit

Permalink
Release v1.1.2 & Support index search in child Selector (#53)
Browse files Browse the repository at this point in the history
* Support index search in child Selector

* Update index modifier to final
  • Loading branch information
ko1in1u authored Aug 12, 2024
1 parent 6a52776 commit e6bde32
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## 1.1.2:
* Support ancestor search in Selector
* Remove ineffectiveness Configurator settings
* Support gesture margin for default Scroll
* Support index search in child Selector

## 1.1.1:
* Migrate -jre flavor of Guava to -android flavor
Expand Down
12 changes: 11 additions & 1 deletion docs/selector.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,22 @@ itself.

* Child

Find the child directly under present Selector.
Find the child under present Selector.

```python
ad.ui(...).child(...)
```

> [!TIP]
> The child selector now supports index search, and it's recommended to pair
> it with depth search to pinpoint the specific child element you're looking
> for.

```python
# Find the second object directly under present Selector.
ad.ui(...).child(depth=1, index=2)
```

* Parent

Find the parent of present Selector, or null if it has no parent.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ maintainers = [{ name = "Kolin Lu", email = "kolinlu@google.com" }]
license = { file = "LICENSE" }
readme = "README.md"
requires-python = ">=3.7"
version = "1.1.1"
version = "1.1.2"
dependencies = [
'mobly>=1.12.2',
]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

setuptools.setup(
name='snippet-uiautomator',
version='1.1.1',
version='1.1.2',
author='Kolin Lu',
author_email='kolinlu@google.com',
description='A Python wrapper for UiAutomator based on Mobly Snippet Lib.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -71,7 +72,8 @@ private boolean isCriteriaMatch(UiObject2 uiObject2, BySelector bySelector) {
return null;
}

private @Nullable BySelector getBySelector(JSONObject selector) throws SelectorException {
private @Nullable BySelector getBySelector(JSONObject selector, String type)
throws SelectorException {
Log.d(String.format("Receive selector: %s", selector));
BySelector bySelector = null;
Iterator<String> keys = selector.keys();
Expand All @@ -80,6 +82,13 @@ private boolean isCriteriaMatch(UiObject2 uiObject2, BySelector bySelector) {
if (SUB_SELECTORS.contains(key)) {
continue;
}
if (key.equals("index")) {
if (type.equals("child")) {
continue;
} else {
throw new SelectorException("Index search is only supported in child selector");
}
}
try {
bySelector =
Objects.requireNonNull(bySelectorMap.get(key)).select(bySelector, selector.get(key));
Expand All @@ -96,7 +105,7 @@ private boolean isCriteriaMatch(UiObject2 uiObject2, BySelector bySelector) {
private @Nullable UiObject2 getUiObject2(
JSONObject selector, @Nullable UiObject2 baseUiObject2, String type)
throws SelectorException {
BySelector bySelector = getBySelector(selector);
BySelector bySelector = getBySelector(selector, type);
if (bySelector == null && type.equals("self")) {
return null;
}
Expand All @@ -108,6 +117,19 @@ private boolean isCriteriaMatch(UiObject2 uiObject2, BySelector bySelector) {
break;
case "child":
uiObject2List = baseUiObject2.findObjects(bySelector);
if (selector.has("index")) {
try {
final int index = selector.getInt("index");
if (index >= 0 && index < uiObject2List.size()) {
uiObject2List = Collections.singletonList(uiObject2List.get(index));
} else {
return null;
}
} catch (JSONException e) {
throw new SelectorException(
String.format("Fail to get index value from Selector<%s>", selector), e);
}
}
break;
case "parent":
uiObject2List.add(baseUiObject2.getParent());
Expand Down Expand Up @@ -178,11 +200,11 @@ private boolean isCriteriaMatch(UiObject2 uiObject2, BySelector bySelector) {
}

public @Nullable BySelector toBySelector() throws SelectorException {
return getBySelector(selector);
return getBySelector(selector, "self");
}

public @Nullable UiObject2 toUiObject2() throws SelectorException {
BySelector bySelector = getBySelector(selector);
BySelector bySelector = getBySelector(selector, "self");
if (bySelector == null) {
return null;
}
Expand Down
Binary file modified snippet_uiautomator/android/app/uiautomator.apk
Binary file not shown.

0 comments on commit e6bde32

Please sign in to comment.