Skip to content

Commit 7f491e3

Browse files
authored
viewtag selector strategy (#189)
1 parent ccc67c5 commit 7f491e3

File tree

6 files changed

+21
-22
lines changed

6 files changed

+21
-22
lines changed

espresso-server/app/src/androidTest/java/io/appium/espressoserver/lib/handlers/GetAttribute.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ public String handle(AppiumParams params) throws AppiumException {
8484
return Integer.toString(viewElement.getIndex());
8585
case PACKAGE:
8686
return viewElement.getPackageName();
87+
case VIEW_TAG:
88+
return viewElement.getViewTag();
8789
default:
8890
throw new NotYetImplementedException();
8991
}

espresso-server/app/src/androidTest/java/io/appium/espressoserver/lib/helpers/ViewFinder.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,15 @@
4747
import static android.support.test.espresso.matcher.ViewMatchers.withClassName;
4848
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;
4949
import static android.support.test.espresso.matcher.ViewMatchers.withId;
50+
import static android.support.test.espresso.matcher.ViewMatchers.withTagValue;
5051
import static android.support.test.espresso.matcher.ViewMatchers.withText;
5152
import static io.appium.espressoserver.lib.viewmatcher.WithXPath.withXPath;
53+
import static org.hamcrest.CoreMatchers.allOf;
54+
import static org.hamcrest.CoreMatchers.equalTo;
5255
import static org.hamcrest.CoreMatchers.is;
5356
import static org.hamcrest.Matchers.endsWith;
5457
import static org.hamcrest.Matchers.hasEntry;
58+
import static org.hamcrest.Matchers.instanceOf;
5559

5660
/**
5761
* Helper methods to find elements based on locator strategies and selectors
@@ -141,7 +145,6 @@ private static List<View> findAllBy(@Nullable View root,
141145
Strategy strategy, String selector, boolean findOne)
142146
throws InvalidStrategyException, XPathLookupException {
143147
List<View> views;
144-
145148
switch (strategy) {
146149
case ID: // with ID
147150

@@ -179,6 +182,9 @@ private static List<View> findAllBy(@Nullable View root,
179182
views = getViews(root, withXPath(selector), false);
180183
}
181184
break;
185+
case VIEW_TAG:
186+
views = getViews(root, withTagValue(allOf(instanceOf(String.class), equalTo((Object) selector))), findOne);
187+
break;
182188
default:
183189
throw new InvalidStrategyException(String.format("Strategy is not implemented: %s", strategy.getStrategyName()));
184190
}

espresso-server/app/src/androidTest/java/io/appium/espressoserver/lib/model/SourceDocument.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,15 @@ private void buildXML(Document doc, Element parentElement, View view) {
119119
setAttribute(element, ViewAttributesEnum.PASSWORD, Boolean.toString(viewElement.isPassword()));
120120
setAttribute(element, ViewAttributesEnum.SELECTED, Boolean.toString(viewElement.isSelected()));
121121
setAttribute(element, ViewAttributesEnum.BOUNDS, viewElement.getBounds().toShortString());
122+
122123
final ViewText viewText = viewElement.getText();
123124
if (viewText != null) {
124125
setAttribute(element, ViewAttributesEnum.TEXT, viewText.getRawText());
125126
}
126127
setAttribute(element, ViewAttributesEnum.RESOURCE_ID, viewElement.getResourceId());
127128

128129
if (view.getTag() != null) {
129-
setAttribute(element, ViewAttributesEnum.VIEW_TAG, view.getTag().toString());
130+
setAttribute(element, ViewAttributesEnum.VIEW_TAG, viewElement.getViewTag());
130131
}
131132

132133
// If this is the rootElement, append it to the document

espresso-server/app/src/androidTest/java/io/appium/espressoserver/lib/model/Strategy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public enum Strategy {
4343
ACCESSIBILITY_ID("accessibility id"),
4444
@SerializedName("text")
4545
TEXT("text"),
46-
47-
ANDROID_UIAUTOMATOR("-android uiautomator");
46+
@SerializedName("-android viewtag")
47+
VIEW_TAG("-android viewtag");
4848

4949
private final String strategyName;
5050

espresso-server/app/src/androidTest/java/io/appium/espressoserver/lib/model/ViewAttributesEnum.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public enum ViewAttributesEnum {
3838
INSTANCE,
3939
INDEX,
4040
ADAPTERS,
41-
ADAPTER_TYPE, VIEW_TAG;
41+
ADAPTER_TYPE,
42+
VIEW_TAG;
4243

4344

4445
@Override

espresso-server/app/src/androidTest/java/io/appium/espressoserver/lib/model/ViewElement.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,6 @@ public class ViewElement {
4343
public ViewElement(View view) {
4444
this.view = view;
4545

46-
// TODO: Attributes that need to be added with examples
47-
// resource-id android:id/decor_content_parent
48-
// index 0
49-
// package io.appium.android.apis
50-
// content-desc
51-
// checkable false
52-
// checked false
53-
// enabled true
54-
// focusable false
55-
// scrollable false
56-
// password false
57-
// selected false
58-
// bounds [0,0][1440,2560]
59-
// resource-id android:id/decor_content_parent
60-
// instance 0
61-
// visibility
62-
6346
}
6447

6548
public synchronized Activity extractActivity() {
@@ -248,4 +231,10 @@ public int getRelativeTop() {
248231
public String getPackageName() {
249232
return InstrumentationRegistry.getTargetContext().getPackageName();
250233
}
234+
235+
@Nullable
236+
public String getViewTag() {
237+
Object tag = view.getTag();
238+
return tag == null ? null : tag.toString();
239+
}
251240
}

0 commit comments

Comments
 (0)