Skip to content

Commit ad44fa9

Browse files
Merge pull request #317 from TikhomirovSergey/SrinivasanTarget-locatorfixes
#311 fix
2 parents 8f88f67 + 5ed8b9e commit ad44fa9

File tree

8 files changed

+161
-121
lines changed

8 files changed

+161
-121
lines changed

src/main/java/io/appium/java_client/pagefactory/AndroidFindBy.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@
3535
String uiAutomator() default "";
3636
String accessibility() default "";
3737
String id() default "";
38-
String name() default "";
38+
@Deprecated
39+
/**
40+
* By.name selector is not supported by Appium server node since 1.5.x.
41+
* So this option is going to be removed further. Be careful.
42+
*/String name() default "";
3943
String className() default "";
4044
String tagName() default "";
4145
String xpath() default "";

src/main/java/io/appium/java_client/pagefactory/DefaultElementByBuilder.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616

1717
package io.appium.java_client.pagefactory;
1818

19-
import static io.appium.java_client.remote.MobilePlatform.*;
20-
import static io.appium.java_client.remote.AutomationName.*;
21-
2219
import java.lang.annotation.Annotation;
2320
import java.lang.reflect.*;
2421
import java.util.HashMap;
@@ -117,8 +114,7 @@ protected By buildDefaultBy() {
117114
@Override
118115
protected By buildMobileNativeBy() {
119116
AnnotatedElement annotatedElement = annotatedElementContainer.getAnnotated();
120-
if (ANDROID.toUpperCase().equals(platform)
121-
&& SELENDROID.toUpperCase().equals(automation)) {
117+
if (isSelendroidAutomation()) {
122118
SelendroidFindBy selendroidFindBy = annotatedElement.getAnnotation(SelendroidFindBy.class);
123119
SelendroidFindBys selendroidFindBys = annotatedElement.getAnnotation(SelendroidFindBys.class);
124120
SelendroidFindAll selendroidFindByAll = annotatedElement.getAnnotation(SelendroidFindAll.class);
@@ -136,7 +132,7 @@ protected By buildMobileNativeBy() {
136132
}
137133
}
138134

139-
if (ANDROID.toUpperCase().equals(platform)) {
135+
if (isAndroid()) {
140136
AndroidFindBy androidFindBy = annotatedElement.getAnnotation(AndroidFindBy.class);
141137
AndroidFindBys androidFindBys= annotatedElement.getAnnotation(AndroidFindBys.class);
142138
AndroidFindAll androidFindAll = annotatedElement.getAnnotation(AndroidFindAll.class);
@@ -154,7 +150,7 @@ protected By buildMobileNativeBy() {
154150
}
155151
}
156152

157-
if (IOS.toUpperCase().equals(platform)) {
153+
if (isIOS()) {
158154
iOSFindBy iOSFindBy = annotatedElement.getAnnotation(iOSFindBy.class);
159155
iOSFindBys iOSFindBys= annotatedElement.getAnnotation(iOSFindBys.class);
160156
iOSFindAll iOSFindAll = annotatedElement.getAnnotation(iOSFindAll.class);
@@ -181,25 +177,38 @@ public boolean isLookupCached() {
181177
return (annotatedElement.getAnnotation(CacheLookup.class) != null);
182178
}
183179

180+
private By returnMappedBy(By byDefault, By nativeAppBy) {
181+
Map<ContentType, By> contentMap = new HashMap<>();
182+
contentMap.put(ContentType.HTML_OR_DEFAULT, byDefault);
183+
contentMap.put(ContentType.NATIVE_MOBILE_SPECIFIC, nativeAppBy);
184+
return new ContentMappedBy(contentMap);
185+
}
186+
184187
@Override
185188
public By buildBy() {
186189
assertValidAnnotations();
187190

188191
By defaultBy = buildDefaultBy();
189192
By mobileNativeBy = buildMobileNativeBy();
190193

191-
if (defaultBy == null) {
194+
String idOrName = ((Field) annotatedElementContainer.getAnnotated()).getName();
195+
196+
if (defaultBy == null && mobileNativeBy == null) {
192197
defaultBy = new ByIdOrName(((Field) annotatedElementContainer.getAnnotated()).getName());
198+
mobileNativeBy = new By.ById(idOrName);
199+
return returnMappedBy(defaultBy, mobileNativeBy);
193200
}
194201

202+
if (defaultBy == null) {
203+
defaultBy = new ByIdOrName(((Field) annotatedElementContainer.getAnnotated()).getName());
204+
return returnMappedBy(defaultBy, mobileNativeBy);
205+
}
195206

196207
if (mobileNativeBy == null) {
197208
mobileNativeBy = defaultBy;
209+
return returnMappedBy(defaultBy, mobileNativeBy);
198210
}
199211

200-
Map<ContentType, By> contentMap = new HashMap<>();
201-
contentMap.put(ContentType.HTML_OR_DEFAULT, defaultBy);
202-
contentMap.put(ContentType.NATIVE_MOBILE_SPECIFIC, mobileNativeBy);
203-
return new ContentMappedBy(contentMap);
212+
return returnMappedBy(defaultBy, mobileNativeBy);
204213
}
205214
}

src/main/java/io/appium/java_client/pagefactory/ThrowableUtil.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.appium.java_client.pagefactory;
1818

19+
import org.openqa.selenium.InvalidSelectorException;
1920
import org.openqa.selenium.StaleElementReferenceException;
2021

2122
import java.lang.reflect.InvocationTargetException;
@@ -28,7 +29,11 @@ static boolean isInvalidSelectorRootCause(Throwable e) {
2829
return false;
2930
}
3031

31-
if (String.valueOf(e.getMessage()).contains(INVALID_SELECTOR_PATTERN)) {
32+
if (InvalidSelectorException.class.isAssignableFrom(e.getClass())) {
33+
return true;
34+
}
35+
36+
if (String.valueOf(e.getMessage()).contains(INVALID_SELECTOR_PATTERN) || String.valueOf(e.getMessage()).contains("Locator Strategy \\w+ is not supported")) {
3237
return true;
3338
}
3439

src/main/java/io/appium/java_client/pagefactory/bys/builder/AppiumByBuilder.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
import java.util.ArrayList;
2525
import java.util.List;
2626

27+
import static io.appium.java_client.remote.AutomationName.SELENDROID;
28+
import static io.appium.java_client.remote.MobilePlatform.ANDROID;
29+
import static io.appium.java_client.remote.MobilePlatform.IOS;
30+
2731
/**
2832
* It is the basic handler of Appium-specific page object annotations
2933
* About the Page Object design pattern please read these documents:
@@ -163,6 +167,18 @@ public void setAnnotated(AnnotatedElement annotated) {
163167
this.annotatedElementContainer.setAnnotated(annotated);
164168
}
165169

170+
protected boolean isAndroid() {
171+
return ANDROID.toUpperCase().equals(platform);
172+
}
173+
174+
protected boolean isSelendroidAutomation() {
175+
return isAndroid() && SELENDROID.toUpperCase().equals(automation);
176+
}
177+
178+
protected boolean isIOS() {
179+
return IOS.toUpperCase().equals(platform);
180+
}
181+
166182
/**
167183
* Defines how to transform given object (field, class, etc)
168184
* into {@link org.openqa.selenium.By} class used by webdriver to locate elements.

src/main/java/io/appium/java_client/pagefactory/iOSFindBy.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@
3535
String uiAutomator() default "";
3636
String accessibility() default "";
3737
String id() default "";
38-
String name() default "";
38+
@Deprecated
39+
/**
40+
* By.name selector is not supported by Appium server node since 1.5.x.
41+
* So this option is going to be removed further. Be careful.
42+
*/String name() default "";
3943
String className() default "";
4044
String tagName() default "";
4145
String xpath() default "";

src/test/java/io/appium/java_client/pagefactory_tests/MobileBrowserCompatibilityTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,18 @@
4141
public class MobileBrowserCompatibilityTest {
4242

4343
private WebDriver driver;
44-
45-
@FindBy(name = "q")
46-
@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/someId\")")
47-
private WebElement searchTextField;
44+
4845
private AppiumDriverLocalService service;
4946

5047
@AndroidFindBys({
5148
@AndroidFindBy(className = "someClass"),
5249
@AndroidFindBy(xpath = "//someTag")})
53-
@FindBy(name="btnG")
54-
private RemoteWebElement searchButton;
55-
50+
private RemoteWebElement btnG; //this element should be found by id = 'btnG' or name = 'btnG'
51+
52+
@FindBy(name = "q")
53+
@AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"android:id/someId\")")
54+
private WebElement searchTextField;
55+
5656
@AndroidFindBy(className = "someClass")
5757
@FindBys({@FindBy(className = "r"), @FindBy(tagName = "a")})
5858
private List<WebElement> foundLinks;
@@ -84,7 +84,7 @@ public void test() {
8484
driver.get("https://www.google.com");
8585

8686
searchTextField.sendKeys("Hello");
87-
searchButton.click();
87+
btnG.click();
8888
Assert.assertNotEquals(0, foundLinks.size());
8989
}
9090

src/test/java/io/appium/java_client/pagefactory_tests/SelendroidModeTest.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package io.appium.java_client.pagefactory_tests;
1818

1919
import io.appium.java_client.android.AndroidDriver;
20+
import io.appium.java_client.android.AndroidElement;
2021
import io.appium.java_client.pagefactory.*;
2122
import io.appium.java_client.remote.AutomationName;
2223
import io.appium.java_client.remote.MobileCapabilityType;
@@ -37,7 +38,7 @@
3738
import static org.junit.Assert.*;
3839

3940
public class SelendroidModeTest {
40-
private static int SELENDROID_PORT = 9999;
41+
private static int SELENDROID_PORT = 9999;
4142

4243
private static WebDriver driver;
4344
private static AppiumDriverLocalService service;
@@ -63,30 +64,30 @@ public class SelendroidModeTest {
6364
private WebElement textXpath;
6465

6566
@SelendroidFindBys({
66-
@SelendroidFindBy(id = "text1")})
67+
@SelendroidFindBy(id = "text1")})
6768
private WebElement textIds;
6869

6970
@SelendroidFindAll({
70-
@SelendroidFindBy(id = "text1")})
71+
@SelendroidFindBy(id = "text1")})
7172
private WebElement textAll;
7273

7374
@SelendroidFindAll({
74-
@SelendroidFindBy(id = "text1")})
75+
@SelendroidFindBy(id = "text1")})
7576
private List<WebElement> textsAll;
7677

7778
@SelendroidFindBy(className = "android.widget.TextView")
7879
private WebElement textClass;
7980

8081
@SelendroidFindBy(tagName = "TextView")
8182
private WebElement textTag;
82-
83+
8384
@SelendroidFindBy(linkText = "Accessibility")
8485
private WebElement textLink;
85-
86+
8687
@SelendroidFindBy(partialLinkText = "ccessibilit")
8788
private WebElement textPartialLink;
8889

89-
@BeforeClass
90+
@BeforeClass
9091
public static void beforeClass() throws Exception {
9192
AppiumServiceBuilder builder = new AppiumServiceBuilder().withArgument(GeneralServerFlag.AUTOMATION_NAME, AutomationName.SELENDROID);
9293
service = builder.build();
@@ -123,8 +124,8 @@ public static void afterClass() throws Exception {
123124
public void findByIdElementTest() {
124125
assertNotEquals(null, textId.getAttribute("text"));
125126
}
126-
127-
@Test
127+
128+
@Test
128129
public void findBySelendroidSelectorTest() {
129130
assertNotEquals(null, textSelendroidId.getAttribute("text"));
130131
}
@@ -173,15 +174,15 @@ public void findByElementByCalssTest() {
173174
public void findByElementByTagTest() {
174175
assertNotEquals(null, textTag.getAttribute("text"));
175176
}
176-
177+
177178
@Test
178179
public void findBySelendroidAnnotationOnlyTest() {
179180
assertNotEquals(null, textSelendroidId.getAttribute("text"));
180181
}
181-
182+
182183
@Test
183184
public void findBySelendroidLinkTextTest() {
184185
assertEquals("Accessibility", textLink.getText());
185186

186187
}
187-
}
188+
}

0 commit comments

Comments
 (0)