From bd1cc89968305e3304c96812e83a008f8c37a262 Mon Sep 17 00:00:00 2001 From: Simon Stewart Date: Sat, 3 Sep 2016 20:10:19 +0100 Subject: [PATCH] Java: Get the remote-client tests passing --- .../org/openqa/selenium/remote/Dialect.java | 11 +++++++++ .../internal/JsonToWebElementConverter.java | 10 ++++---- .../internal/WebElementToJsonConverter.java | 6 +++-- .../WebElementToJsonConverterTest.java | 23 ++++++++++--------- 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/java/client/src/org/openqa/selenium/remote/Dialect.java b/java/client/src/org/openqa/selenium/remote/Dialect.java index a5c4bc95f4804..5b0ece9fc96fb 100644 --- a/java/client/src/org/openqa/selenium/remote/Dialect.java +++ b/java/client/src/org/openqa/selenium/remote/Dialect.java @@ -35,6 +35,11 @@ public CommandCodec getCommandCodec() { public ResponseCodec getResponseCodec() { return new JsonHttpResponseCodec(); } + + @Override + public String getEncodedElementKey() { + return "ELEMENT"; + } }, W3C { @Override @@ -46,8 +51,14 @@ public CommandCodec getCommandCodec() { public ResponseCodec getResponseCodec() { return new W3CHttpResponseCodec(); } + + @Override + public String getEncodedElementKey() { + return "element-6066-11e4-a52e-4f735466cecf"; + } }; public abstract CommandCodec getCommandCodec(); public abstract ResponseCodec getResponseCodec(); + public abstract String getEncodedElementKey(); } diff --git a/java/client/src/org/openqa/selenium/remote/internal/JsonToWebElementConverter.java b/java/client/src/org/openqa/selenium/remote/internal/JsonToWebElementConverter.java index 736ca1af9cbc2..e00abb97b4edc 100644 --- a/java/client/src/org/openqa/selenium/remote/internal/JsonToWebElementConverter.java +++ b/java/client/src/org/openqa/selenium/remote/internal/JsonToWebElementConverter.java @@ -23,6 +23,7 @@ import com.google.common.collect.Maps; import org.openqa.selenium.WebElement; +import org.openqa.selenium.remote.Dialect; import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.remote.RemoteWebElement; @@ -34,6 +35,7 @@ * and Maps to catch nested references. All other values pass through the converter unchanged. */ public class JsonToWebElementConverter implements Function { + private final RemoteWebDriver driver; public JsonToWebElementConverter(RemoteWebDriver driver) { @@ -48,14 +50,14 @@ public Object apply(Object result) { if (result instanceof Map) { Map resultAsMap = (Map) result; - if (resultAsMap.containsKey("ELEMENT")) { + if (resultAsMap.containsKey(Dialect.OSS.getEncodedElementKey())) { RemoteWebElement element = newRemoteWebElement(); - element.setId(String.valueOf(resultAsMap.get("ELEMENT"))); + element.setId(String.valueOf(resultAsMap.get(Dialect.OSS.getEncodedElementKey()))); element.setFileDetector(driver.getFileDetector()); return element; - } else if (resultAsMap.containsKey("element-6066-11e4-a52e-4f735466cecf")) { + } else if (resultAsMap.containsKey(Dialect.W3C.getEncodedElementKey())) { RemoteWebElement element = newRemoteWebElement(); - element.setId(String.valueOf(resultAsMap.get("element-6066-11e4-a52e-4f735466cecf"))); + element.setId(String.valueOf(resultAsMap.get(Dialect.W3C.getEncodedElementKey()))); element.setFileDetector(driver.getFileDetector()); return element; } else { diff --git a/java/client/src/org/openqa/selenium/remote/internal/WebElementToJsonConverter.java b/java/client/src/org/openqa/selenium/remote/internal/WebElementToJsonConverter.java index bd6da4e686067..14addc5ed56a6 100644 --- a/java/client/src/org/openqa/selenium/remote/internal/WebElementToJsonConverter.java +++ b/java/client/src/org/openqa/selenium/remote/internal/WebElementToJsonConverter.java @@ -24,6 +24,7 @@ import com.google.common.collect.Maps; import org.openqa.selenium.internal.WrapsElement; +import org.openqa.selenium.remote.Dialect; import org.openqa.selenium.remote.RemoteWebElement; import java.util.Collection; @@ -50,8 +51,9 @@ public Object apply(Object arg) { } if (arg instanceof RemoteWebElement) { - return ImmutableMap.of("ELEMENT", ((RemoteWebElement) arg).getId(), - "element-6066-11e4-a52e-4f735466cecf", ((RemoteWebElement) arg).getId()); + return ImmutableMap.of( + Dialect.OSS.getEncodedElementKey(), ((RemoteWebElement) arg).getId(), + Dialect.W3C.getEncodedElementKey(), ((RemoteWebElement) arg).getId()); } if (arg.getClass().isArray()) { diff --git a/java/client/test/org/openqa/selenium/remote/internal/WebElementToJsonConverterTest.java b/java/client/test/org/openqa/selenium/remote/internal/WebElementToJsonConverterTest.java index 2389aae898bc2..0f5ff21549d9d 100644 --- a/java/client/test/org/openqa/selenium/remote/internal/WebElementToJsonConverterTest.java +++ b/java/client/test/org/openqa/selenium/remote/internal/WebElementToJsonConverterTest.java @@ -28,6 +28,9 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; import org.openqa.selenium.By; import org.openqa.selenium.Dimension; import org.openqa.selenium.OutputType; @@ -36,19 +39,13 @@ import org.openqa.selenium.WebDriverException; import org.openqa.selenium.WebElement; import org.openqa.selenium.internal.WrapsElement; +import org.openqa.selenium.remote.Dialect; import org.openqa.selenium.remote.RemoteWebElement; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - import java.util.Collection; import java.util.List; import java.util.Map; -/** - * Unit tests for {@link WebElementToJsonConverter}. - */ @RunWith(JUnit4.class) public class WebElementToJsonConverterTest { @@ -234,7 +231,9 @@ public void convertsAnArrayWithAWebElement() { Object value = CONVERTER.apply(new Object[] { element }); assertContentsInOrder(Lists.newArrayList((Collection) value), - ImmutableMap.of("ELEMENT", "abc123")); + ImmutableMap.of( + Dialect.OSS.getEncodedElementKey(), "abc123", + Dialect.W3C.getEncodedElementKey(), "abc123")); } @Test @@ -254,9 +253,11 @@ private static void assertIsWebElementObject(Object value, String expectedKey) { assertThat(value, instanceOf(Map.class)); Map map = (Map) value; - assertEquals(1, map.size()); - assertTrue(map.containsKey("ELEMENT")); - assertEquals(expectedKey, map.get("ELEMENT")); + assertEquals(2, map.size()); + assertTrue(map.containsKey(Dialect.OSS.getEncodedElementKey())); + assertEquals(expectedKey, map.get(Dialect.OSS.getEncodedElementKey())); + assertTrue(map.containsKey(Dialect.W3C.getEncodedElementKey())); + assertEquals(expectedKey, map.get(Dialect.W3C.getEncodedElementKey())); } private static void assertContentsInOrder(List list, Object... expectedContents) {