From 5fa74aec3feea3218688eb86c80e288040147de8 Mon Sep 17 00:00:00 2001 From: Sergey Tikhomirov Date: Sat, 23 Sep 2017 01:57:30 +0300 Subject: [PATCH 1/3] #732 FIX --- .../io/appium/java_client/AppiumDriver.java | 30 ++----------------- .../appium/java_client/HasSessionDetails.java | 27 +++++++++++++---- 2 files changed, 23 insertions(+), 34 deletions(-) diff --git a/src/main/java/io/appium/java_client/AppiumDriver.java b/src/main/java/io/appium/java_client/AppiumDriver.java index be4900a23..4f4616d3f 100644 --- a/src/main/java/io/appium/java_client/AppiumDriver.java +++ b/src/main/java/io/appium/java_client/AppiumDriver.java @@ -17,9 +17,7 @@ package io.appium.java_client; import static com.google.common.base.Preconditions.checkNotNull; -import static io.appium.java_client.remote.MobileCapabilityType.AUTOMATION_NAME; import static io.appium.java_client.remote.MobileCapabilityType.PLATFORM_NAME; -import static java.util.Optional.ofNullable; import com.google.common.collect.ImmutableMap; @@ -72,9 +70,6 @@ public class AppiumDriver private URL remoteAddress; private RemoteLocationContext locationContext; private ExecuteMethod executeMethod; - private final String platformName; - private final String automationName; - /** * @param executor is an instance of {@link org.openqa.selenium.remote.HttpCommandExecutor} @@ -89,20 +84,6 @@ public AppiumDriver(HttpCommandExecutor executor, Capabilities capabilities) { locationContext = new RemoteLocationContext(executeMethod); super.setErrorHandler(errorHandler); this.remoteAddress = executor.getAddressOfRemoteServer(); - - Object capabilityPlatform1 = getCapabilities().getCapability(PLATFORM_NAME); - Object capabilityAutomation1 = getCapabilities().getCapability(AUTOMATION_NAME); - - Object capabilityPlatform2 = capabilities.getCapability(PLATFORM_NAME); - Object capabilityAutomation2 = capabilities.getCapability(AUTOMATION_NAME); - - platformName = ofNullable(ofNullable(super.getPlatformName()) - .orElse(capabilityPlatform1 != null ? String.valueOf(capabilityPlatform1) : null)) - .orElse(capabilityPlatform2 != null ? String.valueOf(capabilityPlatform2) : null); - automationName = ofNullable(ofNullable(super.getAutomationName()) - .orElse(capabilityAutomation1 != null ? String.valueOf(capabilityAutomation1) : null)) - .orElse(capabilityAutomation2 != null ? String.valueOf(capabilityAutomation2) : null); - this.setElementConverter(new JsonToMobileElementConverter(this, this)); } @@ -282,15 +263,8 @@ public URL getRemoteAddress() { return remoteAddress; } - @Override public String getPlatformName() { - return platformName; - } - - @Override public String getAutomationName() { - return automationName; - } - @Override public boolean isBrowser() { - return !getContext().toLowerCase().contains("NATIVE_APP".toLowerCase()); + return super.isBrowser() && + !getContext().toLowerCase().contains("NATIVE_APP".toLowerCase()); } } diff --git a/src/main/java/io/appium/java_client/HasSessionDetails.java b/src/main/java/io/appium/java_client/HasSessionDetails.java index 3106b2905..0b4bc4b8b 100644 --- a/src/main/java/io/appium/java_client/HasSessionDetails.java +++ b/src/main/java/io/appium/java_client/HasSessionDetails.java @@ -18,6 +18,7 @@ import static io.appium.java_client.MobileCommand.GET_SESSION; import static java.util.Optional.ofNullable; +import static java.util.stream.Collectors.toMap; import static org.apache.commons.lang3.StringUtils.isBlank; import com.google.common.collect.ImmutableMap; @@ -34,8 +35,17 @@ public interface HasSessionDetails extends ExecutesMethod { @SuppressWarnings("unchecked") default Map getSessionDetails() { Response response = execute(GET_SESSION); + Map resultMap = Map.class.cast(response.getValue()); + return ImmutableMap.builder() - .putAll(Map.class.cast(response.getValue())).build(); + .putAll(resultMap.entrySet() + .stream().filter(entry -> { + String key = entry.getKey(); + Object value = entry.getValue(); + return !isBlank(key) + && value != null + && !isBlank(String.valueOf(value));} + ).collect(toMap(Map.Entry::getKey, Map.Entry::getValue))).build(); } default Object getSessionDetail(String detail) { @@ -43,17 +53,22 @@ default Object getSessionDetail(String detail) { } default String getPlatformName() { - Object platformName = getSessionDetail("platformName"); - return ofNullable(platformName != null ? String.valueOf(platformName) : null).orElse(null); + Object platformName = ofNullable(getSessionDetail("platformName")) + .orElseGet(() -> getSessionDetail("platform")); + return ofNullable(platformName).map(Object::toString).orElse(null); } default String getAutomationName() { - Object automationName = getSessionDetail("automationName"); - return ofNullable(automationName != null ? String.valueOf(automationName) : null).orElse(null); + return ofNullable(getSessionDetail("automationName")) + .map(Object::toString).orElse(null); } /** * @return is focus on browser or on native content. */ - boolean isBrowser(); + default boolean isBrowser() { + return ofNullable(getSessionDetail("browserName")) + .map(browserName -> browserName.toString()) + .orElse(null) != null; + } } From d2e5ff1a478e87ee761ce6f0384607ab11cf8b32 Mon Sep 17 00:00:00 2001 From: Sergey Tikhomirov Date: Sat, 23 Sep 2017 13:25:36 +0300 Subject: [PATCH 2/3] code style issues were got fixed --- src/main/java/io/appium/java_client/AppiumDriver.java | 4 ++-- .../java/io/appium/java_client/HasSessionDetails.java | 10 ++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/appium/java_client/AppiumDriver.java b/src/main/java/io/appium/java_client/AppiumDriver.java index 4f4616d3f..a301e6ba5 100644 --- a/src/main/java/io/appium/java_client/AppiumDriver.java +++ b/src/main/java/io/appium/java_client/AppiumDriver.java @@ -264,7 +264,7 @@ public URL getRemoteAddress() { } @Override public boolean isBrowser() { - return super.isBrowser() && - !getContext().toLowerCase().contains("NATIVE_APP".toLowerCase()); + return super.isBrowser() + && !getContext().toLowerCase().contains("NATIVE_APP".toLowerCase()); } } diff --git a/src/main/java/io/appium/java_client/HasSessionDetails.java b/src/main/java/io/appium/java_client/HasSessionDetails.java index 0b4bc4b8b..37fc6a5fe 100644 --- a/src/main/java/io/appium/java_client/HasSessionDetails.java +++ b/src/main/java/io/appium/java_client/HasSessionDetails.java @@ -44,20 +44,26 @@ default Map getSessionDetails() { Object value = entry.getValue(); return !isBlank(key) && value != null - && !isBlank(String.valueOf(value));} - ).collect(toMap(Map.Entry::getKey, Map.Entry::getValue))).build(); + && !isBlank(String.valueOf(value)); + }).collect(toMap(Map.Entry::getKey, Map.Entry::getValue))).build(); } default Object getSessionDetail(String detail) { return getSessionDetails().get(detail); } + /** + * @return name of the current mobile platform. + */ default String getPlatformName() { Object platformName = ofNullable(getSessionDetail("platformName")) .orElseGet(() -> getSessionDetail("platform")); return ofNullable(platformName).map(Object::toString).orElse(null); } + /** + * @return current automation name. + */ default String getAutomationName() { return ofNullable(getSessionDetail("automationName")) .map(Object::toString).orElse(null); From 89b22dddd55ab708d0ac923ad508b73bb1d1e5b6 Mon Sep 17 00:00:00 2001 From: Sergey Tikhomirov Date: Sun, 24 Sep 2017 00:04:45 +0300 Subject: [PATCH 3/3] some minor things that were found by reviewers were improved --- .../java/io/appium/java_client/AppiumDriver.java | 3 ++- .../io/appium/java_client/HasSessionDetails.java | 14 ++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/java/io/appium/java_client/AppiumDriver.java b/src/main/java/io/appium/java_client/AppiumDriver.java index a301e6ba5..dc7df8727 100644 --- a/src/main/java/io/appium/java_client/AppiumDriver.java +++ b/src/main/java/io/appium/java_client/AppiumDriver.java @@ -18,6 +18,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import static io.appium.java_client.remote.MobileCapabilityType.PLATFORM_NAME; +import static org.apache.commons.lang3.StringUtils.containsIgnoreCase; import com.google.common.collect.ImmutableMap; @@ -265,6 +266,6 @@ public URL getRemoteAddress() { @Override public boolean isBrowser() { return super.isBrowser() - && !getContext().toLowerCase().contains("NATIVE_APP".toLowerCase()); + && !containsIgnoreCase(getContext(), "NATIVE_APP"); } } diff --git a/src/main/java/io/appium/java_client/HasSessionDetails.java b/src/main/java/io/appium/java_client/HasSessionDetails.java index 37fc6a5fe..cefdfdf3d 100644 --- a/src/main/java/io/appium/java_client/HasSessionDetails.java +++ b/src/main/java/io/appium/java_client/HasSessionDetails.java @@ -26,6 +26,7 @@ import org.openqa.selenium.remote.Response; import java.util.Map; +import javax.annotation.Nullable; public interface HasSessionDetails extends ExecutesMethod { /** @@ -37,6 +38,8 @@ default Map getSessionDetails() { Response response = execute(GET_SESSION); Map resultMap = Map.class.cast(response.getValue()); + //this filtering was added to clear returned result. + //results of further operations should be simply interpreted by users return ImmutableMap.builder() .putAll(resultMap.entrySet() .stream().filter(entry -> { @@ -48,25 +51,25 @@ default Map getSessionDetails() { }).collect(toMap(Map.Entry::getKey, Map.Entry::getValue))).build(); } - default Object getSessionDetail(String detail) { + default @Nullable Object getSessionDetail(String detail) { return getSessionDetails().get(detail); } /** * @return name of the current mobile platform. */ - default String getPlatformName() { + default @Nullable String getPlatformName() { Object platformName = ofNullable(getSessionDetail("platformName")) .orElseGet(() -> getSessionDetail("platform")); - return ofNullable(platformName).map(Object::toString).orElse(null); + return ofNullable(platformName).map(String::valueOf).orElse(null); } /** * @return current automation name. */ - default String getAutomationName() { + default @Nullable String getAutomationName() { return ofNullable(getSessionDetail("automationName")) - .map(Object::toString).orElse(null); + .map(String::valueOf).orElse(null); } /** @@ -74,7 +77,6 @@ default String getAutomationName() { */ default boolean isBrowser() { return ofNullable(getSessionDetail("browserName")) - .map(browserName -> browserName.toString()) .orElse(null) != null; } }