Skip to content

Commit e32b83d

Browse files
committed
Also convert WebElements when decoding HttpRequests.
1 parent 2775361 commit e32b83d

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

java/client/src/org/openqa/selenium/remote/http/AbstractHttpCommandCodec.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
import org.openqa.selenium.remote.CommandCodec;
106106
import org.openqa.selenium.remote.JsonToBeanConverter;
107107
import org.openqa.selenium.remote.SessionId;
108+
import org.openqa.selenium.remote.internal.JsonToWebElementConverter;
108109

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

127129
public AbstractHttpCommandCodec() {
128130
defineCommand(STATUS, get("/status"));
@@ -268,7 +270,9 @@ public Command decode(final HttpRequest encodedCommand) {
268270
String content = encodedCommand.getContentString();
269271
if (!content.isEmpty()) {
270272
@SuppressWarnings("unchecked")
271-
HashMap<String, ?> tmp = jsonToBeanConverter.convert(HashMap.class, content);
273+
Map<String, ?> tmp = jsonToBeanConverter.convert(HashMap.class, content);
274+
//noinspection unchecked
275+
tmp = (Map<String, ?>) elementConverter.apply(tmp);
272276
parameters.putAll(tmp);
273277
}
274278

java/client/test/org/openqa/selenium/remote/http/JsonHttpCommandCodecTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@
2727
import static org.hamcrest.Matchers.is;
2828
import static org.hamcrest.Matchers.nullValue;
2929
import static org.hamcrest.Matchers.startsWith;
30+
import static org.junit.Assert.assertEquals;
3031
import static org.junit.Assert.assertThat;
3132
import static org.junit.Assert.fail;
33+
import static org.openqa.selenium.remote.Dialect.OSS;
3234
import static org.openqa.selenium.remote.http.HttpMethod.DELETE;
3335
import static org.openqa.selenium.remote.http.HttpMethod.GET;
3436
import static org.openqa.selenium.remote.http.HttpMethod.POST;
3537

38+
import com.google.common.collect.ImmutableList;
3639
import com.google.common.collect.ImmutableMap;
3740
import com.google.gson.JsonNull;
3841
import com.google.gson.JsonObject;
@@ -42,9 +45,12 @@
4245
import org.junit.runners.JUnit4;
4346
import org.openqa.selenium.UnsupportedCommandException;
4447
import org.openqa.selenium.remote.Command;
48+
import org.openqa.selenium.remote.DriverCommand;
49+
import org.openqa.selenium.remote.RemoteWebElement;
4550
import org.openqa.selenium.remote.SessionId;
4651

4752
import java.net.URISyntaxException;
53+
import java.util.List;
4854
import java.util.Map;
4955

5056
/**
@@ -356,4 +362,24 @@ public void treatsNullPathAsRoot_unrecognizedCommand() {
356362
// Do nothing.
357363
}
358364
}
365+
366+
@Test
367+
public void whenDecodingAnHttpRequestRecreateWebElements() {
368+
Command command = new Command(
369+
new SessionId("1234567"),
370+
DriverCommand.EXECUTE_SCRIPT,
371+
ImmutableMap.of(
372+
"script", "",
373+
"args", ImmutableList.of(ImmutableMap.of(OSS.getEncodedElementKey(), "67890"))));
374+
375+
HttpRequest request = codec.encode(command);
376+
377+
Command decoded = codec.decode(request);
378+
379+
List<?> args = (List<?>) decoded.getParameters().get("args");
380+
assertEquals(RemoteWebElement.class, args.get(0).getClass());
381+
382+
RemoteWebElement element = (RemoteWebElement) args.get(0);
383+
assertEquals("67890", element.getId());
384+
}
359385
}

0 commit comments

Comments
 (0)