Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 2 additions & 28 deletions src/main/java/io/appium/java_client/AppiumDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -72,9 +70,6 @@ public class AppiumDriver<T extends WebElement>
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}
Expand All @@ -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));
}

Expand Down Expand Up @@ -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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apache's StringUtils has StringUtils.containsIgnoreCase method

}
}
33 changes: 27 additions & 6 deletions src/main/java/io/appium/java_client/HasSessionDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,26 +35,46 @@ public interface HasSessionDetails extends ExecutesMethod {
@SuppressWarnings("unchecked")
default Map<String, Object> getSessionDetails() {
Response response = execute(GET_SESSION);
Map<String, Object> resultMap = Map.class.cast(response.getValue());

return ImmutableMap.<String, Object>builder()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be helpful to have a comment here about why additional filtering is necessary

.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) {
return getSessionDetails().get(detail);
}

/**
* @return name of the current mobile platform.
*/
default String getPlatformName() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also mark these with Nullable annotation

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment for the stuff below

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);
}

/**
* @return current automation name.
*/
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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be String::valueOf

.orElse(null) != null;
}
}