Skip to content

Commit

Permalink
Also convert WebElements when decoding HttpRequests.
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed May 18, 2017
1 parent 2775361 commit e32b83d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
import org.openqa.selenium.remote.CommandCodec;
import org.openqa.selenium.remote.JsonToBeanConverter;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.remote.internal.JsonToWebElementConverter;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -123,6 +124,7 @@ public abstract class AbstractHttpCommandCodec implements CommandCodec<HttpReque
private final Map<String, String> aliases = new HashMap<>();
private final BeanToJsonConverter beanToJsonConverter = new BeanToJsonConverter();
private final JsonToBeanConverter jsonToBeanConverter = new JsonToBeanConverter();
private final JsonToWebElementConverter elementConverter = new JsonToWebElementConverter(null);

public AbstractHttpCommandCodec() {
defineCommand(STATUS, get("/status"));
Expand Down Expand Up @@ -268,7 +270,9 @@ public Command decode(final HttpRequest encodedCommand) {
String content = encodedCommand.getContentString();
if (!content.isEmpty()) {
@SuppressWarnings("unchecked")
HashMap<String, ?> tmp = jsonToBeanConverter.convert(HashMap.class, content);
Map<String, ?> tmp = jsonToBeanConverter.convert(HashMap.class, content);
//noinspection unchecked
tmp = (Map<String, ?>) elementConverter.apply(tmp);
parameters.putAll(tmp);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.openqa.selenium.remote.Dialect.OSS;
import static org.openqa.selenium.remote.http.HttpMethod.DELETE;
import static org.openqa.selenium.remote.http.HttpMethod.GET;
import static org.openqa.selenium.remote.http.HttpMethod.POST;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
Expand All @@ -42,9 +45,12 @@
import org.junit.runners.JUnit4;
import org.openqa.selenium.UnsupportedCommandException;
import org.openqa.selenium.remote.Command;
import org.openqa.selenium.remote.DriverCommand;
import org.openqa.selenium.remote.RemoteWebElement;
import org.openqa.selenium.remote.SessionId;

import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -356,4 +362,24 @@ public void treatsNullPathAsRoot_unrecognizedCommand() {
// Do nothing.
}
}

@Test
public void whenDecodingAnHttpRequestRecreateWebElements() {
Command command = new Command(
new SessionId("1234567"),
DriverCommand.EXECUTE_SCRIPT,
ImmutableMap.of(
"script", "",
"args", ImmutableList.of(ImmutableMap.of(OSS.getEncodedElementKey(), "67890"))));

HttpRequest request = codec.encode(command);

Command decoded = codec.decode(request);

List<?> args = (List<?>) decoded.getParameters().get("args");
assertEquals(RemoteWebElement.class, args.get(0).getClass());

RemoteWebElement element = (RemoteWebElement) args.get(0);
assertEquals("67890", element.getId());
}
}

0 comments on commit e32b83d

Please sign in to comment.