Skip to content

Commit 67b9216

Browse files
Defect fixes & code style improvements
1 parent 50ba57d commit 67b9216

File tree

8 files changed

+66
-74
lines changed

8 files changed

+66
-74
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ public void zoom(int x, int y) {
335335
@Override public String getContext() {
336336
String contextName =
337337
String.valueOf(execute(DriverCommand.GET_CURRENT_CONTEXT_HANDLE).getValue());
338-
if ("null".equalsIgnoreCase(String.valueOf(contextName))) {
338+
if ("null".equalsIgnoreCase(contextName)) {
339339
return null;
340340
}
341341
return contextName;

src/main/java/io/appium/java_client/internal/ElementMap.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.appium.java_client.remote.MobilePlatform;
2626
import io.appium.java_client.windows.WindowsElement;
2727
import io.appium.java_client.youiengine.YouiEngineElement;
28+
import org.openqa.selenium.remote.RemoteWebElement;
2829

2930
import java.util.Map;
3031
import java.util.Optional;
@@ -36,7 +37,7 @@ public enum ElementMap {
3637
IOS_XCUI_TEST(AutomationName.IOS_XCUI_TEST.toLowerCase(), IOSElement.class),
3738
ANDROID_UI_AUTOMATOR(MobilePlatform.ANDROID.toLowerCase(), AndroidElement.class),
3839
IOS_UI_AUTOMATION(MobilePlatform.IOS.toLowerCase(), IOSElement.class),
39-
WINDOwS(MobilePlatform.WINDOWS, WindowsElement.class);
40+
WINDOWS(MobilePlatform.WINDOWS, WindowsElement.class);
4041

4142

4243
private static final Map<String, ElementMap> mobileElementMap;
@@ -52,7 +53,7 @@ public enum ElementMap {
5253

5354

5455
private final String platformOrAutomation;
55-
private final Class<? extends MobileElement> elementClass;
56+
private final Class<? extends RemoteWebElement> elementClass;
5657

5758
private ElementMap(String platformOrAutomation, Class<? extends MobileElement> elementClass) {
5859
this.platformOrAutomation = platformOrAutomation;
@@ -63,7 +64,7 @@ public String getPlatformOrAutomation() {
6364
return platformOrAutomation;
6465
}
6566

66-
public Class<? extends MobileElement> getElementClass() {
67+
public Class<? extends RemoteWebElement> getElementClass() {
6768
return elementClass;
6869
}
6970

@@ -72,11 +73,12 @@ public Class<? extends MobileElement> getElementClass() {
7273
* @param automation automation name.
7374
* @return subclass of {@link io.appium.java_client.MobileElement} that convenient to current session details.
7475
*/
75-
public static Class<? extends MobileElement> getElementClass(String platform, String automation) {
76-
ElementMap element = Optional.ofNullable(mobileElementMap.get(automation))
77-
.orElse(mobileElementMap.get(platform));
76+
public static Class<? extends RemoteWebElement> getElementClass(String platform, String automation) {
77+
ElementMap element = Optional.ofNullable(mobileElementMap.get(String
78+
.valueOf(automation).toLowerCase().trim()))
79+
.orElse(mobileElementMap.get(String.valueOf(platform).toLowerCase().trim()));
7880
if (element == null) {
79-
return null;
81+
return RemoteWebElement.class;
8082
}
8183
return element.getElementClass();
8284
}

src/main/java/io/appium/java_client/internal/JsonToMobileElementConverter.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
2222
import com.google.common.collect.Lists;
2323
import com.google.common.collect.Maps;
2424

25-
import io.appium.java_client.MobileElement;
26-
2725
import org.openqa.selenium.WebDriverException;
2826
import org.openqa.selenium.remote.RemoteWebDriver;
27+
import org.openqa.selenium.remote.RemoteWebElement;
2928
import org.openqa.selenium.remote.internal.JsonToWebElementConverter;
3029

3130
import java.lang.reflect.Constructor;
@@ -74,7 +73,7 @@ public Object apply(Object result) {
7473
if (result instanceof Map<?, ?>) {
7574
Map<?, ?> resultAsMap = (Map<?, ?>) result;
7675
if (resultAsMap.containsKey("ELEMENT")) {
77-
MobileElement element = newMobileElement();
76+
RemoteWebElement element = newMobileElement();
7877
element.setId(String.valueOf(resultAsMap.get("ELEMENT")));
7978
element.setFileDetector(driver.getFileDetector());
8079
return element;
@@ -93,19 +92,13 @@ public Object apply(Object result) {
9392
return result;
9493
}
9594

96-
protected MobileElement newMobileElement() {
97-
Class<? extends MobileElement> target =
95+
protected RemoteWebElement newMobileElement() {
96+
Class<? extends RemoteWebElement> target =
9897
getElementClass(platform, automation);
99-
100-
if (target == null) {
101-
throw new WebDriverException(new ClassNotFoundException("The class of mobile element is "
102-
+ "unknown for current session"));
103-
}
104-
10598
try {
106-
Constructor<? extends MobileElement> constructor = target.getDeclaredConstructor();
99+
Constructor<? extends RemoteWebElement> constructor = target.getDeclaredConstructor();
107100
constructor.setAccessible(true);
108-
MobileElement result = constructor.newInstance();
101+
RemoteWebElement result = constructor.newInstance();
109102
result.setParent(driver);
110103
return result;
111104
} catch (Exception e) {

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

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import java.util.ArrayList;
4545
import java.util.List;
4646
import java.util.Map;
47-
import java.util.Optional;
4847
import java.util.concurrent.TimeUnit;
4948

5049
/**
@@ -93,13 +92,7 @@ private static String extractSessionData(WebDriver driver, String parameter) {
9392
return null;
9493
}
9594

96-
Object parameterValue = HasSessionDetails.class.cast(driver).getSessionDetail(parameter);
97-
98-
if (parameterValue == null) {
99-
return null;
100-
}
101-
102-
return String.valueOf(parameterValue).toLowerCase();
95+
return String.valueOf(HasSessionDetails.class.cast(driver).getSessionDetail(parameter));
10396
}
10497

10598
public AppiumFieldDecorator(SearchContext context, long implicitlyWaitTimeOut,
@@ -150,10 +143,9 @@ protected List<WebElement> proxyForListLocator(ClassLoader ignored,
150143
Type listType = ((ParameterizedType) genericType).getActualTypeArguments()[0];
151144

152145
for (Class<? extends WebElement> webElementClass : availableElementClasses) {
153-
if (!webElementClass.equals(listType)) {
154-
continue;
146+
if (webElementClass.equals(listType)) {
147+
return true;
155148
}
156-
return true;
157149
}
158150
return false;
159151
}
@@ -235,14 +227,8 @@ private Object decorateWidget(Field field) {
235227
new WidgetInterceptor(locator, originalDriver, null, map, timeOutDuration));
236228
}
237229

238-
private Class<?> getTypeForProxy() {
239-
Optional<Class<?>> optionalClass =
240-
Optional.ofNullable(getElementClass(platform, automation));
241-
return optionalClass.orElse(RemoteWebElement.class);
242-
}
243-
244230
private WebElement proxyForAnElement(ElementLocator locator) {
245231
ElementInterceptor elementInterceptor = new ElementInterceptor(locator, originalDriver);
246-
return (WebElement) getEnhancedProxy(getTypeForProxy(), elementInterceptor);
232+
return getEnhancedProxy(getElementClass(platform, automation), elementInterceptor);
247233
}
248234
}

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

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,16 @@ private static void checkDisallowedAnnotationPairs(Annotation a1, Annotation a2)
5151
}
5252

5353
private static By buildMobileBy(LocatorGroupStrategy locatorGroupStrategy, Annotation[] annotations) {
54-
if (annotations != null && annotations.length > 0) {
55-
if (annotations.length == 1) {
56-
return createBy(new Annotation[] {annotations[0]}, HowToUseSelectors.USE_ONE);
57-
} else {
58-
LocatorGroupStrategy strategy = Optional.ofNullable(locatorGroupStrategy)
59-
.orElse(LocatorGroupStrategy.CHAIN);
60-
if (strategy.equals(LocatorGroupStrategy.ALL_POSSIBLE)) {
61-
return createBy(annotations, HowToUseSelectors.USE_ANY);
62-
}
63-
return createBy(annotations, HowToUseSelectors.BUILD_CHAINED);
54+
if (annotations.length == 1) {
55+
return createBy(new Annotation[] {annotations[0]}, HowToUseSelectors.USE_ONE);
56+
} else {
57+
LocatorGroupStrategy strategy = Optional.ofNullable(locatorGroupStrategy)
58+
.orElse(LocatorGroupStrategy.CHAIN);
59+
if (strategy.equals(LocatorGroupStrategy.ALL_POSSIBLE)) {
60+
return createBy(annotations, HowToUseSelectors.USE_ANY);
6461
}
62+
return createBy(annotations, HowToUseSelectors.BUILD_CHAINED);
6563
}
66-
return null;
6764
}
6865

6966
@Override protected void assertValidAnnotations() {
@@ -125,7 +122,6 @@ private static By buildMobileBy(LocatorGroupStrategy locatorGroupStrategy, Annot
125122
@Override protected By buildMobileNativeBy() {
126123
AnnotatedElement annotatedElement = annotatedElementContainer.getAnnotated();
127124
HowToUseLocators howToUseLocators = annotatedElement.getAnnotation(HowToUseLocators.class);
128-
By result = null;
129125

130126
if (isSelendroidAutomation()) {
131127
SelendroidFindBy[] selendroidFindByArray =
@@ -148,8 +144,11 @@ private static By buildMobileBy(LocatorGroupStrategy locatorGroupStrategy, Annot
148144
return createBy(selendroidFindByAll.value(), HowToUseSelectors.USE_ANY);
149145
}
150146
///////////////////////////////////////
151-
result = buildMobileBy(howToUseLocators != null ? howToUseLocators.selendroidAutomation() : null,
152-
selendroidFindByArray);
147+
//code that supposed to be supported
148+
if (selendroidFindByArray != null && selendroidFindByArray.length > 0) {
149+
return buildMobileBy(howToUseLocators != null ? howToUseLocators.selendroidAutomation() : null,
150+
selendroidFindByArray);
151+
}
153152
}
154153

155154
if (isAndroid()) {
@@ -170,8 +169,11 @@ private static By buildMobileBy(LocatorGroupStrategy locatorGroupStrategy, Annot
170169
return createBy(androidFindAll.value(), HowToUseSelectors.USE_ANY);
171170
}
172171
///////////////////////////////////////
173-
result = buildMobileBy(howToUseLocators != null ? howToUseLocators.androidAutomation() : null,
174-
androidFindByArray);
172+
//code that supposed to be supported
173+
if (androidFindByArray != null && androidFindByArray.length > 0) {
174+
return buildMobileBy(howToUseLocators != null ? howToUseLocators.androidAutomation() : null,
175+
androidFindByArray);
176+
}
175177
}
176178

177179
if (isIOS()) {
@@ -181,28 +183,33 @@ private static By buildMobileBy(LocatorGroupStrategy locatorGroupStrategy, Annot
181183
iOSFindAll iOSFindAll = annotatedElement.getAnnotation(iOSFindAll.class);
182184

183185
if (iOSFindByArray != null && iOSFindByArray.length == 1) {
184-
return createBy(new Annotation[] {iOSFindByArray[0]}, HowToUseSelectors.USE_ONE);
186+
return createBy(new Annotation[] {iOSFindByArray[0]}, HowToUseSelectors.USE_ONE);
185187
}
186188

187189
if (iOSFindBys != null) {
188-
return createBy(iOSFindBys.value(), HowToUseSelectors.BUILD_CHAINED);
190+
return createBy(iOSFindBys.value(), HowToUseSelectors.BUILD_CHAINED);
189191
}
190192

191193
if (iOSFindAll != null) {
192194
return createBy(iOSFindAll.value(), HowToUseSelectors.USE_ANY);
193195
}
194196
///////////////////////////////////////
195-
result = buildMobileBy(howToUseLocators != null ? howToUseLocators.iOSAutomation() : null,
196-
iOSFindByArray);
197+
//code that supposed to be supported
198+
if (iOSFindByArray != null && iOSFindByArray.length > 0) {
199+
return buildMobileBy(howToUseLocators != null ? howToUseLocators.iOSAutomation() : null,
200+
iOSFindByArray);
201+
}
197202
}
198203

199204
if (isWindows()) {
200205
WindowsFindBy[] windowsFindByArray = annotatedElement.getAnnotationsByType(WindowsFindBy.class);
201-
result = buildMobileBy(howToUseLocators != null ? howToUseLocators.windowsAutomation() : null,
202-
windowsFindByArray);
206+
if (windowsFindByArray != null && windowsFindByArray.length > 0) {
207+
return buildMobileBy(howToUseLocators != null ? howToUseLocators.windowsAutomation() : null,
208+
windowsFindByArray);
209+
}
203210
}
204211

205-
return result;
212+
return null;
206213
}
207214

208215
@Override public boolean isLookupCached() {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,13 @@ private By getByFromDeclaredClass(WhatIsNeeded whatIsNeeded) {
9292
}
9393

9494
@Override protected By buildDefaultBy() {
95-
return Optional.ofNullable(super.buildDefaultBy()).orElse(getByFromDeclaredClass(WhatIsNeeded.DEFAULT_OR_HTML));
95+
return Optional.ofNullable(super.buildDefaultBy())
96+
.orElse(getByFromDeclaredClass(WhatIsNeeded.DEFAULT_OR_HTML));
9697
}
9798

9899
@Override protected By buildMobileNativeBy() {
99-
return Optional.ofNullable(super.buildMobileNativeBy()).orElse(getByFromDeclaredClass(WhatIsNeeded.MOBILE_NATIVE));
100+
return Optional.ofNullable(super.buildMobileNativeBy())
101+
.orElse(getByFromDeclaredClass(WhatIsNeeded.MOBILE_NATIVE));
100102
}
101103

102104
private enum WhatIsNeeded {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public abstract class AppiumByBuilder extends AbstractAnnotations {
6161

6262
protected AppiumByBuilder(String platform, String automation) {
6363
this.annotatedElementContainer = new AnnotatedElementContainer();
64-
this.platform = String.valueOf(platform).toUpperCase().trim();
65-
this.automation = String.valueOf(automation).toUpperCase().trim();
64+
this.platform = String.valueOf(platform);
65+
this.automation = String.valueOf(automation);
6666
}
6767

6868
private static List<String> getMethodNames(Method[] methods) {
@@ -169,19 +169,19 @@ public void setAnnotated(AnnotatedElement annotated) {
169169
}
170170

171171
protected boolean isAndroid() {
172-
return ANDROID.toUpperCase().equals(platform);
172+
return ANDROID.equalsIgnoreCase(platform);
173173
}
174174

175175
protected boolean isSelendroidAutomation() {
176-
return isAndroid() && SELENDROID.toUpperCase().equals(automation);
176+
return isAndroid() && SELENDROID.equalsIgnoreCase(automation);
177177
}
178178

179179
protected boolean isIOS() {
180-
return IOS.toUpperCase().equals(platform);
180+
return IOS.equalsIgnoreCase(platform);
181181
}
182182

183183
protected boolean isWindows() {
184-
return WINDOWS.toUpperCase().equals(platform);
184+
return WINDOWS.equalsIgnoreCase(platform);
185185
}
186186

187187
/**

src/main/java/io/appium/java_client/windows/WindowsDriver.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package io.appium.java_client.windows;
1818

19+
import static io.appium.java_client.remote.MobilePlatform.WINDOWS;
20+
1921
import io.appium.java_client.AppiumDriver;
2022
import io.appium.java_client.FindsByWindowsAutomation;
2123
import io.appium.java_client.HidesKeyboardWithKeyName;
@@ -29,8 +31,6 @@
2931

3032
import java.net.URL;
3133

32-
import static io.appium.java_client.remote.MobilePlatform.WINDOWS;
33-
3434
public class WindowsDriver<T extends WebElement>
3535
extends AppiumDriver<T> implements PressesKeyCode, HidesKeyboardWithKeyName,
3636
FindsByWindowsAutomation<T> {
@@ -51,15 +51,17 @@ public WindowsDriver(AppiumDriverLocalService service, Capabilities desiredCapab
5151
super(service, substituteMobilePlatform(desiredCapabilities, WINDOWS));
5252
}
5353

54-
public WindowsDriver(AppiumDriverLocalService service, HttpClient.Factory httpClientFactory, Capabilities desiredCapabilities) {
54+
public WindowsDriver(AppiumDriverLocalService service, HttpClient.Factory httpClientFactory,
55+
Capabilities desiredCapabilities) {
5556
super(service, httpClientFactory, substituteMobilePlatform(desiredCapabilities, WINDOWS));
5657
}
5758

5859
public WindowsDriver(AppiumServiceBuilder builder, Capabilities desiredCapabilities) {
5960
super(builder, substituteMobilePlatform(desiredCapabilities, WINDOWS));
6061
}
6162

62-
public WindowsDriver(AppiumServiceBuilder builder, HttpClient.Factory httpClientFactory, Capabilities desiredCapabilities) {
63+
public WindowsDriver(AppiumServiceBuilder builder, HttpClient.Factory httpClientFactory,
64+
Capabilities desiredCapabilities) {
6365
super(builder, httpClientFactory, substituteMobilePlatform(desiredCapabilities, WINDOWS));
6466
}
6567

0 commit comments

Comments
 (0)