Skip to content

Commit 5d76ed5

Browse files
#995 add android viewtag for espresso driver (#996)
* #995 add android viewtag for espresso driver * fixed review * fixed linter star imports
1 parent b37395b commit 5d76ed5

File tree

4 files changed

+130
-2
lines changed

4 files changed

+130
-2
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* See the NOTICE file distributed with this work for additional
5+
* information regarding copyright ownership.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.appium.java_client;
18+
19+
import org.openqa.selenium.NoSuchElementException;
20+
import org.openqa.selenium.WebDriverException;
21+
import org.openqa.selenium.WebElement;
22+
23+
import java.util.List;
24+
25+
public interface FindsByAndroidViewTag<T extends WebElement> extends FindsByFluentSelector<T> {
26+
/**
27+
* Method performs the searching for a single element by view tag selector
28+
* and value of the given selector.
29+
*
30+
* @param using an view tag selector
31+
* @return The first element that matches the given selector
32+
*
33+
* @throws WebDriverException This method is not applicable with browser/webview UI.
34+
* @throws NoSuchElementException when no one element is found
35+
*/
36+
default T findElementByAndroidViewTag(String using) {
37+
return findElement(MobileSelector.ANDROID_VIEWTAG.toString(), using);
38+
}
39+
40+
/**
41+
* Method performs the searching for a list of elements by view tag selector
42+
* and value of the given selector.
43+
*
44+
* @param using an view tag selector
45+
* @return a list of elements that match the given selector
46+
*
47+
* @throws WebDriverException This method is not applicable with browser/webview UI.
48+
*/
49+
default List<T> findElementsByAndroidViewTag(String using) {
50+
return findElements(MobileSelector.ANDROID_VIEWTAG.toString(), using);
51+
}
52+
}

src/main/java/io/appium/java_client/MobileBy.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ public static By windowsAutomation(final String windowsAutomation) {
121121
return new ByWindowsAutomation(windowsAutomation);
122122
}
123123

124+
/**
125+
* This locator strategy is available in Espresso Driver mode.
126+
* @since Appium 1.8.2 beta
127+
* @param tag is an view tag string
128+
* @return an instance of {@link ByAndroidViewTag}
129+
*/
130+
public static By AndroidViewTag(final String tag) {
131+
return new ByAndroidViewTag(tag);
132+
}
133+
124134
/**
125135
* This locator strategy is available only if OpenCV libraries and
126136
* NodeJS bindings are installed on the server machine.
@@ -561,6 +571,69 @@ protected ByImage(String b64Template) {
561571
return "By.Image: " + getLocatorString();
562572
}
563573
}
574+
575+
public static class ByAndroidViewTag extends MobileBy implements Serializable {
576+
577+
public ByAndroidViewTag(String tag) {
578+
super(MobileSelector.ANDROID_VIEWTAG, tag);
579+
}
580+
581+
/**
582+
* {@inheritDoc}
583+
*
584+
* @throws WebDriverException when current session doesn't support the given selector or when
585+
* value of the selector is not consistent.
586+
* @throws IllegalArgumentException when it is impossible to find something on the given
587+
* {@link SearchContext} instance
588+
*/
589+
@SuppressWarnings("unchecked")
590+
@Override
591+
public List<WebElement> findElements(SearchContext context) throws WebDriverException,
592+
IllegalArgumentException {
593+
Class<?> contextClass = context.getClass();
594+
595+
if (FindsByAndroidViewTag.class.isAssignableFrom(contextClass)) {
596+
return FindsByAndroidViewTag.class.cast(context)
597+
.findElementsByAndroidViewTag(getLocatorString());
598+
}
599+
600+
if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {
601+
return super.findElements(context);
602+
}
603+
604+
throw formIllegalArgumentException(contextClass, FindsByAndroidViewTag.class,
605+
FindsByFluentSelector.class);
606+
}
607+
608+
/**
609+
* {@inheritDoc}
610+
*
611+
* @throws WebDriverException when current session doesn't support the given selector or when
612+
* value of the selector is not consistent.
613+
* @throws IllegalArgumentException when it is impossible to find something on the given
614+
* {@link SearchContext} instance
615+
*/
616+
@Override public WebElement findElement(SearchContext context) throws WebDriverException,
617+
IllegalArgumentException {
618+
Class<?> contextClass = context.getClass();
619+
620+
if (FindsByAndroidViewTag.class.isAssignableFrom(contextClass)) {
621+
return FindsByAndroidViewTag.class.cast(context)
622+
.findElementByAndroidViewTag(getLocatorString());
623+
}
624+
625+
if (FindsByFluentSelector.class.isAssignableFrom(contextClass)) {
626+
return super.findElement(context);
627+
}
628+
629+
throw formIllegalArgumentException(contextClass, FindsByAndroidViewTag.class,
630+
FindsByFluentSelector.class);
631+
}
632+
633+
@Override public String toString() {
634+
return "By.AndroidViewTag: " + getLocatorString();
635+
}
636+
}
564637
}
565638

566639

src/main/java/io/appium/java_client/MobileSelector.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public enum MobileSelector {
2323
IOS_PREDICATE_STRING("-ios predicate string"),
2424
IOS_CLASS_CHAIN("-ios class chain"),
2525
WINDOWS_UI_AUTOMATION("-windows uiautomation"),
26-
IMAGE("-image");
26+
IMAGE("-image"),
27+
ANDROID_VIEWTAG("-android viewtag");
2728

2829
private final String selector;
2930

src/main/java/io/appium/java_client/android/AndroidDriver.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import io.appium.java_client.AppiumDriver;
2828
import io.appium.java_client.CommandExecutionHelper;
2929
import io.appium.java_client.FindsByAndroidUIAutomator;
30+
import io.appium.java_client.FindsByAndroidViewTag;
3031
import io.appium.java_client.HasOnScreenKeyboard;
3132
import io.appium.java_client.LocksDevice;
3233
import io.appium.java_client.android.connection.HasNetworkConnection;
@@ -61,7 +62,8 @@
6162
public class AndroidDriver<T extends WebElement>
6263
extends AppiumDriver<T>
6364
implements PressesKey, HasNetworkConnection, PushesFiles, StartsActivity,
64-
FindsByAndroidUIAutomator<T>, LocksDevice, HasAndroidSettings, HasAndroidDeviceDetails,
65+
FindsByAndroidUIAutomator<T>, FindsByAndroidViewTag<T>,
66+
LocksDevice, HasAndroidSettings, HasAndroidDeviceDetails,
6567
HasSupportedPerformanceDataType, AuthenticatesByFinger, HasOnScreenKeyboard,
6668
CanRecordScreen, SupportsSpecialEmulatorCommands,
6769
SupportsNetworkStateManagement, ListensToLogcatMessages, HasAndroidClipboard,

0 commit comments

Comments
 (0)