Skip to content

Cache session details to avoid making unnecessary queries #765

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@
import java.util.List;
import java.util.Map;

import static io.appium.java_client.MobileCommand.GET_SESSION;
import static java.util.stream.Collectors.toMap;
import static org.apache.commons.lang3.StringUtils.isBlank;

@SuppressWarnings({"unchecked", "rawtypes"})
abstract class DefaultGenericMobileDriver<T extends WebElement> extends RemoteWebDriver
implements MobileDriver<T> {

private ImmutableMap sessionDetails = null;

public DefaultGenericMobileDriver(CommandExecutor executor, Capabilities desiredCapabilities) {
super(executor, desiredCapabilities);
}
Expand Down Expand Up @@ -151,6 +157,32 @@ public List findElementsByXPath(String using) {
return super.getMouse();
}

/**
* @return a map with values that hold session details.
*
*/
@SuppressWarnings("unchecked")
@Override
public synchronized Map<String, Object> getSessionDetails() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I am against this change. Session details data is not immutable. It may change when driver is switched to another context (NATIVE APP or WEB VIEW).

I think the right way is to find places where it is invoked frequently.

Copy link

Choose a reason for hiding this comment

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

If full copy is used, shouldn't there at least be an 'expiry time' or a method to force the details to be refreshed?

When I ran into this issue I decided to use an expiry time, based on the time it took to retrieve the information, in my (not so nice) workaround in https://github.com/fhoeben/hsac-fitnesse-appium.
The approach I currently use there is to have some cache at the element level, and using my own 'json to element' converter.
My custom elements require less retrieval of session details (for me the 'foundBy' information does not require session details, which is currently required for the driver's toString(), and they 'cache' information retrieved from the driver about an element for a short while (so my test code does not need to worry too much about for instance checking isDisplayed() twice)).

if (sessionDetails == null) {
Response response = execute(GET_SESSION);
Map<String, Object> resultMap = Map.class.cast(response.getValue());

//this filtering was added to clear returned result.
//results of further operations should be simply interpreted by users
sessionDetails = ImmutableMap.<String, Object>builder()
.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();
}
return sessionDetails;
}

@Override
public String toString() {
return String.format("%s: %s", getPlatformName(),
Expand Down
34 changes: 7 additions & 27 deletions src/main/java/io/appium/java_client/HasSessionDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,21 @@

package io.appium.java_client;

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;

import org.openqa.selenium.remote.Response;

import java.util.Map;
import javax.annotation.Nullable;

public interface HasSessionDetails extends ExecutesMethod {

Map<String, Object> getSessionDetails();

/**
* @return a map with values that hold session details.
* Get the value of the corresponding session detail.
*
* @param detail The name of the details to get in the map.
* @return The value of the detail.
*/
@SuppressWarnings("unchecked")
default Map<String, Object> getSessionDetails() {
Response response = execute(GET_SESSION);
Map<String, Object> 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.<String, Object>builder()
.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 @Nullable Object getSessionDetail(String detail) {
return getSessionDetails().get(detail);
}
Expand All @@ -67,7 +47,7 @@ default Map<String, Object> getSessionDetails() {
/**
* @return current automation name.
*/
default @Nullable String getAutomationName() {
default @Nullable String getAutomationName() {
return ofNullable(getSessionDetail("automationName"))
.map(String::valueOf).orElse(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
import java.util.concurrent.TimeUnit;

public abstract class AbstractStubWebDriver implements WebDriver, HasSessionDetails {

@Override
public Map<String, Object> getSessionDetails() {
return null;
}

@Override
public Response execute(String driverCommand, Map<String, ?> parameters) {
return null;
Expand Down