Skip to content

Commit

Permalink
Java: Get the remote-client tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Sep 3, 2016
1 parent 20c37c3 commit bd1cc89
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
11 changes: 11 additions & 0 deletions java/client/src/org/openqa/selenium/remote/Dialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public CommandCodec<HttpRequest> getCommandCodec() {
public ResponseCodec<HttpResponse> getResponseCodec() {
return new JsonHttpResponseCodec();
}

@Override
public String getEncodedElementKey() {
return "ELEMENT";
}
},
W3C {
@Override
Expand All @@ -46,8 +51,14 @@ public CommandCodec<HttpRequest> getCommandCodec() {
public ResponseCodec<HttpResponse> getResponseCodec() {
return new W3CHttpResponseCodec();
}

@Override
public String getEncodedElementKey() {
return "element-6066-11e4-a52e-4f735466cecf";
}
};

public abstract CommandCodec<HttpRequest> getCommandCodec();
public abstract ResponseCodec<HttpResponse> getResponseCodec();
public abstract String getEncodedElementKey();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -34,6 +35,7 @@
* and Maps to catch nested references. All other values pass through the converter unchanged.
*/
public class JsonToWebElementConverter implements Function<Object, Object> {

private final RemoteWebDriver driver;

public JsonToWebElementConverter(RemoteWebDriver driver) {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit bd1cc89

Please sign in to comment.